问题描述
我在ggplot中有一个绘图,有4个单独的行,我已经添加了一个单独的geom_line()参数。我想添加图例,但scale_colour_manual在这种情况下不起作用。
这是我的代码:
<$ p $ (线性类型= 1,大小= 0.3)+
scale_x_continuous(breaks = seq)(p =
ggplot(proba [108:140,],aes(c,four))+
gem_line (110,140,5))+
theme_bw()+
theme(axis.line = element_line(color =black,size = 0.25),
panel.grid.major = element_blank( ),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())+
theme(axis.text .x = element_text(angle = 0,hjust = +0.5,size = 6,color =black))+
theme(axis.text.y = element_text(angle = 0,hjust = -100,size =color=black))+
theme(axis.ticks = element_line(color =black,size = 0.25))+
xlab(\ nTime-steps)+
ylab(Proportion correct \\\
)+
theme(axis.text = element_text(size = 8),axis.title = element_text(size = 8))+
geom_line(aes(c,3),size = 0.2,linetype = 2)+
geom_line(aes(c,one),linetype = 3,size = 0.8,color =darkgrey)+
geom_line(aes(c,two),linetype = 1,size = 0.8,color =darkgrey)
我没有你的数据,但这是一个使用> iris
一行随机y值的例子:
library(ggplot2)
line.data< - data.frame(x = seq(0,10,length.out = 10),y = runif(10,0,10))
qplot(Sepal.Length,Petal.Length,color = Species,data = iris)+
geom_line(aes(x ,y,color =My Line),data = line.data)
关键要注意的是,重新创建审美映射,而不是将颜色映射到列i在数据框中,您将它映射到您指定的字符串。就像来自数据框的值一样, ggplot
将为该值分配一个颜色。您可以通过在数据框中添加一个 Species
列来生成与上面相同的图:
line.data $ Species< - My Line
qplot(Sepal.Length,Petal.Length,color = Species,data = iris)+
geom_line(aes(x ,y),data = line.data)
无论哪种方式,如果您不喜欢颜色 ggplot2
赋值,那么您可以使用 scale_color_manual
指定您自己的:
qplot(Sepal.Length,Petal.Length,color = Species,data = iris)+
geom_line(aes(x,y,color =My Line) ,data = line.data)+
scale_color_manual(values = c(setosa=blue4,versicolor=red4,
virginica=purple4,My Line =grey))
I have a plot in ggplot with 4 separate lines that I have added with a separate geom_line() argument. I would like to add legend but scale_colour_manual doesn't work in this case. What is the proper way to add legends when I added the variables separately?
Here's my code:
ggplot(proba[108:140,], aes(c,four)) +
geom_line(linetype=1, size=0.3) +
scale_x_continuous(breaks=seq(110,140,5)) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) +
theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) +
theme(axis.ticks=element_line(colour="black",size=0.25)) +
xlab("\nTime-steps") +
ylab("Proportion correct\n") +
theme(axis.text=element_text(size=8),axis.title=element_text(size=8)) +
geom_line(aes(c,three), size=0.2, linetype=2) +
geom_line(aes(c,one),linetype=3, size=0.8, colour="darkgrey") +
geom_line(aes(c,two), linetype=1, size=0.8, colour="darkgrey")
Just set the color name in aes
to whatever the line's name on the legend should be.
I don't have your data, but here's an example using iris
a line with random y values:
library(ggplot2)
line.data <- data.frame(x=seq(0, 10, length.out=10), y=runif(10, 0, 10))
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
geom_line(aes(x, y, color="My Line"), data=line.data)
The key thing to note is that you're creating an aesthetic mapping, but instead of mapping color to a column in a data frame, you're mapping it to a string you specify. ggplot
will assign a color to that value, just as with values that come from a data frame. You could have produced the same plot as above by adding a Species
column to the data frame:
line.data$Species <- "My Line"
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
geom_line(aes(x, y), data=line.data)
Either way, if you don't like the color ggplot2
assigns, then you can specify your own using scale_color_manual
:
qplot(Sepal.Length, Petal.Length, color=Species, data=iris) +
geom_line(aes(x, y, color="My Line"), data=line.data) +
scale_color_manual(values=c("setosa"="blue4", "versicolor"="red4",
"virginica"="purple4", "My Line"="gray"))
Another alternative is to just directly label the lines, or to make the purpose of the lines obvious from the context. Really, the best option depends on your specific circumstances.
这篇关于手动添加行时向ggplot添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!