facet_wrap的百分比直方图 [英] Percentage histogram with facet_wrap

查看:74
本文介绍了facet_wrap的百分比直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将百分比直方图与facet_wrap组合在一起,但是百分比不是基于组而是根据所有数据来计算的.我希望每个直方图都显示一个组中的分布,而不是相对于所有人群.我知道可以绘制多个图并将它们与multiplot组合.

I am trying to combine percentage histogram with facet_wrap, but the percentages are not calculated based on group but all data. I would like each histogram to show distribution in a group, not relative to all population. I know it is possible to do several plots and combine them with 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) 

输出:

推荐答案

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

Try with y = stat(density) (or y = ..density.. prior to ggplot2 version 3.0.0) instead of 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在计算变量"下

密度:bin中点的密度,按比例缩放为1

density : density of points in bin, scaled to integrate to 1

我们将其乘以5(箱宽),因为y轴是密度(面积积分为1),而不是百分比(高度之和为1),请参见

We multiply by 5 (the bin width) because the y-axis is a density (the area integrates to 1), not a percentage (the heights sum to 1), see Hadley's comment (thanks to @MariuszSiatka).

这篇关于facet_wrap的百分比直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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