如何使用 ggplot 和 brewer 包绘制包含计数的数据集?
How to plot a dataset containing counts using ggplot and brewer package?
以下是我的数据集
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 中提到的颜色代码的输出。相反,我得到了普通的灰色条形图。我不确定是什么问题。请帮忙
您需要在 geom
的 aes
中添加 fill
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")
结果是
但是,通过添加:
scale_fill_brewer(palette = "Set1") +
结果乱七八糟。并且有些列不存在。
以下是我的数据集
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 中提到的颜色代码的输出。相反,我得到了普通的灰色条形图。我不确定是什么问题。请帮忙
您需要在 geom
aes
中添加 fill
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")
结果是
但是,通过添加:
scale_fill_brewer(palette = "Set1") +
结果乱七八糟。并且有些列不存在。