简体   繁体   中英

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. Although all of the graphs have the same y-axis scale, I have added significance values to each plot individually. Because the graphs have different numbers of comparisons, the annotations go to different heights. How can I scale the ggarrange figure so that the y-axes are aligned?

Below is an example of the syntax for one of the graphs and the 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)

在此处输入图像描述

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.


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 has the option labels to hide some y ticks you need to add due to the significance bar
  • The function patchwork::wrap_plots enforces that the physical height of the axis is the same in every subplot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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