简体   繁体   中英

Facet_wrap: Y scale from 0 to 1, based on variable plotted

Is there a way to scale the y axis in facet wrap from 0 to 1, based on the variable plotted, in my case (see below) the gender? As if the ..count.. / max(..count..) would be grouped on the gender variable. Any ideas would be greatly appreciated.

Below is my current attempt

ex[c("var1","var2", "gender")] %>%
  gather(-gender, key="var", value = "value") %>% 
  ggplot(aes(value, fill = gender)) +  geom_histogram(aes(y=..count.. / max(..count..)), stat="count") + 
  facet_wrap(~var + gender, scales = "free_x", ncol= 2)  +
  ylab("% in each group") 

Resulting in在此处输入图片说明

Sample of the data:

structure(list(row = 1:100, var1 = c(" <25", " <25", " 25-50", 
" 25-50", " 50-75", " <25", " 25-50", " 25-50", " 25-50", " 25-50", 
" 50-75", " 25-50", " 25-50", " 25-50", " <25", " 25-50", " 25-50", 
" 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", 
">75", " 25-50", " 25-50", " <25", " <25", " 25-50", " 25-50", 
" 25-50", " 50-75", " 50-75", " 25-50", " 25-50", " 50-75", " 25-50", 
" 25-50", " <25", " 25-50", " 25-50", " 25-50", " 25-50", " <25", 
" 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", 
" 25-50", " 50-75", " 50-75", " <25", " 25-50", " <25", " 50-75", 
" <25", " <25", " <25", " 25-50", " <25", " <25", " 25-50", " 50-75", 
" 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " <25", " 25-50", 
" <25", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " <25", 
" 50-75", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 50-75", 
" 25-50", " <25", " 25-50", " 25-50", " <25", " 25-50", " <25", 
" <25", " 25-50", " 25-50", " <25", " <25"), var2 = c(0L, 0L, 
0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), gender = c("M", "M", "M", "M", "M", "M", "M", "F", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "F", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "F", "M", "M", "M", "M", "M", "M", "M", "M", 
"F", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M"
)), class = "data.frame", row.names = c(NA, -100L))

To the best of my knowledge there is no out-of-the-box solution to tell facet_wrap to do the kind of normalization you are trying to achieve. Instead you have to do this manually.

There are (at least) two approaches to achieve this:

  1. The easy approach would be to pre-compute the (normalized) counts and to make use of geom_col
  2. The more elaborate approach would be to use eg tapply to compute the maximum counts by gender . For this approach I would recommend to switch to geom_bar instead of using geom_histogram .

Both approaches are shown below:

library(ggplot2)
library(tidyr)
library(dplyr)

ex[c("var1","var2", "gender")] %>%
  gather(-gender, key="var", value = "value") %>% 
  count(gender, var, value) %>% 
  group_by(gender) %>% 
  mutate(pct = n / max(n)) %>% 
  ggplot(aes(value, pct, fill = gender)) +  
  geom_col() +
  facet_wrap(~var + gender, scales = "free_x", ncol= 2)  +
  ylab("% in each group") 

ex[c("var1","var2", "gender")] %>%
  gather(-gender, key="var", value = "value") %>% 
  ggplot(aes(value, fill = gender)) +  
  geom_bar(aes(y = ..count.. / tapply(..count.., ..fill.., function(x) max(x))[..fill..]), stat="count") + 
  facet_wrap(~var + gender, scales = "free_x", ncol= 2)  +
  ylab("% in each group")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM