Ordering of faceted stacked barplot with ggplot2

a 夏天 提交于 2021-01-27 12:43:41

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/57845331/ordering-of-faceted-stacked-barplot-with-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!