I'm having an issue trying to reduce the margins around my plots in plot_grid.
Whereas defining margins within each plot reduces the whitespace as desired, but leaves the y axes misaligned due to their different sizes, like so:
I've included a reproducible example below.
Thanks in advance for any help on this matter -- I appreciate the work you're doing with cowplot.
#my data
mydat <- structure(list(sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"),
treat = structure(c(3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L,
2L, 2L), .Label = c("nylon", "stab", "no treat"), class = "factor"),
pre_post = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L), .Label = c("pre", "post"), class = "factor"),
meanPO = c(0.271111104444444, 2.32355555333333, 0.288222211111111,
0.15, 0.254444435555555, 1.86595237619048, 0.164901954901961,
0.247333333333333, 0.794901970588235, 0.6429166625, 0.390196078431373,
0.339777768888889), meanproPO = c(1.17066665333333, 3.96755554888889,
1.68466666666667, 0.750888877777778, 1.11022221777778, 2.90190476428571,
0.269411770588235, 0.493777764444444, 0.790392158823529,
0.926458333333333, 0.529583329166667, 0.803111106666667)), .Names = c("sex",
"treat", "pre_post", "meanPO", "meanproPO"), class = "data.frame", row.names = c(13L,
15L, 17L, 19L, 21L, 23L, 1L, 3L, 5L, 7L, 9L, 11L))
#individual plots
#set common themes
my_theme <- theme(
legend.position="none",
axis.line = element_line(colour = "black")
)
#plot 1
gg_TL <- ggplot(subset(mydat, sex=="F"), aes(x=treat, y=meanPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(x="") +
my_theme + theme(
axis.text.x = element_blank()
)
#plot 2
gg_BL <- ggplot(subset(mydat, sex=="F"), aes(x=treat, y=meanproPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(x="") +
my_theme
#plot 3
gg_TR <- ggplot(subset(mydat, sex=="M"), aes(x=treat, y=meanPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(x="", y="") +
my_theme + theme(
axis.text.x = element_blank()
)
#plot 4
gg_BR <- ggplot(subset(mydat, sex=="M"), aes(x=treat, y=meanproPO, fill=pre_post)) +
geom_bar(position="dodge", stat="identity", width=0.75) +
scale_y_continuous(limits=c(0,my_ylim), expand=c(0,0) ) +
labs(y="", x="") +
my_theme
# cowplot
# Plot #1: hv align
plot_grid(gg_TL, gg_TR, gg_BL, gg_BR, labels = LETTERS[1:4], label_size = 18, align="hv")
# Plot #2: define margins of each plot, no hv align
gg_TL <- gg_TL + theme(plot.margin=unit(c(0.6,-0.2,-0.5,0.2), "cm"))
gg_BL <- gg_BL + theme(plot.margin=unit(c(0.6,-0.2,-0.5,0.2), "cm"))
gg_TR <- gg_TR + theme(plot.margin=unit(c(0.4,0.2,-0.5,0.2), "cm"))
gg_BR <- gg_BR + theme(plot.margin=unit(c(0.4,0.2,-0.5,0.2), "cm"))
plot_grid(gg_TL, gg_TR, gg_BL, gg_BR, labels = LETTERS[1:4], label_size = 18)