簡體   English   中英

R:ggplot 的 ggarrange 中的標簽不起作用

[英]R: Labels in ggarrange for ggplot won't work

我希望這很容易解決,但是,我花了很多時間研究關於這個主題的相關帖子,但並沒有真正接近解決我的問題。

我有這個 3x2 ggarrange object,我希望每一行都有一個 label。 到目前為止一切順利,但我不希望標簽是“A”、“B”等……我正在評估問卷,所以我希望 label 更像是每個行面板的標題。

我嘗試使用 vjust = 或 hjust = 等選項定位它們,但無法使其正常工作。

我的方法是什么? 我確信有幾種選擇。 我也嘗試過使用 cowplot 和 patchwork 包,但我總是被困在同一件事上。

我將 mtcars package 用於一些示例代碼:


p1 <- mtcars %>% ggplot(aes(x = mpg, y = cyl)) + geom_point()
p2 <- mtcars %>% ggplot(aes(x = mpg, y = disp)) + geom_point()
p3 <- mtcars %>% ggplot(aes(x = disp, y = cyl)) + geom_point()
p4 <- mtcars %>% ggplot(aes(x = mpg, y = drat)) + geom_point()
p5 <- mtcars %>% ggplot(aes(x = drat, y = wt)) + geom_point()
p6 <- mtcars %>% ggplot(aes(x = mpg, y = qsec)) + geom_point()

pcars1 <- ggarrange(p1,p2,p3,p4,p5,p6, ncol = 2, nrow = 3,
                   common.legend = T, legend = "bottom",
                   labels = c("A", "", "B", "","C",""))

pcars2 <- ggarrange(p1,p2,p3,p4,p5,p6, ncol = 2, nrow = 3,
                    common.legend = T, legend = "bottom",
                    labels = c("This is example A", "", 
                               "This is example B", "",
                               "This is example C",""))

note <- "Notes: This is mtcars data. A: Test sample 1, B: Test sample 2, C: Test sample3."

annotate_figure(pcars1, top = text_grob("Figure 1: Nonsense plots with mtcars"),
                bottom = text_grob(note, x = .5, size = 9))

annotate_figure(pcars2, top = text_grob("Figure 1: Nonsense plots with mtcars"),
                bottom = text_grob(note, x = .5, size = 9))

所以 object pcars2 就是我想要的。 只有它應該在每行的第一個 plot 的左側和上方,而不是在 plot 的右側。可能還有較小的字體而不是粗體,但我稍后會修復它。

非常感謝任何幫助:)

使用patchwork尤其是patchwork::wrap_elements你可以這樣做:

library(patchwork)

note <- "Notes: This is mtcars data. A: Test sample 1, B: Test sample 2, C: Test sample3."
title <- "Figure 1: Nonsense plots with mtcars"

list(
  p1 + p2 & plot_annotation(title = "This is example A"),
  p3 + p4 & plot_annotation(title = "This is example B"),
  p5 + p6 & plot_annotation(title = "This is example C")  
) |> 
  lapply(wrap_elements) |> 
  wrap_plots(ncol = 1) +
  plot_annotation(title = title, caption = note) &
  theme(plot.title = element_text(hjust = .5),
        plot.caption = element_text(hjust = .5))

在此處輸入圖像描述

編輯將圖例添加到patch的默認方法是使用guides="collect" 但是,由於我的方法使用wrap_elements (這是為每一行設置標題的技巧),所以該選項不可用(除了我們希望每一行都有一個單獨的圖例的情況)。 因此,我們必須采用 hacky 方式,例如,首先使用cowplot::get_legend提取其中一個圖的圖例。 其次,在將圖粘合在一起時刪除圖例。 最后添加圖例作為補丁的一部分,並根據自己的喜好設置heights參數。

library(ggplot2)
library(magrittr)

p1 <- mtcars %>% ggplot(aes(x = mpg, y = cyl, color = factor(cyl))) + geom_point()
p2 <- mtcars %>% ggplot(aes(x = mpg, y = disp, color = factor(cyl))) + geom_point()
p3 <- mtcars %>% ggplot(aes(x = disp, y = cyl, color = factor(cyl))) + geom_point()
p4 <- mtcars %>% ggplot(aes(x = mpg, y = drat, color = factor(cyl))) + geom_point()
p5 <- mtcars %>% ggplot(aes(x = drat, y = wt, color = factor(cyl))) + geom_point()
p6 <- mtcars %>% ggplot(aes(x = mpg, y = qsec, color = factor(cyl))) + geom_point()

library(patchwork)

note <- "Notes: This is mtcars data. A: Test sample 1, B: Test sample 2, C: Test sample3."
title <- "Figure 1: Nonsense plots with mtcars"

legend <- cowplot::get_legend(p1 + theme(legend.position = "bottom"))

plot_list <- list(
  p1 + p2 & plot_annotation(title = "This is example A") & theme(legend.position = "none"),
  p3 + p4 & plot_annotation(title = "This is example B") & theme(legend.position = "none"),
  p5 + p6 & plot_annotation(title = "This is example C") & theme(legend.position = "none")  
) |> 
  lapply(wrap_elements)

c(plot_list, list(legend)) |> 
  wrap_plots(ncol = 1, heights = c(rep(10, 3), 1)) &
  plot_annotation(title = title, caption = note) &
  theme(plot.title = element_text(hjust = .5),
        plot.caption = element_text(hjust = .5))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM