在绘制来自两个data.frame的数据时遇到问题 [英] problem with legend while plotting data from two data.frame
本文介绍了在绘制来自两个data.frame的数据时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果想让ggplot2正常工作,我有点麻烦。基本上,我想比较实际的观察值和近似值,并将它们放在一个单独的图中。例如,
>库(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的传说不太灵活(?)。理想情况下,带有
谢谢。
解决方案
试试这个让你开始
ggplot()+
geom_point(aes(x,y,color =actual),data = df。实际)+
geom_line(aes(x,y,color =approximate),data = df.approx)+
scale_colour_discrete(Type)
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.
解决方案
Try this to get you started
ggplot() +
geom_point(aes(x, y, colour = "actual"), data = df.actual) +
geom_line(aes(x, y, colour = "approximate"), data = df.approx) +
scale_colour_discrete("Type")
这篇关于在绘制来自两个data.frame的数据时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文