我想一起显示几个不同的图,每行上的每种类型的图,共享相同的图例。
为了分享相同的图例,我使用了
grid.arrange()
。ggarrange()
,以便每个图系列显示在特定行上。
我的问题是,当图例名称大小不同时,图例不对齐。
在下面的可重现示例中,我有一个短标题和一个长标题,但它们没有对齐(在左侧)。
我尝试使用 ggarrange 选项
align='v'
但它似乎只适用于绘图,而不适用于图例。
##### Loading libraries
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(lemon)
##### Initiating objects
### COlors
dfCol1 <- data.frame(Groups=0:3,
col=c("grey80", "#87c5ff", "#477cad", "#113c63"))
dfCol2 <- data.frame(Groups=0:3,
col=c("grey80", "#87ffd5", "#47ad88", "#116349"))
### Legends
legend1 <- g_legend(ggplot(dfCol1, aes(x=2, y=1, fill=factor(Groups))) +
geom_bar(stat="identity") +
scale_fill_manual(values=dfCol1$col, name="short") +
theme(legend.position="right", legend.text=element_text(size=7), legend.key.size=unit(0.4, 'cm')) +
guides(fill=guide_legend(title.position="top", title.hjust=0.5)))
legend2 <- g_legend(ggplot(dfCol2, aes(x=2, y=1, fill=factor(Groups))) +
geom_bar(stat="identity") +
scale_fill_manual(values=dfCol2$col, name="veryLongTitle") +
theme(legend.position="right", legend.text=element_text(size=7), legend.key.size=unit(0.4, 'cm')) +
guides(fill=guide_legend(title.position="top", title.hjust=0.5)))
### Plots
barPlot1 <-
ggplot(dfCol1, aes(x=2, y=1, fill=factor(Groups))) +
geom_bar(stat="identity") +
scale_fill_manual(values=dfCol1$col, name="abc") +
guides(fill="none")
barPlot2 <-
ggplot(dfCol2, aes(x=2, y=1, fill=factor(Groups))) +
geom_bar(stat="identity") +
scale_fill_manual(values=dfCol2$col, name="abc") +
guides(fill="none")
##### Display plots
ggarrange(
grid.arrange(barPlot1, barPlot1, barPlot1, nrow=1, legend1),
grid.arrange(barPlot2, barPlot2, barPlot2, nrow=1, legend2),
nrow=2
)
我想实现以下目标。
谢谢你
一种选择是使用
legend.justification
设置图例左对齐,并通过 legend.box.margin
在图例框的左侧添加一些边距:
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(lemon)
### Legends
legend1 <- g_legend(ggplot(dfCol1, aes(x = 2, y = 1, fill = factor(Groups))) +
geom_bar(stat = "identity") +
scale_fill_manual(values = dfCol1$col, name = "short") +
theme(
legend.position = "right",
legend.text = element_text(size = 7),
legend.key.size = unit(0.4, "cm"),
legend.justification = "left",
legend.box.margin = margin(l = 1, unit = "cm")
) +
guides(fill = guide_legend(title.position = "top", title.hjust = 0.5)))
legend2 <- g_legend(ggplot(dfCol2, aes(x = 2, y = 1, fill = factor(Groups))) +
geom_bar(stat = "identity") +
scale_fill_manual(values = dfCol2$col, name = "veryLongTitle") +
theme(
legend.position = "right",
legend.text = element_text(size = 7),
legend.key.size = unit(0.4, "cm"),
legend.justification = "left",
legend.box.margin = margin(l = 1, unit = "cm")
) +
guides(fill = guide_legend(title.position = "top", title.hjust = 0.5)))
### Plots
barPlot1 <-
ggplot(dfCol1, aes(x = 2, y = 1, fill = factor(Groups))) +
geom_bar(stat = "identity") +
scale_fill_manual(values = dfCol1$col, name = "abc") +
guides(fill = "none")
barPlot2 <-
ggplot(dfCol2, aes(x = 2, y = 1, fill = factor(Groups))) +
geom_bar(stat = "identity") +
scale_fill_manual(values = dfCol2$col, name = "abc") +
guides(fill = "none")
##### Display plots
ggarrange(
grid.arrange(barPlot1, barPlot1, barPlot1, nrow = 1, legend1),
grid.arrange(barPlot2, barPlot2, barPlot2, nrow = 1, legend2),
nrow = 2
)