将图例添加到 r 中的 geom_line() 图中 [英] Add legend to geom_line() graph in r
本文介绍了将图例添加到 r 中的 geom_line() 图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图在我的 ggplot 中添加图例,但失败了.我尝试了函数 scale_colour_manual()
,但图例没有出现.
I've been trying to add legend to my ggplot, but failed miserably. I tried the function scale_colour_manual()
, but the legend doesn't show up.
ggplot()+
geom_line(data=Summary,aes(y=Y1,x= X),colour="darkblue",size=1 )+
geom_line(data=Summary,aes(y=Y2,x= X),colour="red",size=1 )
我的数据框摘要"如下:
My dataframe 'Summary' is as follows:
X Y1 Y2
139 1.465477e+16 7.173075e+15
277 1.044803e+16 9.275002e+15
415 1.059258e+16 8.562518e+15
553 1.033283e+16 8.268984e+15
691 9.548019e+15 1.022248e+16
830 1.008212e+16 8.641891e+15
968 9.822061e+15 9.315856e+15
1106 9.948143e+15 9.178694e+15
1244 1.013922e+16 8.825904e+15
1382 9.815094e+15 9.283662e+15
请告诉我如何在同一张图上绘制 Y1、Y2 与 X 的关系图并在侧面添加图例.
Please advise me how to plot Y1, Y2 against X on the same graph and add a legend on the side.
推荐答案
ggplot
需要 aes
制作图例,将 colour
移动到 code>aes(...)
将自动构建一个图例.然后我们可以通过 scale_color_manual
调整标签颜色配对:
ggplot
needs aes
to make a legend, moving colour
inside aes(...)
will build a legend automatically. then we can adjust the labels-colors pairing via scale_color_manual
:
ggplot()+
geom_line(data=Summary,aes(y=Y1,x= X,colour="Y1"),size=1 )+
geom_line(data=Summary,aes(y=Y2,x= X,colour="Y2"),size=1) +
scale_color_manual(name = "Y series", values = c("Y1" = "darkblue", "Y2" = "red"))
这篇关于将图例添加到 r 中的 geom_line() 图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文