When using grid arrange I encountered the following problem : I want all my panels (a,b,c) to
One hack could be to use facet_grid with scales = "free_y"
, and add some "invisible" data that will be used when ggplot automatically generates the y axis limits. This would work if you want to expand the automatically generated axes beyond the limits of your data, but not if you want to constrain them to exclude a portion of your data:
data <- data.frame(values=c(runif(20,0,40),runif(40,0,15)),
product=c(rep("a",20),rep("b",20),rep("c",20)),
treat=c("e","f","g","h","i"),
time=c(0,6,24,48))
ggplot(data, aes(time, values, color=treat)) +
geom_line() + geom_point() +
geom_hline(data=data.frame(product=c("a", "b", "c"), values=c(40, 15, 15)),
aes(yintercept=values), color="white") +
facet_grid(product~., scales="free") +
theme(panel.background=element_blank(), panel.grid=element_blank())
You should use rbind
instead of grid.arrange,
plots <- list(p_a, p_b, p_c)
grobs <- lapply(plots, ggplotGrob)
# for gridExtra < v2.3, use do.call(gridExtra::rbind.gtable, grobs)
# for gridExtra >= v2.3 use:
g <- do.call(gridExtra::gtable_rbind, grobs)
library(grid)
grid.newpage()
grid.draw(g)