13
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 类别”,而不是像翻转坐标时出现的相反顺序,那就太好了

4

2 回答 2

14
library(ggplot2)
df.plot <- ggplot(df, aes(x=Categories,y=reorder(choices,ratings), fill = choices)) +
  geom_bar(position = "dodge", stat = "identity") + coord_flip() +
  scale_x_discrete(limits = rev(levels(df$Categories)))

在此处输入图像描述

于 2013-09-15T19:18:23.557 回答
6

这个答案没有利用ggplot转换变量和尺度的可能性(请参阅@Metric 的干净答案),而是预先转换变量。

在每个类别中,根据评级重新排序选择。检查“选择”是否为character. 如果它是factor,你应该用 转换成字符as.character,因为用一个因子作为输入重新排序并不能得到我们想要的(见下文)。

str(df$choices)
# chr [1:9] "alpha1" "alpha2" "alpha3" ...

library(plyr)
df <- ddply(.data = df, .variables = .(Categories), mutate,
            choices = reorder(choices, ratings))

“类别”的反向级别

df$Categories <- as.factor(df$Categories)
levels(df$Categories) <- rev(levels(df$Categories))

阴谋

df.plot <- ggplot(df, aes(x = Categories, y = ratings, fill = choices)) +
  geom_bar(position = "dodge", stat = "identity") +
  coord_flip() +
  theme_classic(base_size = 16, base_family = "") +
  scale_fill_brewer(palette = "Paired") +
  scale_y_continuous(breaks = seq(0, 100, by = 10), limits = c(0, 80)) +
  ylab("Ratings")  +
  theme(axis.text.y = element_text(size = 16))   

df.plot

在此处输入图像描述

根据@Michael Bellhouse 的评论进行编辑 - “似乎 alpha 类别已排名但不是 bravo 或 charlie”

当“选择”是一个字符时,生成和重新排序的因子级别ddply基于“选择”的每个子集。哪个工作正常。另一方面,当“选择”是原始数据中的一个因素时,其级别基于数据中存在的所有级别。然后在ddply“选择”级别的子集中重新排序,但重新排序发生在完整的级别集中。这会导致三组相互冲突的级别,并且只使用第一组。

# reorder character version
ll <- dlply(.data = df, .variables = .(Categories), mutate,
            choices.ro = reorder(choices, ratings))

# check levels
lapply(ll, function(x) levels(x$choices.ro))
# $`Alpha Category`
# [1] "alpha1" "alpha3" "alpha2"
# 
# $`Bravo Category`
# [1] "bravo3" "bravo1" "bravo2"
# 
# $`Charlie Category`
# [1] "charlie2" "charlie3" "charlie1"


# choices as factor
df$choices.fac <- as.factor(df$choices)
levels(df$choices.fac)
# [1] "alpha1"   "alpha2"   "alpha3"   "bravo1"   "bravo2"   "bravo3"   "charlie1" "charlie2"
# [9] "charlie3"

# reorder factor version
ll <- dlply(.data = df, .variables = .(Categories), mutate,
            choices.fac.ro = reorder(choices.fac, ratings))

# reordering takes place _within_ each Category, but on the _full set_ of levels
# $`Alpha Category`
# [1] "alpha1"   "alpha3"   "alpha2"   "bravo1"   "bravo2"   "bravo3"   "charlie1" "charlie2"
# [9] "charlie3"
# This set of levels will be used in ggplot if you start with choices as a factor.
# Hence @Michael Bellhouse comment: "alpha category is ranked but not bravo or charlie"

# $`Bravo Category`
# [1] "bravo3"   "bravo1"   "bravo2"   "alpha1"   "alpha2"   "alpha3"   "charlie1" "charlie2"
# [9] "charlie3"
# 
# $`Charlie Category`
# [1] "charlie2" "charlie3" "charlie1" "alpha1"   "alpha2"   "alpha3"   "bravo1"   "bravo2"  
# [9] "bravo3"

# Because a factor only can have one set of levels,
# the first set is used - $`Alpha Category`
# Thus, relordered within category Alpha only.
于 2013-09-15T19:21:02.117 回答