在ggplot2中生成图例 [英] Generating a legend in ggplot2
问题描述
我试图为我的ggplot生成图例,在这里我想用11个数据点在2.5个月的时间范围内说明五行.我可以生成图形,但是将图例与线的颜色和名称进行比较无法正常工作.我尝试了不同的命令,即scale_colour_manual,scale_fill_discrete,opts,指南等...但没有任何效果.我不知道这是怎么回事.请任何人都可以帮助我吗?
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)
例如,使用以下命令(包括comman scale_colour_manual)来生成图形.包括此命令在内,R不会创建图形.如果您从ggplot命令中将其删除,则图形会显示出来:
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
最后,我还尝试将命令scale_fill_discrete或指南添加到SP中.也就是说:SP + guides(fill = guide_legend(reverse=TRUE))
或SP + scale_fill_discrete(name="Legende", labels=c("Sehr viel Spaß", "Viel Spaß", "Normal", "Wenig Spaß", "Sehr wenig Spaß"))
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
推荐答案
要扩展Heroka的注释,您需要使用tidyr
或reshape2
将数据从宽格式转换为长格式.每个软件包的小插图都对此进行了深入介绍.然后,您可以在geom_line
的aes
调用中设置颜色,并使用scale_color_manual
适当设置中断,颜色和标签.
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ß")
这篇关于在ggplot2中生成图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!