使用 R ggplot2 右对齐多个图的水平 y 轴标题
Right align horizontal y axis titles for multiple plots using R ggplot2
我在 R ggplot2 中右对齐多个绘图的水平 y 轴标题时遇到问题。我有一个主图,它是一个带有使用 ggdendro 包创建的叶标签的树状图,我在主图下方有多个颜色条,标题在左侧。如果我使用 grid.arrange 将绘图放在同一页上,我可以在绘图之间获得良好的垂直间距,但我无法 right-align 颜色条的 y 轴标题始终如一。如果我使用 plot_grid,我可以 right-align y 轴标题始终如一,但我无法在绘图之间获得适当的垂直间距。如有任何帮助,我们将不胜感激!
更新: 两个建议的解决方案同样有效,所以我接受第一个作为答案。使用 egg 包中的 ggarrange
并使用 plot_grid
和 align = "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))
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())
使用 ggarrange()
将 p1
、p2
和 p3
堆叠在一起
# install.packages("egg", dependencies = TRUE)
library(egg)
ggarrange(p1, p2, p3,
ncol = 1,
heights = c(8, 1, 1))
由 reprex package (v0.3.0)
于 2020-08-06 创建
我在 R ggplot2 中右对齐多个绘图的水平 y 轴标题时遇到问题。我有一个主图,它是一个带有使用 ggdendro 包创建的叶标签的树状图,我在主图下方有多个颜色条,标题在左侧。如果我使用 grid.arrange 将绘图放在同一页上,我可以在绘图之间获得良好的垂直间距,但我无法 right-align 颜色条的 y 轴标题始终如一。如果我使用 plot_grid,我可以 right-align y 轴标题始终如一,但我无法在绘图之间获得适当的垂直间距。如有任何帮助,我们将不胜感激!
更新: 两个建议的解决方案同样有效,所以我接受第一个作为答案。使用 egg 包中的 ggarrange
并使用 plot_grid
和 align = "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))
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())
使用 ggarrange()
p1
、p2
和 p3
堆叠在一起
# install.packages("egg", dependencies = TRUE)
library(egg)
ggarrange(p1, p2, p3,
ncol = 1,
heights = c(8, 1, 1))
由 reprex package (v0.3.0)
于 2020-08-06 创建