same_scales | R Documentation |
Given a list of ggplots, make the x and/or y scales of the plots the same.
same_scales(
p,
xsame = TRUE,
ysame = TRUE,
xmin = -Inf,
xmax = Inf,
ymin = -Inf,
ymax = Inf
)
p |
List of ggplot objects |
xsame |
TRUE/FALSE, should x axes be the same? |
ysame |
TRUE/FALSE, should y axes be the same? |
xmin |
Optional minimum value for x axis (see details) |
xmax |
Optional maximum value for x axis (see details) |
ymin |
Optional minimum value for y axis (see details) |
ymax |
Optional maximum value for y axis (see details) |
Note that if you want to manually adjust scales (ymin, ymax, etc), you should do it in each plot using, for example, scale_y_continuous limits. Here, xmin/xmax are only used if xsame=TRUE, and ymin/ymax are only used if ysame=TRUE.
Same list of ggplots, with axes adjusted
library(ggplot2)
library(patchwork) # for wrap_plots
p1 <- ggplot(data.frame(x=1:100,y1=1:100), aes(x=x,y=y1)) + geom_point()
p2 <- ggplot(data.frame(x=1:100,y2=rnorm(100)), aes(x=x,y=y2)) + geom_point()
p3 <- ggplot(data.frame(x=1:100,y3=50:-49), aes(x=x,y=y3)) + geom_point()
p4 <- ggplot(data.frame(x=4:5, y4=1:2), aes(x=x,y=y4)) + geom_point()
p <- list(p1, p2, p3, p4)
wrap_plots(p)
wrap_plots(same_scales(p))
wrap_plots(same_scales(p, xsame=FALSE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.