簡體   English   中英

ggarrange 為具有相同 y 軸但注釋不同高度的圖形對齊 y 軸

[英]ggarrange align y-axes for graphs with same y-axis but annotations of different heights

我使用 ggarrange 排列了一系列 9 個圖表。 盡管所有圖表都具有相同的 y 軸刻度,但我已分別為每個 plot 添加了顯着性值。 因為圖表有不同的比較次數,所以注釋 go 到不同的高度。 如何縮放 ggarrange 圖形以使 y 軸對齊?

下面是其中一個圖表和 ggarrange function 的語法示例:

pwc_example <- example_melt %>%
  wilcox_test(value ~ variable, paired = TRUE, p.adjust.method = "holm", detailed = TRUE)

gg_example <- ggplot(example_melt, aes(x = reorder(variable, value), y = value)) +
  stat_summary(fun = mean, geom = "bar", width = 0.75, aes(fill = variable)) +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar",
               colour="black", position=position_dodge(1), width=.2) + 
  stat_pvalue_manual(pwc_example, label = "p.adj.signif", tip.length = 0.02, step.increase = 0.05, hide.ns = TRUE, y.position = c(6.3, 6.4, 6.5), label.size = 3) +
  scale_x_discrete(labels = c(init_com_rank = "Initial Communication", intervention_rank = "Intervention Needed", no_prob_rank = "No Problem", fin_com_rank = "Close communication", com_interrupted_rank = "Broken Communication", battery_rank = "Low Battery")) +
  ggtitle("Sequence 9") +
  theme(plot.title = element_text(size=10, hjust = 0.5, face = "bold")) +
  scale_y_continuous(breaks = seq(1,6,by = 1)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25))

gg_1 + scale_fill_manual(values = c("#9E0142", "#FDAE61","#FDAE61", "#FDAE61", "#FDAE61", "#FDAE61")) + theme(legend.position = "none")

figure <- ggarrange(gg_1, gg_2, gg_3, gg_4, gg_5, gg_6, gg_7, gg_8, gg_9, ncol = 3, nrow = 3, align = "hv")

pdf("figure.pdf", height=14)
ggdraw(figure)
dev.off()

這導致下圖(底行被截斷,但您仍然可以看到 y 軸高度的不同范圍)

在此處輸入圖像描述

I tried manually changing the positions of the annotations using y.position in stat_pvalue_manual(pwc, label = "p.adj.signif", tip.length = 0.02, step.increase = 0.05, hide.ns = TRUE, y.position = c(6.3, 6.4, 6.5) ,這樣每個圖都有相同的最大 y 坐標 (6.5),但這不起作用。我無法更改scale_y_continuous(breaks = seq(1,6,by = 1)) ) 中的 y 軸比例scale_y_continuous(breaks = seq(1,6,by = 1))到更高的值,因為這超出了實際比例(注釋是“額外的”),所以 6 需要是最大數字(我不知道是否可以添加額外的並以某種方式“隱藏” 6 以上的數字?)。我也玩過圖形的縱橫比以及 ggarrange 中的寬度和高度,但還沒有找到解決方案。


編輯我將每個圖形的語法更新為```scale_y_continuous(breaks = seq(1,10,by = 1), labels = c("1", "2", "3", "4", "5" , "6", "", "", "", "")) 根據@danloo 的建議,但高度仍然不同,有沒有辦法強制上限?

在此處輸入圖像描述

  • scale_y_continuous 具有選項標簽來隱藏由於重要性欄而需要添加的一些 y 刻度
  • function patchwork::wrap_plots強制軸的物理高度在每個子圖中都相同。

暫無
暫無

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

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