多面雨云图:如何使直方图y轴的每个面缩放相同?

时间:2019-04-21 21:58:10

标签: r ggplot2

我正在尝试创建多面雨云图。默认情况下,所有构面应具有相同的比例;但是,我发现直方图在构面内具有相同的y轴比例,而在构面之间具有不同的y轴比例。

group <- c(rep("treatment", 24), rep("control", 24))

a1 <- c(rep(0, 16), rep(.2, 2), rep(.4, 2), rep(.8, 2), rep(1,2), 
        #right skewed distribution in treatment
        rep(.01, 4), rep(.2, 4), rep(.4, 4), rep(.6, 4), rep(.8, 4), rep(1,4)) 
        # flat distribution in control

a2 <- c(rep(.01, 4), rep(.2, 4), rep(.4, 4), rep(.6, 4), rep(.8, 4), rep(1,4),
        # flat distribution in treatment,
        rep(.01, 4), rep(.2, 4), rep(.4, 4), rep(.6, 4), rep(.8, 4), rep(1,4)) 
        #  flat distribution in control

data <- data.frame(group, a1, a2)
data.long <- gather(data, condition, response, 2:3)

library(ggplot2)
library(tidyr)
source("https://gist.githubusercontent.com/benmarwick/2a1bb0133ff568cbe28d/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R")

ggplot(data=data.long,aes(x=group,y=response,fill=group))+
  facet_wrap(~condition,nrow=2)+
  geom_flat_violin(position=position_nudge(x=.15,y=0))+
  geom_point(aes(y=response),position=position_jitter(width=.13))+
  geom_boxplot(position=position_nudge(x=-.25,y=0),width=.1)+
  coord_flip()  

“控制”直方图在两个方面均应相同,但事实并非如此。这似乎是由于条件a1中的治疗组规模较大所致。

enter image description here

0 个答案:

没有答案