0

I am trying to create a plot using ggplot2 with 9 subplots. My goal is to allow the vertical axis to be scaled at the individual level for each of the 9 subplots. To that end, I am trying the syntax facet_wrap(scales = "free_y"). However, it does not appear to be working for me, and instead, the vertical axis seems to be identical across all 9 subplots. Below is a MWE:

x <- rep(c(rep("F",4),rep("M",4)),9)
y <- abs(c(rnorm(36,0,20),rnorm(36,0,5)))
z <- rep(1:9,each=8)
meanG1 <- c()
meanG2 <- c()
for (i in c(1,9,17,25,33,41,49,57,65)){
  meanG1 <- c(meanG1, rep(mean(y[i:(i+3)]),8))
  meanG2 <- c(meanG2, rep(mean(y[5:(i+3)]),8))
}
dat <- data.frame(x=x,y=y,z=z,meanG1=meanG1,meanG2=meanG2)

ggplot(dat, aes(x, y)) + 
  geom_point(aes(colour = factor(x)), shape = 20, size=5, alpha = 0.5) + 
  scale_shape(solid = FALSE) + 
  ggtitle(paste("My Plot")) + 
  ylab("Count") + 
  scale_y_continuous(limits=c(0, max(dat$y))) + 
  theme(axis.title.x = element_blank(), 
        legend.position="bottom", 
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12), 
        legend.title=element_text(size=12), 
        legend.text=element_text(size=12), 
        plot.title=element_text(hjust=0.5)) + 
  labs(colour = "Group", size=12) + 
  geom_segment(aes(x = 1, y = meanG1, xend = 2, yend = meanG2), colour="gray25", size = 0.1) + 
  facet_wrap(~ z, ncol = 3, scales = "free_y")

If anyone has advice on what might be causing this and/or how to fix this, I would be all ears! Thank you.

4

1 回答 1

2

您必须从 ggplot 代码中删除它

scale_y_continuous(limits=c(0, max(dat$y)))

因为它在所有方面都“固定”了 y 轴的范围

于 2016-12-01T02:24:59.443 回答