2013-03-04 70 views
1

我對R很新,所以這個問題是非常基本的,但我無法自己解決。我非常感謝你的幫助。使用ggplot2創建線條圖,使用時間段作爲x變量

這是一種數據幀的我想用:

 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? 

我該如何更改代碼,以便將點連接起來?

回答

4

你應該在你的aes()調用中添加group=1以直線連接點。這將通知geom_line()所有的點屬於一個級別,他們應該連接。

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

謝謝你的回答......幫了很多! – user1738753 2013-03-05 14:55:01