如何以数字向量的降序显示ggplot2中的条形图?

时间:2013-09-15 18:35:50

标签: r ggplot2 rank

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

我真的很感激一些帮助

最重要的是,我想按照“收视率”的降序显示每个“类别”中的“选择”,例如“Charlie Category”会显示charlie1,然后是charlie3,然后是charlie2。

我老老实实地在网上找了大约一个星期的解决方案,却找不到它。我目前的想法是,我应该将选择转换为因素,但我还没弄清楚如何正确地做到这一点。

次要的,如果可以列出“类别”,从上到下,“阿尔法类别”,“布拉沃类别”,“查理类别”而不是按照相反的顺序列出,那将是很好的坐标被翻转

2 个答案:

答案 0 :(得分:13)

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)))

enter image description here

答案 1 :(得分: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

enter image description here

根据@Michael Bellhouse的评论编辑 - “看起来alpha类别排名但不是bravo或charlie”

当'choices'是一个字符时,ddply中生成和重新排序的因子级别基于'choices'的每个子集。哪个工作正常。另一方面,当“选择”是原始数据中的一个因素时,其级别基于数据中存在的所有级别。然后在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.