【发布时间】:2021-02-21 17:06:39
【问题描述】:
我有一些基于二分计数数据创建的 ggplot 对象。我使用 ggarrange 将它们组合在一起。在这个函数中,有一个创建共享图例的选项,但据我所知,没有办法创建共享的 x 和 y 轴标签。此外,图形的间距非常奇怪——两个图形列之间存在巨大的间隙,另外在共享图例之前还有大量的垂直空间。所以总而言之,我希望能够创建共享的 x 和 y 轴,并尽量减少不必要的垂直和水平空间。
我查看了以下主题: ggplot2 grid_arrange_shared_legend share axis labels
ggplot: align plots together and add common labels and legend
Add common axis titles with lines/arrows for multiple plots in ggplot
ggplot: how to add common x and y labels to a grid of plots
但我认为我想做的事情要简单一些,我不知道如何将 ggarrange 与 facet.grid 结合起来,这被建议了几次。
我在下面复制了一个可重现的示例。
require(tidyverse)
require(ggpubr)
require(reshape2)
condition <- c("a", "a", "a", "b", "b", "b", "c", "c", "c", "c")
binary_1 <- c(0,0,0,0,0,1,1,1,1,1)
binary_2 <- c(1,1,1,1,1,1,0,0,0,0)
binary_3 <- c(0,1,1,1,1,1,1,1,0,0)
binary_4 <- c(1,1,1,0,0,0,0,0,0,0)
df <- data.frame(condition, binary_1, binary_2, binary_3, binary_4)
df
gg_df <- df %>%
mutate(binary_1 = as.factor(binary_1), binary_2 = as.factor(binary_2), binary_3 = as.factor(binary_3), binary_4 = as.factor(binary_4))
gg_melt <- melt(gg_df)
gg_1 <- ggplot(gg_melt, aes(x=condition, fill = binary_1)) +
geom_bar(stat="count") +
scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) +
xlab("Condition") +
ylab("Number of Participants") +
ggtitle("Binary 1") +
theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
theme(aspect.ratio = 1) +
theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
theme(legend.box.just = "center")
gg_2 <- ggplot(gg_melt, aes(x=condition, fill = binary_2)) +
geom_bar(stat="count") +
scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) +
xlab("Condition") +
ylab("Number of Participants") +
ggtitle("Binary 2") +
theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
theme(aspect.ratio = 1) +
theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
theme(legend.box.just = "center")
gg_3 <- ggplot(gg_melt, aes(x=condition, fill = binary_3)) +
geom_bar(stat="count") +
scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) +
xlab("Condition") +
ylab("Number of Participants") +
ggtitle("Binary 3") +
theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
theme(aspect.ratio = 1) +
theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
theme(legend.box.just = "center")
gg_4 <- ggplot(gg_melt, aes(x=condition, fill = binary_4)) +
geom_bar(stat="count") +
scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) +
xlab("Condition") +
ylab("Number of Participants") +
ggtitle("Binary 4") +
theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
theme(aspect.ratio = 1) +
theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
theme(legend.box.just = "center")
figure <- ggarrange(gg_1, gg_2, gg_3, gg_4,
labels = NULL,
ncol = 2, nrow = 4,
common.legend = TRUE, legend = "bottom",
align = "hv",
font.label = list(size = 10, color = "black", face = "bold", family = NULL, position = "top"))
pdf("figure.pdf")
figure
dev.off()
【问题讨论】:
-
尝试使用
pdf("figure.pdf", width=7, height=14)。请记住,在您的情节中有 2 个空行(nrow = 2将是ggarrange中的正确选择)。