df <- data.frame (Categories=c("Alpha Category", "Alpha Category",
"Alpha Category", "Bravo Category",
"Bravo Category", "Bravo Category",
"Charlie Category", "Charlie Category",
"Charlie Category"),
choices=c("alpha1", "alpha2", "alpha3", "bravo1",
"bravo2", "bravo3", "charlie1", "charlie2",
"charlie3") ,
ratings=c(20,60,40, 55,75,25,65,35,45))
df.plot <- ggplot(df, aes(Categories, ratings, fill = choices))
+ geom_bar(position="dodge", stat="identity")
+ coord_flip()
df.plot <- df.plot
+ theme_classic(base_size = 16, base_family = "")
+ scale_fill_brewer(palette="Paired")
df.plot <- df.plot
+ scale_y_continuous(breaks=seq(0,100,by=10),limits=c(0,80) )
+ ylab("Ratings")
+ theme(axis.text.y = element_text(size=16)) #change font size of y axis label
df.plot
最重要的是,我想按其“评级”的降序显示每个“类别”中的“选择”,例如这里的“查理类别”将显示 charlie1,然后是 charlie3,然后是 charlie2。
老实说,我在网上寻找解决方案大约一个星期,但找不到。我目前的想法是我应该将选择转换为因素,但我还没有弄清楚如何正确地做到这一点。
次要的,如果可以从上到下列出“类别”,“Alpha 类别”、“Bravo 类别”、“Charlie 类别”,而不是像翻转坐标时出现的相反顺序,那就太好了