快速進行ggplot2繪圖布置-patchwork
LHB阿好伯, 2024/01/13
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
patchwork 利用簡單的數學運算符組合多個ggplot2圖片
允許任意複雜的圖組成
基本應用
+
加號與 |
豎線用於水平排版
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
/
除號用於垂直排版
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
()
括弧用於合併圖區
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
控制布局
plot_layout( ncol = NULL, nrow = NULL, byrow = NULL, widths = NULL, heights = NULL, guides = NULL, tag_level = NULL, design = NULL
)
欄列數
plot_layout(ncol = NULL, nrow = NULL )
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
繪圖區大小
plot_layout(widths = NULL, heights = NULL)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
空白繪圖區
plot_spacer()
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
指定排版
plot_layout(design = NULL)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
鑲嵌圖片
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
圖例控制
刪除重複圖例
plot_layout(guides='collect')


更改位置
theme(legend.position='bottom')

標題與註腳

變更標題字體大小

添加標籤
plot_annotation(tag_levels = 'A')
類型有'a', 'A', '1', 'i, 'I'
多層次標籤


添加對點陣圖形式(如raster類和nativeRaster類)
patchwork最初是為ggplot2設計的
但它一直支持其他圖形類型
比如grobs和基礎圖形(通過使用公式表示法)
在這次更新中,它增加了對另一種類型的支持
點陣圖。點陣圖類(raster class)和原生點陣圖類(nativeRaster class)現在它們可以被直接識別,並且可以通過wrap_elements()
函數使用
在被patchwork匯入的圖形也會成為事是ggplot2的物件
可以使用ggplot2的一些與法修改圖形呈現
例如背景、標題等

利用inset_element()就可以將圖片作為浮水印放到繪圖中
inset_element 函數在 patchwork 包中被用於將一個圖形元素(p)插入到另一個圖形中。
它的主要參數包括:
left, bottom, right, top:這些參數定義了插入圖形的位置和大小,
通常使用“npc”單位(0至1之間的數字),表示在父圖形中的相對位置。
align_to:決定對齊方式,可以是"panel"(默認)、"plot"或"full"。
on_top:布林值,決定圖形是否疊加在頂部。
clip:布林值,用於決定是否剪裁超出範圍的圖形部分。
ignore_tag:布林值,用於決定是否忽略標籤。

刪除重複的座標軸
當多個圖表共享相同的y軸時,為了避免視覺上的混亂
可以移除最右側圖表的軸
當然,通過設定主題來移除它,將相關的主題元素設置為element_blank()
但這樣做可能有些麻煩!
使用element_blank()
非常簡單
這種方法能輕鬆地管理和自定義圖表的軸線顯示
從而使得圖形的整體呈現更加清晰和專業。


也可以選擇只刪除Y座標標題
p1 + p2 + plot_layout(axis_titles = "collect")

若是有使用到 |
豎線用於水平鑲嵌的排版
你會發現plot_layout(axes = "collect")
不起作用

官方推薦可以使用widths
進行排版
但我更推薦使用design矩陣的方式
更加直覺且能進行更多變化
ncol 和 nrow:分別定義列數和行數。
byrow:決定布局是按行還是按列填充。
widths 和 heights:用於設定每列或每行的寬度和高度。
guides:控制圖例的顯示方式。
tag_level:設定標籤層次。
axes 和 axis_titles:控制軸線的收集與顯示方式。
design:提供一個更靈活的方式來定義佈局。

最後官方展示了一個不受文字拘束的的排版方式
避免受到過長的軸標籤所影響p1 / p3


參考資料
Multiple plots in ggplot2 with patchwork
Insetting a new patchwork version
Insetting a new patchwork version
全文分享至
https://www.facebook.com/LHB0222/
有疑問想討論的都歡迎於下方留言
喜歡的幫我分享給所有的朋友 \o/
有所錯誤歡迎指教
