【问题标题】:Percentage histogram with facet_wrap带有 facet_wrap 的百分比直方图
【发布时间】:2019-03-12 10:03:50
【问题描述】:

我正在尝试将百分比直方图与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) 

输出:

【问题讨论】:

    标签: r ggplot2 histogram facet-wrap


    【解决方案1】:

    尝试使用 y = stat(density)(或 ggplot2 版本 3.0.0 之前的 y = ..density..)而不是 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)
    

    来自“计算变量”下的?geom_histogram

    density : bin 中点的密度,缩放到积分为 1

    我们乘以 5(bin 宽度),因为 y 轴是密度(面积积分为 1),而不是百分比(高度总和为 1),请参阅 Hadley's comment(感谢 @MariuszSiatka)。

    【讨论】:

      【解决方案2】:

      虽然facet_wrap 似乎没有在每个子集中运行特殊的geom_histogram 百分比计算,但请考虑单独构建一个图列表,然后将它们网格排列在一起。

      具体来说,调用bygroup 的子集中运行你的ggplots,然后调用gridExtra::grid.arrange()(实际的封装方法)在某种程度上模仿facet_wrap

      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)
      


      但是,为了避免重复的 y 轴和 x 轴,请考虑在 by 调用中有条件地设置 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-14
        • 2016-02-10
        • 1970-01-01
        • 2019-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多