Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

r - Combine Points with lines with ggplot2

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

enter image description here

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:

enter image description here

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

Any suggestions? Thanks for y'all help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...