在 ggplot() 中结合并改变同一 y 轴上两个图形的尺寸
Cohesively combine and alter the dimensions of two figures on the same y axis in ggplot()
我正在尝试完成两项任务:
将共享相同 y 轴的两个图形结合在一起,但一个具有分类 x 轴变量,另一个具有连续 x 轴变量。我想将它们显示为连续的,仅由黑色实线分隔(即左图的右边缘和右图的左边缘)。
自由修改图形尺寸,让我可以。延长左图的 x 轴以更好地展示数据的传播,以及 ii.将两个图形的大小比例理想化。
以下是我的尝试:
#libraries used:
library(ggplot2)
library(dplyr)
#Pulling in example dataset:
data_1 <- iris
#Building my left figure, which has a continuous x and y axis, and establishing y axis limits to match between the two figures:
object_1 <- ggplot(data_1, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() + ylim(0, 10)
#Building my second data table:
data_2 <- iris %>% group_by(Species) %>% summarize(av_petal_length = mean(Petal.Length))
#Building my right hand figure, with empty y axis titles and text to provide space to combine the two figures on the left y axis:
object_2 <- ggplot(data_2, aes(x = Species, y = av_petal_length)) + geom_point() + ylim(0, 10) +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank())
#Attempt to grid.arrange:
grid.arrange(object_1, object_2, nrow = 1)
如您所见,一个简单的grid.arrange并没有将它们完全结合起来。我试图通过修改 theme() 下的 plot.margin() 来修改两个图中的面板边距,但这需要大量修改,如果完全调整图形大小,则两个图形之间的关系可能会变成扭曲。是否可以干净利落地将这两个图形简单地组合成一个连贯的矩形,用一条线分隔,然后手动修改图形的尺寸?
下面,我们对左右图使用单独的主题,删除相关图边距和右侧图的 y-axis。
我相信你也可以用 grid.arrange()
做到这一点,但 {patchwork} 也允许你设置图形宽度。
library(ggplot2)
library(dplyr)
library(patchwork)
# As before
data_1 <- iris
object_1 <- ggplot(data_1, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() + ylim(0, 10)
data_2 <- iris %>% group_by(Species) %>% summarize(av_petal_length = mean(Petal.Length))
object_2 <- ggplot(data_2, aes(x = Species, y = av_petal_length)) + geom_point() + ylim(0, 10)
# Remove relevant margins from theme, including y-axis elements on the right
theme_left <- theme(plot.margin = margin(5.5, 0, 5.5, 5.5))
theme_right <- theme(plot.margin = margin(5.5, 5.5, 5.5, 0),
axis.ticks.length.y = unit(0, "pt"),
axis.title.y = element_blank(),
axis.text.y = element_blank())
black_line <- annotate("segment", x = Inf, xend = Inf, y = -Inf, yend = Inf, size = 2)
# Patchwork everything together
(object_1 + theme_left + black_line) +
(object_2 + theme_right) +
plot_layout(widths = c(2, 1))
由 reprex package (v2.0.1)
创建于 2022-02-01
我正在尝试完成两项任务:
将共享相同 y 轴的两个图形结合在一起,但一个具有分类 x 轴变量,另一个具有连续 x 轴变量。我想将它们显示为连续的,仅由黑色实线分隔(即左图的右边缘和右图的左边缘)。
自由修改图形尺寸,让我可以。延长左图的 x 轴以更好地展示数据的传播,以及 ii.将两个图形的大小比例理想化。
以下是我的尝试:
#libraries used:
library(ggplot2)
library(dplyr)
#Pulling in example dataset:
data_1 <- iris
#Building my left figure, which has a continuous x and y axis, and establishing y axis limits to match between the two figures:
object_1 <- ggplot(data_1, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() + ylim(0, 10)
#Building my second data table:
data_2 <- iris %>% group_by(Species) %>% summarize(av_petal_length = mean(Petal.Length))
#Building my right hand figure, with empty y axis titles and text to provide space to combine the two figures on the left y axis:
object_2 <- ggplot(data_2, aes(x = Species, y = av_petal_length)) + geom_point() + ylim(0, 10) +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank())
#Attempt to grid.arrange:
grid.arrange(object_1, object_2, nrow = 1)
如您所见,一个简单的grid.arrange并没有将它们完全结合起来。我试图通过修改 theme() 下的 plot.margin() 来修改两个图中的面板边距,但这需要大量修改,如果完全调整图形大小,则两个图形之间的关系可能会变成扭曲。是否可以干净利落地将这两个图形简单地组合成一个连贯的矩形,用一条线分隔,然后手动修改图形的尺寸?
下面,我们对左右图使用单独的主题,删除相关图边距和右侧图的 y-axis。
我相信你也可以用 grid.arrange()
做到这一点,但 {patchwork} 也允许你设置图形宽度。
library(ggplot2)
library(dplyr)
library(patchwork)
# As before
data_1 <- iris
object_1 <- ggplot(data_1, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() + ylim(0, 10)
data_2 <- iris %>% group_by(Species) %>% summarize(av_petal_length = mean(Petal.Length))
object_2 <- ggplot(data_2, aes(x = Species, y = av_petal_length)) + geom_point() + ylim(0, 10)
# Remove relevant margins from theme, including y-axis elements on the right
theme_left <- theme(plot.margin = margin(5.5, 0, 5.5, 5.5))
theme_right <- theme(plot.margin = margin(5.5, 5.5, 5.5, 0),
axis.ticks.length.y = unit(0, "pt"),
axis.title.y = element_blank(),
axis.text.y = element_blank())
black_line <- annotate("segment", x = Inf, xend = Inf, y = -Inf, yend = Inf, size = 2)
# Patchwork everything together
(object_1 + theme_left + black_line) +
(object_2 + theme_right) +
plot_layout(widths = c(2, 1))
由 reprex package (v2.0.1)
创建于 2022-02-01