代码之家  ›  专栏  ›  技术社区  ›  AAAA

百分比直方图

  •  5
  • AAAA  · 技术社区  · 7 年前

    我试着把百分比直方图和 facet_wrap ,但百分比不是基于组而是基于所有数据计算的。我想每个直方图显示一个群体的分布,而不是相对于所有人口。我知道有可能做几个情节,并结合他们与 multiplot .

    library(ggplot2)
    library(scales)
    library(dplyr)
    
    set.seed(1)
    df <- data.frame(age = runif(900, min = 10, max = 100),
                     group = rep(c("a", "b", "c", "d", "e", "f", "g", "h", "i"), 100))
    
    tmp <- df %>%
      mutate(group = "ALL")
    
    df <- rbind(df, tmp)
    
    ggplot(df, aes(age)) + 
      geom_histogram(aes(y = (..count..)/sum(..count..)), binwidth = 5) + 
      scale_y_continuous(labels = percent ) + 
      facet_wrap(~ group, ncol = 5) 
    

    output plot

    2 回复  |  直到 7 年前
        1
  •  8
  •   jbaums    5 年前

    尝试 y = stat(density) (或 y = ..density.. ggplot2 3.0.0版之前的版本),而不是 y = (..count..)/sum(..count..)

    ggplot(df, aes(age, group = group)) + 
      geom_histogram(aes(y = stat(density) * 5), binwidth = 5) + 
      scale_y_continuous(labels = percent ) +
      facet_wrap(~ group, ncol = 5)
    

    enter image description here

    ?geom_histogram 在“计算变量”下

    我们乘以5(箱子宽度),因为y轴是密度(面积积分为1),而不是百分比(高度总和为1),请参见 Hadley's comment

        2
  •  3
  •   Parfait    7 年前

    facet_wrap 不运行特殊 geom_histogram 在每个子集内的百分比计算中,考虑分别构建一个绘图列表,然后将它们排列在一起。

    by gridExtra::grid.arrange() 小平面U形包装

    library(ggplot2)
    library(scales)
    library(gridExtra)
    
    ...
    
    grp_plots <- by(df, df$group, function(sub){
      ggplot(sub, aes(age)) + 
        geom_histogram(aes(y = (..count..)/sum(..count..)), binwidth = 5) + 
        scale_y_continuous(labels = percent ) + ggtitle(sub$group[[1]]) +
        theme(plot.title = element_text(hjust = 0.5))
    })
    
    grid.arrange(grobs = grp_plots, ncol=5)
    

    Plot Output


    但是,为了避免重复的y轴和x轴,请考虑有条件地设置 theme 在内部 打电话,假设你提前了解了你的团队,而且他们的人数相当少。

    grp_plots <- by(df, df$group, function(sub){
    
      # BASE GRAPH
      p <- ggplot(sub, aes(age)) + 
        geom_histogram(aes(y = (..count..)/sum(..count..)), binwidth = 5) + 
        scale_y_continuous(labels = percent ) + ggtitle(sub$group[[1]])
    
      # CONDITIONAL theme() CALLS
      if (sub$group[[1]] %in% c("a")) {
        p <- p + theme(plot.title = element_text(hjust = 0.5), axis.title.x = element_blank(), 
                      axis.text.x = element_blank(), axis.ticks.x = element_blank())
      }
      else if (sub$group[[1]] %in% c("f")) {
        p <- p + theme(plot.title = element_text(hjust = 0.5))
      }
      else if (sub$group[[1]] %in% c("b", "c", "d", "e")) {
        p <- p + theme(plot.title = element_text(hjust = 0.5), axis.title.y = element_blank(), 
                       axis.text.y = element_blank(), axis.ticks.y = element_blank(),
                       axis.title.x = element_blank(), axis.text.x = element_blank(), 
                       axis.ticks.x = element_blank())
      }
      else {
        p <- p + theme(plot.title = element_text(hjust = 0.5), axis.title.y = element_blank(), 
                       axis.text.y = element_blank(), axis.ticks.y = element_blank())
      }
      return(p)
    })
    
    grid.arrange(grobs=grp_plots, ncol=5)
    

    Plot Output

    推荐文章