从两个 data.frame 绘制数据时出现图例问题
我在让 ggplot2 按我想要的方式工作时遇到了一些麻烦。基本上,我想通过将实际观察结果与近似值放在一个图中来进行比较。例如,
> library(ggplot2)
> df.actual <- data.frame(x = 1:100, y = (1:100) * 2)
> df.approx <- data.frame(x = 1:150, y = (1:150) * 2 + 5 + rnorm(150, mean = 3) )
> ggplot() + geom_point(aes(x, y), data = df.actual) + geom_line(aes(x,y), data = df.approx)
我的问题是无法显示图例。我在某处读到 ggplot2 的图例不是很灵活(?)。理想情况下,图例的
- 标题 = '类型'
- 键:黑色填充点,黑色线条
- 键标签:'实际'、'近似'
- legend.position = 'topright'
谢谢。
I'm having a little trouble with getting ggplot2 to work as I want. Basically, I'd like to compare actual observations vs. approximated by putting them in one single plot. For example,
> library(ggplot2)
> df.actual <- data.frame(x = 1:100, y = (1:100) * 2)
> df.approx <- data.frame(x = 1:150, y = (1:150) * 2 + 5 + rnorm(150, mean = 3) )
> ggplot() + geom_point(aes(x, y), data = df.actual) + geom_line(aes(x,y), data = df.approx)
My problem is that I can't display a legend. I read somewhere that ggplot2's legend is not very flexible(?). Ideally, a legend with
- title = 'Type'
- key: a black filled point, and a black line
- key label: 'Actual', 'Approximate'
- legend.position = 'topright'
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个来帮助您入门
Try this to get you started
这是某种通过操纵网格对象来修改图例的黑客:
ggplot2 输出 http ://img134.imageshack.us/img134/8427/ggplotlegend.png
This is some kind of hack to modify the legend by manipulation of the grid object:
ggplot2 output http://img134.imageshack.us/img134/8427/ggplotlegend.png