用ggplot2结合点与线 [英] Combine Points with lines with ggplot2

查看:158
本文介绍了用ggplot2结合点与线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个如下所示的时间系列:




 

code> qplot(Jahr,Wert,data = tu,group = Geschlecht,color = Altersgr)+ facet_grid(Geschlecht〜Land)

我的数据如下所示:

  Land Altersgr Geschlecht Jahr Wert 
1 DE < 20 J.m 2000 13.0
2 DE 20-< 65 J.m 2000 25.7
3 DE 65+ J.m 2000 70.1
4 DE 65-< 80 J.m 2000 44.2
5 DE 80+ J.m 2000 213.5
6 BB< 20 J. m 2000 26.8

目前一切都很好。但我必须用线连接相应的点(相同的颜色)。我无法弄清楚如何做到这一点。如果我使用geom_line(),我得到了这个结果:



这不是我想要的......我只是觉得我忽略了一些东西......



有什么建议吗?感谢您的帮助。 您可能会发现使用'group'aes可以帮助您获得想要的结果。例如:

  tu < -  expand.grid(Land = gl(2,1,labels = c(DE ,BB)),
Altersgr = gl(5,1,labels = letters [1:5]),
Geschlecht = gl(2,1,labels = c('m','


set.seed(42)
tu $ Wert < - unclass(tu $ Altersgr)* 200 + rnorm($ w $)),
Jahr = 2000: (0,1,2,0,0,10)

ggplot(tu,aes(x = Jahr,y = Wert,color = Altersgr,group = Altersgr))+
geom_point()+ geom_line()+
facet_grid(Geschlecht〜Land)

产生此处的图表:




I would like to plot a time series that look like this:

what I plot with:

qplot(Jahr, Wert, data=tu, group = Geschlecht, color = Altersgr) + facet_grid(Geschlecht ~ Land)

My data looks like this:

  Land   Altersgr Geschlecht Jahr  Wert
1   DE    < 20 J.          m 2000  13.0
2   DE  20-<65 J.          m 2000  25.7
3   DE     65+ J.          m 2000  70.1
4   DE  65-<80 J.          m 2000  44.2
5   DE     80+ J.          m 2000 213.5
6   BB    < 20 J.          m 2000  26.8

Everything is fine so far. But I have to connect the corresponding points (same color) with a line. I couldn't figure out how to do that. If I use geom_line() I got this result:

Which is not what I want... I just have the feeling I overlook something...

Any suggestions? Thanks for y'all help.

解决方案

You may find that using the `group' aes will help you get the result you want. For example:

tu <- expand.grid(Land       = gl(2, 1, labels = c("DE", "BB")),
                  Altersgr   = gl(5, 1, labels = letters[1:5]),
                  Geschlecht = gl(2, 1, labels = c('m', 'w')),
                  Jahr       = 2000:2009)

set.seed(42)
tu$Wert <- unclass(tu$Altersgr) * 200 + rnorm(200, 0, 10)

ggplot(tu, aes(x = Jahr, y = Wert, color = Altersgr, group = Altersgr)) + 
  geom_point() + geom_line() + 
  facet_grid(Geschlecht ~ Land)

Which produces the plot found here:

这篇关于用ggplot2结合点与线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆