2016-09-28 71 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