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
409 views
in Technique[技术] by (71.8m points)

r - Create line graph with ggplot2, using time periods as x-variable

I am very new to R, and so this question is extremely elementary, but I can't solve it myself. I would very much appreciate your help.

This is a sort of dataframe I want to use:

     Period                           Value   Cut.off
1   January 1998 - August 2002      8.798129    1.64
2   September 2002 - Jun 2006       4.267268    1.64
3   Jul 2006 - Dec 2009             7.280275    1.64

This the code I am using:

require(ggplot2)
bq <- ggplot(data=glomor, aes(x=as.character(Period),y=Value))+geom_point()+ylim(0,10)

bq <- bq + scale_x_discrete(limits=c("January 1998 - August 2002","September 2002 - Jun 2006","Jul 2006 - Dec 2009"))

bq + geom_line()

I receive the following error message:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

How do I need to change the code, so that the points will be connected by a line?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should add group=1 in your aes() call to conect points with line. This will inform geom_line() that all your points belong to one level and they should be connected.

ggplot(data=glomor, aes(x=as.character(Period),y=Value,group=1))+
   geom_point()+ylim(0,10) + geom_line()

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

...