簡體   English   中英

使用 R ggplot2 右對齊多個繪圖的水平 y 軸標題

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

我在右對齊 R ggplot2 中多個繪圖的水平 y 軸標題時遇到問題。 我有一個主要的 plot,它是一個帶有使用 ggdendro package 創建的葉子標簽的樹狀圖,並且我在主要的 Z32FA6E1B78A9D4028953E60564A2AAC4 下方有多個彩條。 如果我使用 grid.arrange 將圖放置在同一頁面上,我可以在圖之間獲得良好的垂直間距,但我無法一致地右對齊彩條的 y 軸標題。 如果我使用 plot_grid,我可以一致地右對齊 y 軸標題,但我無法在繪圖之間獲得適當的垂直間距。 任何幫助,將不勝感激!

更新:兩個建議的解決方案同樣有效,所以我接受第一個作為答案。 使用雞蛋ggarrange中的plot_grid並使用帶有align = "v"而不是align = "hv"的 plot_grid 都解決了我的問題。

創建主 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 方法:

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

在此處輸入圖像描述

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

使用ggarrange()p1p2p3堆疊在一起

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

代表 package (v0.3.0) 於 2020 年 8 月 6 日創建

暫無
暫無

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

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