same_scales: Make scales of list of ggplot objects the same

View source: R/formatting.R

same_scalesR Documentation

Make scales of list of ggplot objects the same

Description

Given a list of ggplots, make the x and/or y scales of the plots the same.

Usage

same_scales(
  p,
  xsame = TRUE,
  ysame = TRUE,
  xmin = -Inf,
  xmax = Inf,
  ymin = -Inf,
  ymax = Inf
)

Arguments

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)

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.

Value

Same list of ggplots, with axes adjusted

Examples

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


BIO-RSG/oceancolouR documentation built on April 30, 2024, 7:54 a.m.