简体   繁体   English

如何按 ggplot2/R 中填充类别之一的值对堆积条形图 x 类别进行排序

[英]How to order stacked bar plot x categories by value of one of the fill categories in ggplot2/R

I'm trying to plot my data as a stacked bar with 3 levels ("catg"), but I want the categories on the X-axis to appeared in increasing order by their value of the "low" sub-categories,我试图将我的数据绘制为具有 3 个级别(“catg”)的堆叠条形图,但我希望 X 轴上的类别按“低”子类别的值按递增顺序出现,

here is reproductive example:这是生殖示例:

#creating df:
set.seed(33)
df<-data.frame(value=runif(12),
               catg=factor(rep(c("high","medium","low")),
                           levels = c("high","medium","low")),
               var_name=(c(rep("question1",3),rep("question2",3),rep("question3",3),rep("question4",3)))
#plotting
bar_dist<-ggplot(df,aes(x=var_name,
                              y=value, 
                              fill=catg, 
                             label=round(value,2)))

bar_dist+ geom_bar(stat = "identity",
                   position = "dodge", 
                   width = 0.7)+
  coord_flip() +
  xlab("questions")+
  ylab("y")+
  geom_text(size = 4,position=position_dodge(width = 0.7))

And here is my current plot:这是我目前的情节:

在此处输入图片说明

so in this case I should have question3 and then 4, 1, and finally 2. every help will be appreciate,所以在这种情况下,我应该有问题 3,然后是 4、1,最后是 2。我们将不胜感激,

A solution that doesn't modify the data frame, using fct_reorder2() from the forcats library:使用forcats库中的fct_reorder2()不修改数据框的解决方案:

library(forcats)

bar_dist <- ggplot(df,
                   aes(
                     x = fct_reorder2(var_name, catg, value),
                     y = value, fill = catg, 
                     label = round(value, 2)))

bar_dist + geom_bar(stat = "identity",
                   position = "dodge", 
                   width = 0.7) +
  coord_flip() +
  xlab("questions") +
  ylab("y") +
  geom_text(size = 4, position = position_dodge(width = 0.7))

分组条形图,列按跨组的一个变量的值排序

One way could be :一种方法可能是:

df$var_name=factor(df$var_name,levels=rev(levels(reorder(df[df$catg=="low",]$var_name,df[df$catg=="low",]$value))))

It uses reorder() as suggested by Richard Telford to reorder the levels according to df$value after filtering df to keep only the "low" .它按照 Richard Telford 的建议使用reorder()在过滤df以仅保留"low"后根据df$value重新排序级别。
levels() is used to extract the levels from the previous function. levels()用于从前一个函数中提取级别。
rev() is used to reverse the levels. rev()用于反转级别。
factor() reassigns the levels to df$var_name factor()将级别重新分配给df$var_name

or :或者 :

df$var_name=factor(df$var_name,levels = df[with(df,order(value,decreasing = T)) ,][df[with(df,order(value,decreasing = T)) ,]$catg=="low",]$var_name)

It sorts df on df$value (by decreasing value), filters on df$catg for "low" and retrieves df$var_name which is used as levels in factor() .它排序dfdf$value (通过降低值),在过滤器df$catg"low" ,并检索df$var_name其用作水平factor()

The same plotting function is then used:然后使用相同的绘图函数:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM