2016-09-28 69 views
1

的比例條形圖DF2R中如何繪製,表示長時間的數據

Meal       Contents apple banana beef berry blackberry cantaloupe chicken mashpotatoes redberries rice strawberry stringbeans 

1 Snack_1 redberries,strawberry,blackberry  0  0 0  0    1   0  0   0   1 0   1   0 
2 Snack_2   banana,apple,strawberry,  1  1 0  0   0   0  0   0   0 0   1   0 
3 Snack_3      rice,chicken  0  0 0  0   0   0  1   0   0 1   0   0 
4 Snack_4  beef,stringbeans,mashpotatoes  0  0 1  0   0   0  0   1   0 0   0   1 
5 Snack_5 banana,strawberry,berry,cantaloupe  0  1 0  1   0   1  0 

我運行一個函數,找出頂部成分的零食

topn_v1 <- names(sort(colSums(df2[3:ncol(df2)]), decreasing=TRUE))[1:n] 

所以導致一些像這樣:

 Berry   10 
    Strawberry  200 
    Cantalopue  30 

當我試圖繪製這個值時,它會在圖上產生一個點。 但我希望它能顯示條形圖來顯示每種原料如何相互公平。這是可能的R中

+0

你可以試試'geom_bar'從'ggplot2'包('庫(GGPLOT2)'),用'統計=「身份」'。假設上面的結果數據框被稱爲'df',列'Ingredient'和'Quantity',我們可以做'ggplot(df,aes(x = factor(Ingredient),y = Quantity))+ geom_bar(stat =「身份「)'。參考:http://docs.ggplot2.org/0.9.3.1/geom_bar.html – jav

回答

0

隨着基礎R & GGPLOT2:

df 
    Fruit Count 
1  Berry 10 
2 Strawberry 200 
3 Cantalopue 30 

barplot(df$Count, names.arg=df$Fruit, ylab='Count') 

library(ggplot2) 
ggplot(df, aes(Fruit, Count)) + geom_bar(stat='identity') 

enter image description here