2012-05-31 56 views
1

我學習上的其他類似的問題,但似乎無法得到這個工作對我的數據。堆棧區域圖中的R

我瞄準了這一結果:

desired output

這是我的數據幀:

 
    Room Direc MB 
    Alley-10 Rx 1 
    Alley-11 Rx 7 
    Alley-12 Rx 11 
    Alley-10 Tx 23 
    Alley-11 Tx 17 
    Alley-12 Tx 20 

當我運行:

ggplot(tp, aes(x=Room,y=MB)) + geom_area(aes(fill=factor(Direc))) 

我得到這樣的結果:

not working

我怎樣才能得到這個工作?

回答

5

因爲Room變量被當作一個因素,因此不會作出任何意義有連接實線這是行不通的。

繪製:

ggplot(tp, aes(x=1:3, y=MB, fill=Direc)) + 
    geom_area() 

給出結果我想你期待。您可以添加:

ggplot(tp, aes(x=1:3, y=MB, fill=Direc)) + 
    geom_area() + 
    scale_x_discrete(labels=tp$Room) 

修復標籤。