2

我在右对齐 R ggplot2 中多个绘图的水平 y 轴标题时遇到问题。我有一个主图,它是一个带有使用 ggdendro 包创建的叶子标签的树状图,我在主图下方有多个颜色条,左侧有标题。如果我使用 grid.arrange 将图放置在同一页面上,我可以在图之间获得良好的垂直间距,但我无法一致地右对齐彩条的 y 轴标题。如果我使用 plot_grid,我可以一致地右对齐 y 轴标题,但我无法在绘图之间获得适当的垂直间距。任何帮助,将不胜感激!

更新:两个建议的解决方案同样有效,所以我接受第一个作为答案。ggarrange从 egg 包中使用并使用withplot_gridalign = "v"不是align = "hv"两者都解决了我的问题。

创建主图和彩条:

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 方法:

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 方法:

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

在此处输入图像描述

4

1 回答 1

2

egg包将完成工作

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())

堆栈p1p2p3一起使用ggarrange()

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

reprex 包(v0.3.0)于 2020-08-06 创建

于 2020-08-06T08:56:32.907 回答