Ed_*_*avy 3 r ggplot2 patchwork
我正在尝试将两个patchwork objects一起绘制。该代码有效但plot_annotations消失了。
如何解决这个问题?
数据+代码:
library(tidyverse)
library(patchwork)
#Plot 1
GG1 = ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point()
#Plot 2
GG2 = ggplot(iris,aes(y=Sepal.Length,x=Sepal.Width))+
geom_point()
#Plot 3
GG3 = ggplot(iris,aes(y=Petal.Width,x=Sepal.Width))+
geom_point()
#Plot 4
GG4 = ggplot(iris,aes(y=Petal.Length,x=Petal.Width))+
geom_point()
combine_1 = GG1 + GG2 +
plot_annotation(title = 'Combine plot 1',
theme = theme(plot.title = element_text(hjust = 0.5)))
combine_2 = GG3 + GG4 +
plot_annotation(title = 'Combine plot 2',
theme = theme(plot.title = element_text(hjust = 0.5)))
combine_1/combine_2
Run Code Online (Sandbox Code Playgroud)
输出
恐怕您无法plot_annotation根据文档实现所需的结果
...它只会对顶层情节产生影响。
但这将是一个很好的功能。
作为解决方法,您可以将标题添加到组合的子图中textGrobs。
笔记:
textGrobs 包裹在里面wrap_elements。plot_layout就是我必须用于plot_layout(heights = c(1, 10, 11))最终情节的原因。library(ggplot2)
library(patchwork)
library(grid)
#Plot 1
GG1 = ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point()
#Plot 2
GG2 = ggplot(iris,aes(y=Sepal.Length,x=Sepal.Width))+
geom_point()
title1 <- grid::textGrob(label = "Combine Plot1")
title2 <- grid::textGrob(label = "Combine Plot2")
combine_1 = (wrap_elements(panel = title1) / (GG1 + GG2)) + plot_layout(heights = c(1, 10))
combine_2 = (wrap_elements(panel = title2) / (GG1 + GG2)) + plot_layout(heights = c(1, 10))
(combine_1 / combine_2) + plot_layout(heights = c(1, 10, 11))
Run Code Online (Sandbox Code Playgroud)

编辑在回答这个相关问题时,我能够想出一种更简单的方法来添加多个图,该方法只需将每个图包装在一起即可将组合图包装在一起patchwork::wrap_elements:
combine_1 = (GG1 + GG2) & plot_annotation(title = "Combine Plot1") & theme(plot.title = element_text(hjust = .5))
combine_2 = (GG1 + GG2) & plot_annotation(title = "Combine Plot2") & theme(plot.title = element_text(hjust = .5))
wrap_elements(combine_1) / wrap_elements(combine_2)
Run Code Online (Sandbox Code Playgroud)
