使用ggplot2创建折线图,使用时间段作为x变量

时间:2013-03-04 19:01:15

标签: r graph ggplot2 line

我是R的新手,所以这个问题非常基本,但我自己无法解决。我非常感谢你的帮助。

这是我想要使用的一种数据帧:

     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

这是我正在使用的代码:

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()

我收到以下错误消息:

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

我如何更改代码,以便通过一条线连接点?

1 个答案:

答案 0 :(得分:5)

您应该在group=1来电中添加aes()以使用线路连接点。这将告知 geom_line()您的所有积分属于一个级别,并且应该连接它们。

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