我只是尝试制作一个折线图并使用 R 中的 ggplot 添加图例。以下是我的代码。
ggplot(mtcars, aes(x=mpg, y=wt)) + geom_line(stat = "identity") + scale_fill_identity(name = "", guide = "legend", labels = c("myLegend"))
and I got the following:
The legend is not shown in the plot and what I want is the following:
我用 Matlab 绘制了它。谁能告诉我如何在 R 中做到这一点?太感谢了!!
您的绘图未显示图例,因为没有将美学映射到该线。基本上,ggplot 认为没有理由添加图例,因为只有一行。
获取图例的一个简单方法是将线型映射到字符串:
ggplot(mtcars, aes(x=mpg, y=wt, lty = 'MyLegend')) + geom_line()
你可以看看?scale_linetype
有关如何修改该图例的信息。
例如,使用+ scale_linetype('MyLegendTitle')
更改图例标题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)