如何使用ggplot和brewer包绘制包含计数的数据集?

问题描述 投票:2回答:1

以下是我的数据集

feature_user <- data.frame (Features = c("Battery","Build_Design", "Camera","Connectivity","Delivery", 
                                         "Design", "Display", "Gaming_Experience", "Performance", 
                                         "Post_Usage", "Protection", "Recommendation", "Sound", "Value_Money"), 
                            Users = c(852, 89, 471, 96, 228, 143, 131, 18, 379, 354, 35, 271, 99, 510))

我正在尝试使用以下代码绘制数据

library(ggplot2)

ggplot(feature_user, aes(x = reorder(Features, Users), y = Users)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set1") + 
  coord_flip() + 
  ggtitle("Most Talked Feature by Users") +
  ylab("No of Users") + 
  xlab("Features") 

但问题是,我没有按照scale_fill_brewer中提到的颜色代码获得输出。相反,我得到了简单的灰色条形图。我不确定是什么问题。请帮助

r ggplot2 bar-chart data-visualization colorbrewer
1个回答
1
投票

你需要在你的fillaes中添加geom

library(ggplot2)      

ggplot(feature_user,aes(x=reorder(Features,Users),y=Users)) + 
  geom_col(stat='identity', aes(fill=Features), width=.5)+
  coord_flip() +
  ggtitle("Most Talked Feature by Users") +ylab("No of Users") + xlab("Features")

结果是

enter image description here

但是,通过添加:

scale_fill_brewer(palette = "Set1") +

它与结果混淆了。并且某些列不存在。

© www.soinside.com 2019 - 2024. All rights reserved.