使用ggplot2订购多面堆叠barplot [英] Ordering of faceted stacked barplot with ggplot2

查看:58
本文介绍了使用ggplot2订购多面堆叠barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的数据集,我试图得到一个有序的多面堆积条形图.我已经在SO上查看了一些答案,特别是

任何有关解决此问题的建议将不胜感激

使用聚合函数 sum时,使用 reorder

解决方案

似乎并不尊重 facet_wrap .使用 forcats :: fct_reorder()对我有用:

  reg_plot<-ggplot(数据,aes(x = forcats :: fct_reorder(reg_l,share,.fun = sum),y = share,group = org))+coord_flip()+facet_wrap(level〜.,ncol = 1,scales ="free")+geom_bar(stat ="identity",aes(fill = org),colour = NA,width = 0.75)+scale_x_discrete(breaks = data $ reg_l,标签= data $ reg)+xlab("\ n")+ylab("\ n")+scale_y_continuous(极限= c(0,100))+ggtitle("\ n")reg_plot 

I have a dataset like the below and am trying to get an ordered faceted stacked barplot. I have looked at some answers on SO particularly this one to come up with my plot but am not sure why this does not work on one particular bar of my stacked barplot

structure(list(reg = c("J", "J", "J", "J", "J", "J", "J", "J", 
"KA", "KA", "KA", "KA", "KA", "KA", "KA", "KA", "KA", "KA", "RI", 
"RI", "RI", "RI", "SU", "SU", "SU", "SU", "SU", "SU", "SA", "SA", 
"SA", "SA", "SA", "SA", "SA", "SA", "SA", "SA", "SA", "SA", "SA", 
"SA", "SA", "SA"), org = structure(c(1L, 1L, 3L, 3L, 4L, 4L, 
2L, 2L, 1L, 1L, 3L, 3L, 4L, 4L, 2L, 2L, 8L, 8L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 2L, 5L, 5L, 1L, 1L, 3L, 3L, 4L, 4L, 2L, 2L, 6L, 6L, 
5L, 8L, 7L, 7L, 9L, 9L), .Label = c("WR", "MS", "SM", "AA", "AAL", 
"PH", "P3", "SD", "HS"), class = "factor"), level = structure(c(2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L), .Label = c("E", 
"R"), class = "factor"), share = c(6.94, 1.51, 3.01, 0.42, 1.58, 
0.24, 2.85, 0.19, 4.7, 0.5, 2.16, 5.32, 1.08, 1.26, 0.82, 1.11, 
1.8, 1.73, 2, 3.52, 5, 2, 1.29, 0.38, 0.72, 0.46, 0.44, 2.27, 
15.79, 13.74, 5.12, 16.21, 6.54, 11.16, 8.19, 10.91, 4.71, 5.32, 
0.45, 0.14, 1.03, 0.33, 5.04, 3.03), reg_l = c("J_R", "J_E", 
"J_R", "J_E", "J_R", "J_E", "J_R", "J_E", "KA_R", "KA_E", "KA_R", 
"KA_E", "KA_R", "KA_E", "KA_R", "KA_E", "KA_R", "KA_E", "RI_R", 
"RI_E", "RI_R", "RI_E", "SU_R", "SU_E", "SU_R", "SU_E", "SU_R", 
"SU_E", "SA_R", "SA_E", "SA_R", "SA_E", "SA_R", "SA_E", "SA_R", 
"SA_E", "SA_R", "SA_E", "SA_E", "SA_E", "SA_R", "SA_E", "SA_R", 
"SA_E")), row.names = c(NA, -44L), class = c("tbl_df", "tbl", 
"data.frame"))

and here is my code right now

data$level <- as.factor(as.character(data$level))
data$reg_l <- with(data, paste(reg,level,sep = "_"))

data$org <- factor(data$org,levels=c("WR","MS","SM","AA","AAL","PH","P3","SD","HS"))

# plot
reg_plot <- ggplot(data, aes(x = reorder(reg_l,share),y = share,group=org)) +
  coord_flip()+
  facet_wrap(level~.,ncol = 1,scales="free") +
  geom_bar(stat="identity",aes(fill=org),colour=NA,width=0.75) +
  scale_x_discrete(breaks = data$reg_l, labels = data$reg) +
  xlab("\n") + 
  ylab("\n") +
  scale_y_continuous(limits = c(0,100)) +
  ggtitle("\n")

reg_plot

The image below is the plot I am getting now but some reason the RI bar does not seem to follow the order of high to low values

Any suggestions on how to fix this would be highly appreciated

解决方案

using reorder doesn't seem to respect the facet_wrap when using the aggregation function sum. Using forcats::fct_reorder() worked for me:

reg_plot <- ggplot(data, aes(x = forcats::fct_reorder(reg_l, share, .fun = sum),y = share,group=org)) +
  coord_flip()+
  facet_wrap(level~.,ncol = 1,scales="free") +
  geom_bar(stat="identity",aes(fill=org),colour=NA,width=0.75) +
  scale_x_discrete(breaks = data$reg_l, labels = data$reg) +
  xlab("\n") + 
  ylab("\n") +
  scale_y_continuous(limits = c(0,100)) +
  ggtitle("\n")

reg_plot

这篇关于使用ggplot2订购多面堆叠barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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