在图上使用多行将图例添加到ggplot2 [英] Adding legend to ggplot2 with multiple lines on plot
问题描述
我真的很感谢您的帮助.我完全不了解ggplot2如何思考传说!
I would really appreciate some help with this. I completely do not understand how ggplot2 is thinking about legends!
该图表基于名为"meltdf"的以下数据框:
The chart is based on the following data frame, called "meltdf":
xval variable value
1 0.000000 Shortfall Risk 100.00
2 4.624457 Shortfall Risk 99.83
3 9.179656 Shortfall Risk 60.96
4 13.742579 Shortfall Risk 36.29
5 18.620338 Shortfall Risk 27.71
6 22.947704 Shortfall Risk 22.52
7 27.690638 Shortfall Risk 19.72
8 32.174379 Shortfall Risk 17.89
9 36.637940 Shortfall Risk 15.79
10 41.107962 Shortfall Risk 15.96
11 45.644065 Shortfall Risk 15.97
图表绘制如下:
ggplot(data=meltdf,aes(x=xval,y=value))+
geom_line(size=1,colour=rgb(69,99,111,max=255))+
geom_vline(xintercept = 22 ,colour="darkgray")+
geom_vline(xintercept = 30 ,colour="darkred")+
theme_bw()+
labs(title="Shortfall Risk versus Investment Risk, Meeting Expenditure Only")+
theme(legend.position="bottom",
legend.text = element_text(size=9),
axis.text = element_text(size=9),
axis.title = element_text(size=9),
plot.title=element_text(size = 9),
legend.title=element_text(size=9))+
labs(x="Largest Historical Decline (%)", y="Probability of Shortfall (%)")+
scale_y_continuous(limits = c(0, 100))
我想在底部有一个图例,图中的所有三行(蓝色,红色和灰色)均被命名.
I would like to have a legend at the bottom in which all three lines in the chart (blue, red and grey) are named.
推荐答案
ggplot将自动为映射在aes()
调用中的元素(颜色,线型等)生成图例.这意味着当您手动指定元素的颜色时,就像在这里一样(所有colour
语句都不在aes
内),您将不会得到图例.
ggplot will automatically produce legend for the elements (colours, linetype, etc.) that are mapped inside an aes()
call. It means that you will not get a legend when you manually specify the colour of an element, like you do here (all your colour
statements are not inside aes
).
我建议您将垂直线的坐标存储在其自己的数据框中,在此处可以将其映射到给出其颜色的变量.在这里,我创建了这样一个数据框(summ
),并将您的代码重写为geom_vline()
.我还将meltdf$variable
映射到geom_vline
中的colour
,因此它出现在图例中.最后,我添加了scale_colour_manual()
选择颜色.您可能需要调整summ$colour
的值,以便它们在图例中有意义.
I recommend you store the coordinates of your vertical lines in their own dataframe, where they can be mapped to a variable giving their color. Here, I create such a dataframe (summ
) and rewrote your code for geom_vline()
. I also mapped meltdf$variable
to colour
in geom_vline
so it appears in the legend. Finally, I added scale_colour_manual()
to choose the colours. You may want to adapt the value of summ$colour
so they make sense in the legend.
summ <- data.frame(x=c(22,30),
colour=c("gray","red"))
ggplot(data=meltdf,aes(x=xval,y=value))+
geom_line(size=1,aes(colour=variable))+
geom_vline(data=summ,aes(xintercept = x,colour=colour))+
scale_color_manual(values = c(rgb(69,99,111,max=255),"darkgray","darkred")) +
theme_bw()+
labs(title="Shortfall Risk versus Investment Risk, Meeting Expenditure Only")+
theme(legend.position="bottom",
legend.text = element_text(size=9),
axis.text = element_text(size=9),
axis.title = element_text(size=9),
plot.title=element_text(size = 9),
legend.title=element_text(size=9))+
labs(x="Largest Historical Decline (%)", y="Probability of Shortfall (%)")+
scale_y_continuous(limits = c(0, 100))
这篇关于在图上使用多行将图例添加到ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!