我正在嘗試將兩個繪制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
輸出

uj5u.com熱心網友回復:
我擔心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))

由reprex 包(v2.0.1)于 2021 年 11 月 15 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358880.html
