阿好伯
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- disqus: ahb0222 GA : G-CQ4L16KHK4 --- # 快速進行ggplot2繪圖布置-patchwork > [color=#40f1ef][name=LHB阿好伯, 2024/01/13][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `R` `ggplot2` `可視化` [TOC] **patchwork** 利用簡單的數學運算符組合多個ggplot2圖片 允許任意複雜的圖組成 # 基本應用 ## `+` 加號與 `|` 豎線用於水平排版 ![](https://i.imgur.com/e0IF3gv.png) ```r= library(ggplot2) library(patchwork) # 將ggplot2物件保存至變量中 p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear)) p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec)) p4 <- ggplot(mtcars) + geom_bar(aes(carb)) # 搭配數學運算符號排版 p1 + p2 ``` ![](https://i.imgur.com/hCr6Exa.png) ## `/`除號用於垂直排版 ```r=+ p1/p2 ``` ![](https://i.imgur.com/caz10al.png) ## `()`括弧用於合併圖區 ```r=+ (p1 | p2 | p3) / p4 ``` ![](https://i.imgur.com/qJbEiCe.png) ```r=+ (p1 / p2 /p3) | p4 ``` ![](https://i.imgur.com/OVJXQwr.png) # 控制布局 :::success `plot_layout( ncol = NULL, nrow = NULL, byrow = NULL, widths = NULL, heights = NULL, guides = NULL, tag_level = NULL, design = NULL` ) ::: ## 欄列數 :::success `plot_layout(ncol = NULL, nrow = NULL )` ::: ```r= p1 + p2 + p3 ``` ![](https://i.imgur.com/sUtmiMu.png) ```r=+ p1 + p2 + p3 + plot_layout(ncol=1) ``` ![](https://i.imgur.com/yQ71u55.png) ## 繪圖區大小 `plot_layout(widths = NULL, heights = NULL)` ```r= p1 / p2 / p3 ``` ![](https://i.imgur.com/gpfmaUD.png) ```r=+ p1 / p2 / p3 + plot_layout(heights=c(2,2,1)) ``` ![](https://i.imgur.com/xOcESVx.png) ## 空白繪圖區 `plot_spacer()` ```r= (p1 + plot_spacer() + p2)/ (plot_spacer() + p3 + plot_spacer()) ``` ![](https://i.imgur.com/gkWoVIp.png) # 指定排版 :::success `plot_layout(design = NULL)` ::: ```r= design <- " 122 133 444 " p1 + p2 + p3 + p4 + plot_layout(design = design) ``` ![](https://i.imgur.com/YvzLnHi.png) ## 鑲嵌圖片 :::success inset_element() ::: ```r= p1 + inset_element(p2, left = 0.5, bottom = 0.4, right = 0.99, top = 0.99) ``` ![](https://i.imgur.com/4iKOJPx.png) ## 圖例控制 ### 刪除重複圖例 :::success `plot_layout(guides='collect')` ::: ```r= p5 <- ggplot(mtcars) + geom_point(aes(mpg, disp, color=cyl)) p6 <- ggplot(mtcars) + geom_point(aes(mpg, hp, color=cyl)) p5 + inset_element(p6, left = 0.5, bottom = 0.4, right = 0.99, top = 0.99) ``` ![](https://i.imgur.com/Wu4SI70.png) ```r=+ p5 + inset_element(p6, left = 0.5, bottom = 0.4, right = 0.99, top = 0.99) +plot_layout(guides='collect') ``` ![](https://i.imgur.com/4Ntki7Q.png) ### 更改位置 :::success `theme(legend.position='bottom')` ::: ```r=+ p5 + inset_element(p6, left = 0.5, bottom = 0.4, right = 0.99, top = 0.99) +plot_layout(guides='collect')& theme(legend.position='bottom') ``` ![](https://i.imgur.com/g4sdjPa.png) ## 標題與註腳 ```r= p1 + inset_element(p2, left = 0.5, bottom = 0.4, right = 0.99, top = 0.99)+ plot_annotation('標題',caption = '註腳' ) ``` ![](https://i.imgur.com/foL4HIM.png) ### 變更標題字體大小 ```r=+ p1 + inset_element(p2, left = 0.5, bottom = 0.4, right = 0.99, top = 0.99)+ plot_annotation('標題',caption = '註腳', theme = theme(plot.title = element_text(size = 20))) ``` ![](https://i.imgur.com/deLQ514.png) ## 添加標籤 :::success `plot_annotation(tag_levels = 'A')` 類型有'a', 'A', '1', 'i, 'I' ::: ```r= ((p1 / p2 /p3) | p4 )+ plot_annotation(tag_levels = 'A') ``` ### 多層次標籤 ```r=+ (((p1 / p2 /p3)+ plot_layout(tag_level = 'new')) | p4) + plot_annotation(tag_levels = c('A', '1')) ``` ![](https://i.imgur.com/LPAoZsn.png) ```r=+ (((p1 / p2 /p3)+ plot_layout(tag_level = 'new')) | p4) + plot_annotation(tag_levels = list(c('smaple',"type"),'1')) ``` ![](https://i.imgur.com/xgzFLK2.png) ## 添加對點陣圖形式(如raster類和nativeRaster類) patchwork最初是為ggplot2設計的 但它一直支持其他圖形類型 比如grobs和基礎圖形(通過使用公式表示法) 在這次更新中,它增加了對另一種類型的支持 點陣圖。點陣圖類(raster class)和原生點陣圖類(nativeRaster class)現在它們可以被直接識別,並且可以通過`wrap_elements()`函數使用 ```r= library(png) # 假設您的檔案名稱是 'LOGO2.png' 並且它位於您的工作目錄中 file_path <- 'LOGO2.png' # 使用 readPNG 函數讀取圖像 logo2 <- readPNG(file_path, native = TRUE) p1 +logo2 ``` 在被patchwork匯入的圖形也會成為事是ggplot2的物件 可以使用ggplot2的一些與法修改圖形呈現 例如背景、標題等 ```r= library(magick) # 使用 magick 包讀取圖像 logo2 <- image_read('LOGO2.png') # 將白色背景變為透明 logo2_transparent <- image_transparent(logo2, 'white') # 保存修改後的圖像 image_write(logo2_transparent, 'LOGO2_transparent.png') file_path <- 'LOGO2_transparent.png' # 使用 readPNG 函數讀取圖像 logo2_transparent <- readPNG(file_path, native = TRUE) p1 +logo2_transparent + ggtitle('AHB Loge') + theme(plot.background = element_rect('grey')) ``` ![image](https://hackmd.io/_uploads/HJmO61xY6.png) 利用inset_element()就可以將圖片作為浮水印放到繪圖中 ```r= inset_element(p, left, bottom, right, top, align_to = "panel", on_top = TRUE, clip = TRUE, ignore_tag = FALSE) ``` :::danger inset_element 函數在 patchwork 包中被用於將一個圖形元素(p)插入到另一個圖形中。 它的主要參數包括: left, bottom, right, top:這些參數定義了插入圖形的位置和大小, 通常使用“npc”單位(0至1之間的數字),表示在父圖形中的相對位置。 align_to:決定對齊方式,可以是"panel"(默認)、"plot"或"full"。 on_top:布林值,決定圖形是否疊加在頂部。 clip:布林值,用於決定是否剪裁超出範圍的圖形部分。 ignore_tag:布林值,用於決定是否忽略標籤。 ::: ```r= p1 + inset_element(logo2_transparent, 0.8, 0.8, 1, 1, align_to = 'full') + theme_void() ``` ![image](https://hackmd.io/_uploads/ryrp5xxF6.png) ## 刪除重複的座標軸 當多個圖表共享相同的y軸時,為了避免視覺上的混亂 可以移除最右側圖表的軸 當然,通過設定主題來移除它,將相關的主題元素設置為element_blank() 但這樣做可能有些麻煩! 使用`element_blank()`非常簡單 這種方法能輕鬆地管理和自定義圖表的軸線顯示 從而使得圖形的整體呈現更加清晰和專業。 ```r= p1+p2 ``` ![image](https://hackmd.io/_uploads/H1BJJbxFT.png) ```r=+ p1 + p2 + plot_layout(axes = "collect") ``` ![image](https://hackmd.io/_uploads/H1bXy-eFp.png) 也可以選擇只刪除Y座標標題 p1 + p2 + plot_layout(axis_titles = "collect") ![image](https://hackmd.io/_uploads/HJJ9g-xYT.png) 若是有使用到 `|` 豎線用於水平鑲嵌的排版 你會發現`plot_layout(axes = "collect")`不起作用 ``` p1 + (p1 | p2) + plot_layout(axes = "collect") ``` ![image](https://hackmd.io/_uploads/ByHlf-lFp.png) 官方推薦可以使用`widths`進行排版 ```r= plot_layout function (ncol = NULL, nrow = NULL, byrow = NULL, widths = NULL, heights = NULL, guides = NULL, tag_level = NULL, design = NULL, axes = NULL, axis_titles = axes) ``` 但我更推薦使用design矩陣的方式 更加直覺且能進行更多變化 :::danger ncol 和 nrow:分別定義列數和行數。 byrow:決定布局是按行還是按列填充。 widths 和 heights:用於設定每列或每行的寬度和高度。 guides:控制圖例的顯示方式。 tag_level:設定標籤層次。 axes 和 axis_titles:控制軸線的收集與顯示方式。 design:提供一個更靈活的方式來定義佈局。 ::: ```r= # 創建一個描述佈局的矩陣 design <- " 1122 3333 4444 " p1 + p2 + p3 + p4 + plot_layout(design = design, axes = "collect") ``` ![image](https://hackmd.io/_uploads/HJBk3bgt6.png) 最後官方展示了一個不受文字拘束的的排版方式 避免受到過長的軸標籤所影響`p1 / p3` ![image](https://hackmd.io/_uploads/Hkocibetp.png) ``` p1 / free(p3) + plot_layout(guides = "collect") & theme_dark() ``` ![image](https://hackmd.io/_uploads/Hy9JjZxFa.png) # 參考資料 >[Multiple plots in ggplot2 with patchwork](https://gotellilab.github.io/GotelliLabMeetingHacks/NickGotelli/ggplotPatchwork.html) >[Insetting a new patchwork version](https://www.r-bloggers.com/2020/11/insetting-a-new-patchwork-version/) >[Insetting a new patchwork version](https://www.data-imaginist.com/posts/2020-11-09-insetting-a-new-patchwork-version/index.html) > 全文分享至 https://www.facebook.com/LHB0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/47HlvGH.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully