Add vertical axis to each panel plots

45 views Asked by At

An editor instructed me to remove the gray background and grid lines and add black axis lines to a plot I created with ggplot2. I utilized cowplot and achieved most of the requested changes. However, one detail that remains missing is the vertical line for each plot. The output only includes the line in the leftmost plot, but I need to add a vertical line (y-axis) to each panel plot.

# example
y <- rnorm(300)
x <- rep(1:3,each=100)
library(ggplot2)
library(cowplot)
dat <- as.data.frame(x=x,y=y)

ggplot(dat,aes(y=y))+geom_boxplot()+facet_grid(~x)+theme_cowplot(12)
1

There are 1 answers

0
stefan On BEST ANSWER

If facet_grid isn't really needed you could switch to facet_wrap(~x, scales = "free_y") and fix the limits of the y scale using scale_y_continuous(limits = range(dat$y)):

# example
set.seed(123)

y <- rnorm(300)
x <- rep(1:3, each = 100)
library(ggplot2)
library(cowplot)

dat <- data.frame(x = x, y = y)

ggplot(dat, aes(y = y)) +
  geom_boxplot() +
  scale_y_continuous(limits = range(dat$y)) +
  facet_wrap(~x, scales = "free_y") +
  theme_cowplot(12)