2015-02-24 129 views
1

我不是R的專家,但我多次使用ggplot2,從來沒有任何問題。不過,這一次我不能在我的圖表中畫出線條,我不知道爲什麼(它應該是非常簡單的東西)。ggplot2中我的geom_line()發生了什麼?

例如爲:

def.percent period 
1 5.0657339 1984-1985 
2 3.9164528 1985-1986 
3 -1.756613 1986-1987 
4 2.8184863 1987-1988 
5 -2.606311 1988-1989 

我不得不代碼:

ggplot(plot.tab, aes(x=period, y=def.percent)) + geom_line() + geom_point() + ggtitle("Deforestation rates within Properties") 

但是當我運行它,它只是繪製的點,而一條線。它也給我這個消息:

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

它不是一個真正的錯誤,但我無法弄清楚如何繪製線......任何想法?

+0

上之後,我添加了一個新列「posite」和「負」標籤「def.percent」,如:plot.tab $ valence < - NA /// plot.tab $ valence [plot.tab $ def.percent> = 0] < - 「pos」/// plot.tab $ valence [ plot.tab $ def.percent <0] < - 「neg」///然後我嘗試使用:geom_area(aes(fill = valence))+ ///來獲得彩色線條下的區域,但我一直得到這個錯誤:「美學不能隨着絲帶而變化」但是,當我嘗試使用其他數據集時,它的工作原理......這裏有什麼問題? – 2015-02-25 21:06:55

回答

1

您的x軸(period)是一個因子而不是數字,因此它不會連接它們。您可以通過在美學,它告訴GGPLOT2將它們全部組合在一起成爲一個單一的線路設置group = 1解決這個問題:

ggplot(plot.tab, aes(x = period, y = def.percent, group = 1)) + 
    geom_line() + 
    geom_point() + 
    ggtitle("Deforestation rates within Properties") 
+0

接下來,我爲「def.percent」添加了一個帶有「posite」和「negative」標籤的新列,如: plot.tab $ valence < - NA /// plot.tab $ valence [plot。 tab $ def.percent> = 0] < - 「pos」/// plot.tab $ valence [plot.tab $ def.percent <0] < - 「neg」/// 然後我嘗試使用: geom_area(aes(fill = valence))+ /// 以獲得彩色線條下的區域,但我不斷收到此錯誤:「美學不能隨着功能區變化」 但是,當我嘗試使用其他數據集時, ...這裏有什麼問題? – 2015-02-25 21:01:42