I am trying to create a faceted raincloud plot. By default, all facets should have the same scales; however, I find that the histograms have the same y-axis scales within facets and different y-axis scales across facets.

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()  

The "control" histogram should be identical in both facets, but it is not. This seems to result from the large scale of the treatment group in condition a1.

enter image description here

0 Answers