简体   繁体   English

ggarrange 为具有相同 y 轴但注释不同高度的图形对齐 y 轴

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

I have arranged a series of 9 graphs using ggarrange.我使用 ggarrange 排列了一系列 9 个图表。 Although all of the graphs have the same y-axis scale, I have added significance values to each plot individually.尽管所有图表都具有相同的 y 轴刻度,但我已分别为每个 plot 添加了显着性值。 Because the graphs have different numbers of comparisons, the annotations go to different heights.因为图表有不同的比较次数,所以注释 go 到不同的高度。 How can I scale the ggarrange figure so that the y-axes are aligned?如何缩放 ggarrange 图形以使 y 轴对齐?

Below is an example of the syntax for one of the graphs and the ggarrange function:下面是其中一个图表和 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()

This results in the following figure (bottom row cut off, but you can still see the different range in heights of the y-axis)这导致下图(底行被截断,但您仍然可以看到 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) ,so that each graph had the same maximum y-coordinate (6.5), but this did not work. I cannot change the y-axis scale in scale_y_continuous(breaks = seq(1,6,by = 1)) to anything higher, as this goes beyond the actual scale (the annotations are "extra"), so 6 needs to be the maximum number (I don't know if it is possible to add extra and somehow "hide" the numbers above 6?). I have also played around with the aspect ratios of the graphs and the width and height in ggarrange, but haven't been able to work out a solution yet. 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 中的宽度和高度,但还没有找到解决方案。


EDIT I updated the syntax for each graph to ```scale_y_continuous(breaks = seq(1,10,by = 1), labels = c("1", "2", "3", "4", "5", "6", "", "", "", "")) as per the suggestion by @danloo, but the heights are still different, is there a way to force an upper limit?编辑我将每个图形的语法更新为```scale_y_continuous(breaks = seq(1,10,by = 1), labels = c("1", "2", "3", "4", "5" , "6", "", "", "", "")) 根据@danloo 的建议,但高度仍然不同,有没有办法强制上限?

在此处输入图像描述

  • scale_y_continuous has the option labels to hide some y ticks you need to add due to the significance bar scale_y_continuous 具有选项标签来隐藏由于重要性栏而需要添加的一些 y 刻度
  • The function patchwork::wrap_plots enforces that the physical height of the axis is the same in every subplot. function patchwork::wrap_plots强制轴的物理高度在每个子图中都相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM