Generating a legend in ggplot2

2019-09-21 10:49发布

I tried to generate a legend for my ggplot, where I want to illustrate five different lines over a time horizon of 2,5 months with 11 datapoints. I can generate the graph, but it doesn't work to get the legend comparing to the colors and names of the lines. I tried different commands, i.e. scale_colour_manual, scale_fill_discrete, opts, guides,... but nothing works. I don't know what's wrong.. Can please anybody help me?

Here are the commands to the define the data frame.

Spaß <- read.csv(file="Spaßfaktor.csv", header = TRUE, sep = ";", dec=",")

SVS <- ts(Spaß$Sehr.Viel.Spaß, deltat=1/52)
VS <- ts(Viel.Spaß, deltat=1/52)
N <- ts(Normal, deltat=1/52)
WS <- ts(wenig.Spaß, deltat=1/52)
SWS <- ts(Sehr.wenig.Spaß, deltat=1/52)

ZP <- strptime(Spaß$Zeitpunkt, format="%d.%m.%Y")
ZP

df<-data.frame(ZP, SVS, VS, N, WS, SWS)

To generate the graph, I used for example the following command (which includes the comman scale_colour_manual). Including this command, R doesn't create the graph. If you delete it out of the ggplot command, the graph shwos up:

       SP<-ggplot(df, aes(Zeit) ) +
      geom_line(aes(y=SVS), colour="green", lwd=1.3) +  # first layer
      geom_line(aes(y=VS), colour="blue", lwd=1.3) +
      geom_line(aes(y=N), colour="black", lwd=1.3) +  
      geom_line(aes(y=WS), colour="orange", lwd=1.3) +
      geom_line(aes(y=SWS), colour="red", lwd=1.3) +
       scale_colour_manual("", 
                breaks = c("Sehr viel Spaß", "Viel Spaß", "Normal", "Wenig Spaß", "Sehr wenig Spaß"),
                          values = c("green", "blue", "black", "Orange", "Red"))+
      xlab("Zeit") +
      scale_y_continuous("Anzahl in Prozent", limits = c(0,65)) + 
      labs(title="Häufigkeit der Auspräungen des Faktors Spaß")

SP

I also tried the command scale_fill_discrete or guide at the end by adding it to the SP. That means i.e.: SP + guides(fill = guide_legend(reverse=TRUE)) or SP + scale_fill_discrete(name="Legende", labels=c("Sehr viel Spaß", "Viel Spaß", "Normal", "Wenig Spaß", "Sehr wenig Spaß"))

Can please anybody help me??

The datas I generated on my own with a survey. They are shown underneath:

Varaible       Spaß    Beurteilung
sehr viel Spaß/Sehr gut 4   5
Viel Spaß/Gut     8     6
Normal/Neutral    10    9
wenig Spaß/schlecht    3    4
sehr wenig Spaß/Sehr schlecht      3    4

标签: r ggplot2 legend
1条回答
劫难
2楼-- · 2019-09-21 10:58

To expand on Heroka's comment you'll want to use tidyr or reshape2 to convert your data from wide format to long format. The vignettes for each package cover this in-depth. You'd then set color in your aes call in geom_line and use scale_color_manual to set the breaks, colors and labels appropriately.

library(tidyr)
library(ggplot2)

# using your input dataset provided in the separate answer
df <- gather(df, variable, value, -Zeitpunkt)

ggplot(df, aes(x = ZP) ) +
  geom_line(aes(y = value, colour = variable, group = variable), lwd = 1.3) + 
  scale_colour_manual(labels = c("Sehr viel Spaß", "Viel Spaß", "Normal", "Wenig Spaß", "Sehr wenig Spaß"),
                      breaks = c("SVS","VS","N","WS","W"),
                      values = c("green", "blue", "black", "Orange", "Red")) +
  xlab("Zeit") +
  scale_y_continuous("Anzahl in Prozent", limits = c(0,65)) + 
  labs(title="Häufigkeit der Auspräungen des Faktors Spaß")

enter image description here

查看更多
登录 后发表回答