When combining ggplot2
objects using patchwork I would like to be able to have an option that I could easily set an option for all the plots to have the same x-axis and/or y-axis range.
reprex:
library(patchwork)
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
p1 <- mtcars %>%
ggplot() +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p2 <- mtcars %>%
filter(disp < 300) %>%
ggplot() +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 2')
p1 + p2
Created on 2020-02-01 by the reprex package (v0.3.0)
expected result setting it to both axes having the same range across both plots:
library(patchwork)
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
p1 <- mtcars %>%
ggplot() +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p2 <- mtcars %>%
filter(disp < 300) %>%
ggplot() +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 2') +
xlim(ggplot_build(p1)$layout$panel_scales_x[[1]]$range$range) +
ylim(ggplot_build(p1)$layout$panel_scales_y[[1]]$range$range)
p1 + p2
Created on 2020-02-01 by the reprex package (v0.3.0)
Does anyone have any ideas?