[英]Adding legend to a single line chart using ggplot
我只是尝试制作折线图并使用R中的ggplot为其添加图例。以下是我的代码。
ggplot(mtcars, aes(x=mpg, y=wt)) + geom_line(stat = "identity") + scale_fill_identity(name = "", guide = "legend", labels = c("myLegend"))
我得到以下内容:
图例中未显示图例,我想要的是以下内容:
我用Matlab绘制的。 谁能告诉我如何在R中做到这一点? 非常感谢!!
你的情节没有显示传奇,因为没有美学映射到线条。 基本上,ggplot没有理由添加图例,因为只有一行。
获取图例的一种简单方法是将线型映射到字符串:
ggplot(mtcars, aes(x=mpg, y=wt, lty = 'MyLegend')) + geom_line()
您可以查看?scale_linetype以获取有关如何修改图例的信息。
?scale_linetype
例如,使用+ scale_linetype('MyLegendTitle')更改图例标题。
+ scale_linetype('MyLegendTitle')
[英]ggplot: adding a legend entry for a line to a grouped bar chart
[英]Adding a bar chart to a ggplot2 legend
[英]Adding legend to pie chart that is wrapped by ggplot
[英]Adding a legend to GGPlot Lollipop Chart Based on Colour
[英]How to add legend to ggplot2 line chart?
[英]Create legend for line chart R GGPlot2
[英]adding a legend using ggplot2
[英]Adding a legend to a ggplot with a scatterplot and a line graph
[英]Adding legend for geom_line to ggplot
[英]Adding a legend to a combined line and bargraph ggplot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.