当使用拼凑组合ggplot2
对象时,我希望能够有一个选项,我可以轻松地为所有图设置一个选项,使其具有相同的 x 轴和/或 y 轴范围。
代表:
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 年 2 月 1 日创建
预期结果将其设置为在两个图中具有相同范围的两个轴:
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 包(v0.3.0)于 2020 年 2 月 1 日创建
有没有人有任何想法?