简体   繁体   English

R ggplot 根据其长度在组内排序条

[英]R ggplot ordering bars within groups according to their lenght

This is my CSV file:这是我的 CSV 文件:

Number,inst,pour,Type
One Smell,450,66.96%,OO smells
One Smell,237,36.18%,Both
One Smell,255,49.71%,Android-specific smells
Two Smells,160,16.22%,OO smells
Two Smells,143,21.83%,Both
Two Smells,109,31.19%,Android-specific smells
Three Smells,109,16.64%,Both
Three Smells,61,7.29%,OO smells
Three Smells,49,11.89%,Android-specific smells
Four Smells,66,10.08%,Both
Four Smells,33,6.24%,Android-specific smells
Four Smells,32,4.91%,OO smells
Five Smells,35,5.34%,Both
Five Smells,24,3.57%,OO smells
Five Smells,4,0.78%,Android-specific smells
Six Smells,22,3.36%,Both
Six Smells,7,1.04%,OO smells
Six Smells,1,0.19%,Android-specific smells
Seven smells,17,2.60%,Both
Eight smells,14,2.14%,Both
Nine Smells,9,1.37%,Both
Ten smells,2,0.31%,Both
Eleven smells,1,0.15%,Both

I'm grouping according to Type (Android-specific smell, OO smell, Both) and here is the result:我根据类型(Android 特定气味、OO 气味、两者)进行分组,结果如下:

在此处输入图像描述

This is the code:这是代码:

library(ggplot2)
theme_set(theme_classic())

# Plot

g <- ggplot(Co.oooa, aes(x=reorder(Number, inst), y=inst, fill=Type))+


  theme(axis.text.x = element_text(angle=0, vjust=0.67, size = 10), axis.text.y  = element_text(angle=0, vjust=0.6, size=10))
g + geom_bar(stat="identity", width = 0.57, position ="dodge" )+  

  geom_text(aes(label=pour, group=Type), position=position_dodge(width=0.67), hjust=0.0001, size=2.5 )+

  scale_fill_manual(values=c( "gray", "royalblue1", "red"))+

  coord_flip()

I need to re-order bars according to their length.我需要根据它们的长度重新排序酒吧。 for example for one smell the first bar should be OO smells then Android-specific smells and finally Both .例如,对于一个气味,第一个栏应该是OO smells ,然后是Android-specific smells ,最后是Both

I had the same problem and combined two posts to solve it.我有同样的问题,并结合两个帖子来解决它。

  • See this one to order fill-categories within x-categories;请参阅以在 x 类别中订购填充类别;
  • And this one to order x-categories.这个要订购 x 类别。 Note that they are MANY stackoverflow questions related to sorting bars in geom_bar() .请注意,它们是与geom_bar()中的排序条相关的许多 stackoverflow 问题。

It is possible to achieve this using a new ordered factor.可以使用新的有序因子来实现这一点。 Please find below a piece of code that creates the plot you want:请在下面找到一段代码,用于创建您想要的 plot:

Co.oooa = Co.oooa %>% 
  arrange(Number, inst) %>% 
  mutate(group_var = factor(paste(Number, Type), levels = paste(Number, Type)))

# 2nd: use aes(group = group_var) along with fct_reorder() to display your data correctly
ggplot(data = Co.oooa, 
       aes(x = fct_reorder(.f = Number, .x = inst, .fun = sum), 
           y = inst, 
           fill = Type,
           label = pour,
           group = group_var)) +
  geom_bar(stat = "identity",
           width = 0.57,
           position = "dodge") +
  geom_text(
    position = position_dodge(width = 0.67),
    hjust = 0.0001,
    size = 2.5
  ) +
  scale_fill_manual(values = c("gray", "royalblue1", "red")) +
  coord_flip() +
  theme_classic() + 
  theme(axis.text.x = element_text(angle=0, vjust=0.67, size = 10), axis.text.y  = element_text(angle=0, vjust=0.6, size=10))
)

剧情

dput() of the dataset:数据集的dput()

Co.oooa <-  structure(list(Number = c("One Smell", "One Smell", "One Smell", 
                                    "Two Smells", "Two Smells", "Two Smells", "Three Smells", "Three Smells", 
                                    "Three Smells", "Four Smells", "Four Smells", "Four Smells", 
                                    "Five Smells", "Five Smells", "Five Smells", "Six Smells", "Six Smells", 
                                    "Six Smells", "Seven smells", "Eight smells", "Nine Smells", 
                                    "Ten smells", "Eleven smells"), inst = c(450L, 237L, 255L, 160L, 
                                                                             143L, 109L, 109L, 61L, 49L, 66L, 33L, 32L, 35L, 24L, 4L, 22L, 
                                                                             7L, 1L, 17L, 14L, 9L, 2L, 1L), pour = c("66.96%", "36.18%", "49.71%", 
                                                                                                                     "16.22%", "21.83%", "31.19%", "16.64%", "7.29%", "11.89%", "10.08%", 
                                                                                                                     "6.24%", "4.91%", "5.34%", "3.57%", "0.78%", "3.36%", "1.04%", 
                                                                                                                     "0.19%", "2.60%", "2.14%", "1.37%", "0.31%", "0.15%"), Type = c("OO smells", 
                                                                                                                                                                                     "Both", "Android-specific smells", "OO smells", "Both", "Android-specific smells", 
                                                                                                                                                                                     "Both", "OO smells", "Android-specific smells", "Both", "Android-specific smells", 
                                                                                                                                                                                     "OO smells", "Both", "OO smells", "Android-specific smells", 
                                                                                                                                                                                     "Both", "OO smells", "Android-specific smells", "Both", "Both", 
                                                                                                                                                                                     "Both", "Both", "Both")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                   -23L))

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

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