Sometimes I will make two plots that are similair, but have different data. As such, the X- and Y-axis will have different ranges.
library(ggplot2)
library(ggpubr)
#> Loading required package: magrittr
df1 <- data.frame(x=runif(10)*2,y = runif(10)*2)
df2 <- data.frame(x=runif(10)*3,y = runif(10)*1)
p1 <- qplot(x = x, y = y, data = df1, geom = "line")
p2 <- qplot(x = x, y = y, data = df2, geom = "line")
ggarrange(p1,p2)
Created on 2020-07-09 by the reprex package (v0.3.0)
This can manually be overcome by explicitly stating a range with xlim
and ylim
, but this is both tiresome and could lead to some data being outside of the specified range if one is not careful.
An ideal solution would be to dynamically obtain the limits from the p1
, and if these are larger than those of p2
, use those instead for p2
. For example p2 + xlim(getLimits(p1))
.
Is something like this supported?
EDIT:
This question was suggested, but the answer applies to an older version of ggplot2
. Additionally, the supported p1$coordinates$limits
only return the manually specified limits, which defeats the purpose.