将
ggplot2
对象使用patchwork组合时,我希望有一个选项可以轻松设置所有图的x轴和/或y轴范围相同。
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
使用reprex包 (v0.3.0),2020-02-01创建
期望结果是将两张图的坐标轴设置为相同范围:
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
由reprex package (v0.3.0)于2020-02-01创建
有人有什么想法吗?
f(p1 + p2)
这样的函数来设置坐标轴相等? - Allan Cameron