I have a number off ggplot objects created based off dichotomous count data. I have combined them together using ggarrange. In this function, there is an option to create a shared legend, but as far as I can see no way to create shared x and y axis labels. In addition, the spacing of the figures is very weird - there is a huge gap between the two figure columns, and in addition a large amount of vertical space before the shared legend. So in sum I would like to be able to create shared x and y axes and minimise the unnecessary vertical and horizontal space.
I checked out the following threads: 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
But I think what I want to do is quite a bit simpler, and I don't know how to combine ggarrange with facet.grid which was suggested a few times.
I have copied a reproducible example below.
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()