代码之家  ›  专栏  ›  技术社区  ›  Funkeh-Monkeh

用ggplot2排序多面堆积条形图

  •  1
  • Funkeh-Monkeh  · 技术社区  · 5 年前

    我有一个像下面这样的数据集,我正试图得到一个有序的多面堆积条形图。我特别注意了一些答案 this 一个提出了我的计划,但不确定为什么这不适用于我的堆积条形图中的一个特定的条形图

    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"))
    

    这是我的密码

    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
    

    下面的图片是我现在得到的情节,但有些原因 RI 酒吧似乎不遵循 high low 价值观

    enter image description here

    如果您对如何解决这个问题有任何建议,我们将不胜感激。

    1 回复  |  直到 5 年前
        1
  •  1
  •   Chris    5 年前

    使用 reorder 似乎不尊重 facet_wrap 使用聚合函数时 sum . 使用 forcats::fct_reorder() 为我工作:

    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
    

    enter image description here