如何以数值向量的降序在ggplot2中显示条形图? [英] How to show bars in ggplot2 in descending order of a numeric vector?

查看:245
本文介绍了如何以数值向量的降序在ggplot2中显示条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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,rating,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))#改变y轴标签的字体大小
df.plot

我会非常感谢帮助

最重要的是,我想按照他们的收视率降序显示每个类别中的选项,例如查理类别会显示charlie1,然后是charlie3,然后是charlie2。



我已经诚实地在线寻找解决方案大概一周,但找不到它。我目前的想法是,我应该把选择转换成因素,但我还没有想出如何正确地做到这一点。



次要的是,如果可以从上到下列出类别,Alpha类别,Bravo类别,查理类别,而不是按照与翻转坐标时相反的顺序排列。

ggplot 中的可能性来转换变量和比例(请参阅@ Metric的干净答案),而是事先转换变量。



在每个类别中,根据评分对选项进行重新排序。检查选项是字符。如果它是一个因子,您应该将 as.character 转换为字符,因为重新排序因子作为输入不给我们想要的东西(见下文)。

  str(df $ choices)
#chr [1:9]alpha1alpha2alpha3 ...

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

'类别'的反向等级

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


  df.plot < -  ggplot(df,aes(x = Categories,y = ratings,fill = choices))+ 
geom_bar(position = $ d









$ b $ $ b 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 $ b

当选择是一个字符时,在 ddply 中生成和重新排序的因子水平基于'choices'的每个子集。哪些工作正常。另一方面,当选择是原始数据中的一个因素时,其水平基于数据中存在的所有水平。然后在 ddply 子集中的选择级别进行重新排序,但重新排序发生在整个级别集合内。这导致三组相互冲突的等级,只有第一个被使用。

 #重新排序字符版本
ll< - dlply(.data = df,.variables =。(Categories) ,mutate,
choices.ro = reorder(options,ratings))

#检查级别
lapply(ll,function(x)levels(x $ choices.ro))
#$`Alpha Category`
#[1]alpha1alpha3alpha2

#$`Bravo Category`
#[1] bravo3bravo1bravo2

#$`Charlie Category`
#[1]charlie2charlie3charlie1


#选择作为因素
df $ choices.fac< - as.factor(df $ choices)
levels(df $ choices.fac)
#[1]alpha1 alpha2alpha3bravo1bravo2bravo3charlie1charlie2
#[9]charlie3

#重新排序因子版本
ll< ; - dlply(.data = df,.variables =。(Categories),mutate,
choices.fac.ro = reorder(choices.fac,ratings))

#reordering发生_within_每个类别,但在_full set_ of levels
#$`Alpha Category`
#[1] alpha1alpha3alpha2bravo1bravo2bravo3charlie1charlie2
#[9]charlie3
#这组等级将用于ggplot if你首先选择一个因素。
#因此@Michael Bellhouse评论:alpha类别排名,但不是布拉沃或查理

#$`Bravo Category`
#[1]bravo3bravo1 bravo2alpha1alpha2alpha3charlie1charlie2
#[9]charlie3

#$`Charlie Category`
# 1]charlie2charlie3charlie1alpha1alpha2alpha3bravo1bravo2
#[9]bravo3

#因为只有一个因子可以有一组等级,
#使用第一组 - $`Alpha Category`
#因此,仅在类别Alpha中进行重新组织。


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

I would really appreciate some help

Most importantly, I would like to show the "choices" within each "category" in descending order of their "ratings", for example here "Charlie Category" would show charlie1, then charlie3, then charlie2.

I have honestly looked online for solutions for about a week but can't find it. My current thoughts is that I should convert the choices into factors but I haven't figured out how to do this properly.

Of secondary importance, it would be great if the "categories" could be listed, from the top down, "Alpha Category", "Bravo category", "Charlie Category" rather than in the inverse order as seems to occur when coordinates are flipped

解决方案

This answer does not make use of the possibilities in ggplot to transform variables and scales (see @Metric's clean answer), but instead variables are transformed in beforehand.

Within each Category, reorder choices based on ratings. Check that 'choices' is a character. If it is a factor, you should convert to character with as.character, because reordering with a factor as input does not give us what we want (see below).

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

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

Reverse levels of 'Categories'

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

Plot

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

Edit following a comment from @Michael Bellhouse - "it appears alpha category is ranked but not bravo or charlie"

When 'choices' is a character, the factor levels that are generated and reordered in ddply is based on each subset of 'choices'. Which works fine. On the other hand, when 'choices' is a factor in the original data, its levels are based on all levels present in the data. In ddply subset of 'choice' levels are then reordered, but the reordering takes place within the full set of levels. This leads to three sets of conflicting levels and only the first is used.

# 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.

这篇关于如何以数值向量的降序在ggplot2中显示条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆