简体   繁体   English

使用 R ggplot2 右对齐多个绘图的水平 y 轴标题

[英]Right align horizontal y axis titles for multiple plots using R ggplot2

I'm having trouble right aligning horizontal y axis titles for multiple plots in R ggplot2.我在右对齐 R ggplot2 中多个绘图的水平 y 轴标题时遇到问题。 I have a main plot which is a dendrogram with leaf labels created using the ggdendro package, and I have multiple color bars below the main plot with titles to the left.我有一个主要的 plot,它是一个带有使用 ggdendro package 创建的叶子标签的树状图,并且我在主要的 Z32FA6E1B78A9D4028953E60564A2AAC4 下方有多个彩条。 If I use grid.arrange to place the plots on the same page, I'm able to get good vertical spacing between the plots, but I'm not able to right-align the y axis titles for the color bars consistently.如果我使用 grid.arrange 将图放置在同一页面上,我可以在图之间获得良好的垂直间距,但我无法一致地右对齐彩条的 y 轴标题。 If I use plot_grid, I can right-align the y axis titles consistently, but I'm having trouble getting appropriate vertical spacing between plots.如果我使用 plot_grid,我可以一致地右对齐 y 轴标题,但我无法在绘图之间获得适当的垂直间距。 Any help would be appreciated!任何帮助,将不胜感激!

Update: Two suggested solutions work equally well so I'm accepting the first one as the answer.更新:两个建议的解决方案同样有效,所以我接受第一个作为答案。 Using ggarrange from the egg package and using plot_grid with align = "v" instead of align = "hv" both fixed my problem.使用鸡蛋ggarrange中的plot_grid并使用带有align = "v"而不是align = "hv"的 plot_grid 都解决了我的问题。

Create main plot and color bars:创建主 plot 和彩条:

require(ggplot2)
require(gridExtra)
require(cowplot)
require(ggdendro)

hc = hclust(dist(USArrests), "ave")
df = data.frame(cluster = cutree(hc, 6),
                states = factor(hc$labels, levels = hc$labels[hc$order]))
p1_dendro = dendro_data(hc)

p1 = ggdendrogram(hc) +
  coord_cartesian(xlim = c(-1, nrow(df) + 1), ylim = c( -1, max(p1_dendro$segments$y)), expand = F)

p2 = ggplot(df, aes(states, y = 1, fill = factor(cluster))) + 
  ylab("y label") +
  geom_tile() + theme_minimal() +
  coord_cartesian(xlim = c(-1, nrow(df) + 1), expand = F) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 1),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        line = element_blank())

p3 = ggplot(df, aes(states, y = 1, fill = factor(cluster))) +
  ylab("a longer y label") +
  geom_tile() + theme_minimal() +
  coord_cartesian(xlim = c(-1, nrow(df) + 1), expand = F) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 1),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        line = element_blank())

grid.arrange approach: grid.arrange 方法:

gp1 = ggplotGrob(p1)
gp2 = ggplotGrob(p2)  
gp3 = ggplotGrob(p3)

maxWidth = grid::unit.pmax(gp1$widths[2:5], gp2$widths[2:5], gp3$widths[2:5])
gp1$widths[2:5] = as.list(maxWidth)
gp2$widths[2:5] = as.list(maxWidth)
gp3$widths[2:5] = as.list(maxWidth)

grid.arrange(gp1, gp2, gp3, ncol = 1, heights = c(8,1,1))

在此处输入图像描述

plot_grid approach: plot_grid 方法:

plot_grid(p1, p2, p3, ncol = 1, align = "hv", axis = "tblr", rel_heights = c(8,1,1))

在此处输入图像描述

egg package will get the job doneegg package 将完成工作

require(ggplot2)
require(ggdendro)

hc = hclust(dist(USArrests), "ave")
df = data.frame(cluster = cutree(hc, 6),
                states = factor(hc$labels, levels = hc$labels[hc$order]))
p1_dendro = dendro_data(hc)

p1 = ggdendrogram(hc) +
  coord_cartesian(xlim = c(-1, nrow(df) + 1), ylim = c( -1, max(p1_dendro$segments$y)), expand = F)

p2 = ggplot(df, aes(states, y = 1, fill = factor(cluster))) + 
  ylab("y label") +
  geom_tile() + theme_minimal() +
  coord_cartesian(xlim = c(-1, nrow(df) + 1), expand = F) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 1),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        line = element_blank())

p3 = ggplot(df, aes(states, y = 1, fill = factor(cluster))) +
  ylab("a longer y label") +
  geom_tile() + theme_minimal() +
  coord_cartesian(xlim = c(-1, nrow(df) + 1), expand = F) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 1),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        line = element_blank())

Stack p1 , p2 and p3 together using ggarrange()使用ggarrange()p1p2p3堆叠在一起

# install.packages("egg", dependencies = TRUE)
library(egg)
ggarrange(p1, p2, p3, 
          ncol = 1,
          heights = c(8, 1, 1))

Created on 2020-08-06 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 8 月 6 日创建

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

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