0

I am trying to arrange multiple faceted ggplots into a multi-paneled figure. I have 1 plot that isn't faceted because we don't have data to compare across species as we do in the others. Below is a heavily-subsetted dataframe and code to make some figures.

structure(list(ParentID = c(21L, 49L, 50L, 56L, 57L, 57L, 59L, 
60L, 71L, 72L), TransplantSite = structure(c(1L, 2L, 2L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Inner Bay", "Outer Bay"), class = 
"factor"), OriginSite = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L), .Label = c("Inner Bay", "Outer Bay"), class = "factor"), 
Species = c("Montipora capitata", "Porites compressa", "Porites compressa", 
"Porites compressa", "Porites compressa", "Porites compressa", 
"Porites compressa", "Porites compressa", "Montipora capitata", 
"Montipora capitata"), RSA = c(0.003838661, 0.010402833, 
0.013252558, 0.010224238, 0.008994362, 0.010745713, 0.010207059, 
0.010011026, 0.004861123, 0.007916145), P_R = c(3.492398909, 
3.48193224, 3.264889992, 3.310263269, 2.791196059, 3.283083918, 
2.860216513, 3.173035428, 3.939930586, 3.20990334), LE06 = c(0.013694445, 
0.01033, 0.009460571, 0.008771429, 0.005004624, 0.007824762, 
0.003647399, 0.007105714, 0.015163743, 0.009919075)), row.names = c(1L, 
14L, 16L, 26L, 27L, 28L, 31L, 34L, 37L, 40L), class = "data.frame")

R06 = ggplot(G, aes(x = TransplantSite, y = RSA, color = OriginSite)) + 
geom_line(aes(group = ParentID), size = 0.4, alpha = 0.4) + 
stat_summary(aes(group = OriginSite), fun.y = mean, geom = "point", size = 
1.25, position = position_dodge(width = 0.1)) +
stat_summary(aes(group = OriginSite), fun.y = mean, geom = "line", size = 0.8, 
position = position_dodge(width = 0.1)) + 
stat_summary(aes(group = OriginSite), fun.data = mean_se, geom = "errorbar", 
width = 0.1, size = 0.8, position = position_dodge(width = 0.1)) +  
scale_y_continuous(limits = c(0, 0.015)) + 
scale_color_manual(values = c("Inner Bay" = "coral2", "Outer Bay" = 
"skyblue4")) 
R06 = R06 + theme(aspect.ratio = 1, axis.text=element_text(size=9, color = 
"black"), axis.title=element_text(size=10), plot.title = element_text(hjust = 
0.5, size = 9, face = "italic"), panel.grid.major = element_blank(), 
panel.grid.minor = element_blank(), panel.background = element_blank(), 
panel.border = element_blank(), axis.line = element_line(colour = "black"),
legend.position = "none", axis.text.x = element_blank(), axis.title.x = 
element_blank()) + 
scale_fill_manual(values=alpha(c("coral2", "skyblue4")), name = "Origin Site", 
labels = c("Inner Bay Origin", "Outer Bay Origin")) + 
labs(y = expression(atop("Respiration", paste((O["2"]~cm^-2~day^-1%+-%SEM)))), 
x = "Transplant Site", color = "Origin Site", title = "")
R06 = R06 + facet_grid(cols = vars(Species)) + theme(strip.text.x = 
element_blank())
R06

PR06 = ggplot(Genos, aes(x = TransplantSite, y = P_R, color = OriginSite)) + 
geom_line(aes(group = ParentID), size = 0.4, alpha = 0.4) + 
stat_summary(aes(group = OriginSite), fun.y = mean, geom = "point", size = 
1.25, position = position_dodge(width = 0.1)) +
stat_summary(aes(group = OriginSite), fun.y = mean, geom = "line", size = 0.8, 
position = position_dodge(width = 0.1)) + 
stat_summary(aes(group = OriginSite), fun.data = mean_se, geom = "errorbar", 
width = 0.1, size = 0.8, position = position_dodge(width = 0.1)) +  
scale_y_continuous(limits = c(2.5, 4)) + 
scale_color_manual(values = c("Inner Bay" = "coral2", "Outer Bay" = 
"skyblue4"))  
PR06 = PR06 + theme(aspect.ratio = 1, axis.text=element_text(size=9, color = 
"black"), axis.title=element_text(size=10), plot.title = element_text(hjust = 
0.5, size = 9, face = "italic"), 
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
panel.background = element_blank(), panel.border = element_blank(), axis.line = 
element_line(colour = "black"),
legend.position = "none") + 
scale_fill_manual(values=alpha(c("coral2", "skyblue4")), name = "Origin Site", 
labels = c("Inner Bay Origin", "Outer Bay Origin")) + 
labs(y = "P:R", x = "Transplant Site", color = "Origin Site", title = "")
PR06 = PR06 + facet_grid(cols = vars(Species)) + theme(strip.text.x = 
element_blank())
PR06

LE06 = ggplot(Genos, aes(x = TransplantSite, y = LE06, color = OriginSite)) + 
geom_line(aes(group = ParentID), size = 0.4, alpha = 0.4) + 
stat_summary(aes(group = OriginSite), fun.y = mean, geom = "point", size = 
1.25, position = position_dodge(width = 0.1)) +
stat_summary(aes(group = OriginSite), fun.y = mean, geom = "line", size = 0.8, 
position = position_dodge(width = 0.1)) + 
stat_summary(aes(group = OriginSite), fun.data = mean_se, geom = "errorbar", 
width = 0.1, size = 0.8, position = position_dodge(width = 0.1)) +  
scale_y_continuous(limits = c(0, 0.02)) + 
scale_color_manual(values = c("Inner Bay" = "coral2", "Outer Bay" = 
"skyblue4")) 
LE06 = LE06 + theme(aspect.ratio = 1, axis.text=element_text(size=9, color = 
"black"), axis.title=element_text(size=10), plot.title = element_text(hjust = 
0.5, size = 9, face = "italic"), 
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
panel.background = element_blank(), panel.border = element_blank(), axis.line = 
element_line(colour = "black"),
legend.position = "none", axis.text.x = element_blank(), axis.title.x = 
element_blank()) + 
scale_fill_manual(values=alpha(c("coral2", "skyblue4")), name = "Origin Site", 
labels = c("Inner Bay Origin", "Outer Bay Origin")) + 
labs(y = expression(atop("Linear Extension", paste((cm~day^-1%+-%SEM)))), x = 
"Transplant Site", color = "Origin Site", title = "")
LE06 = LE06 + facet_grid(cols = vars(Species)) + theme(strip.text.x = 
element_blank())
LE06

structure(list(TransplantSite = structure(c(1L, 1L, 2L, 2L), .Label = c("Inner 
Bay", "Outer Bay"), class = "factor"), OriginSite = structure(c(1L, 
2L, 1L, 2L), .Label = c("Inner Bay", "Outer Bay"), class = "factor"), 
Prop = c(0.5, 0.9, 0.9, 1)), class = "data.frame", row.names = c(NA, 
-4L))

MCAPSP = ggplot(PropSpawn, aes(x = TransplantSite, y = Prop, color = 
OriginSite)) + 
geom_line(aes(group = OriginSite), size = 0.8) + 
geom_point(aes(group = OriginSite), size = 1.25) +
scale_y_continuous(limits = c(0.5, 1)) + 
scale_color_manual(values = c("Inner Bay" = "coral2", "Outer Bay" = 
"skyblue4")) 
MCAPSP = MCAPSP + theme(aspect.ratio = 1, axis.text=element_text(size=9, color 
= "black"), axis.title=element_text(size=10), plot.title = element_text(hjust = 
0.5, size = 9, face = "italic"), 
panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
panel.background = element_blank(), panel.border = element_blank(), axis.line = 
element_line(colour = "black"),
legend.position = "none") + 
scale_fill_manual(values=alpha(c("coral2", "skyblue4")), name = "Origin Site", 
labels = c("Inner Bay Origin", "Outer Bay Origin")) + 
labs(y = "Proportion Spawned", x = "Transplant Site", color = "Origin Site", 
title = "")
MCAPSP

Below is the code used to generate the figure attached. As you can see, the bottom row is where it gets messed up. I need the faceted figure to align with those above and the single plot to align with Montipora capitata (left plot in pairs).

plots <- list(BM06, S06, GP06, GR06, R06, LE06, PR06, MCAPSP)
grobs <- list()
widths <- list()
for (i in 1:length(plots)){
grobs[[i]] <- ggplotGrob(plots[[i]])
widths[[i]] <- grobs[[i]]$widths[2:5]
}
maxwidth <- do.call(grid::unit.pmax, widths)
for (i in 1:length(grobs)){
grobs[[i]]$widths[2:5] <- as.list(maxwidth)
}
do.call("grid.arrange", c(grobs, ncol = 2))

Multipanel figure with alignment issue

  • 1
    Some newer ggplot layout packages like `patchwork` have the ability to easily align plots by axes. However, we don't have your data or the code involved in making plots. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R post folks can more easily help with – camille Jun 06 '19 at 02:44
  • Yes, there's a few different things I can think of to try here, but I don't have the time to construct a series of example plots myself, so a copy-pasteable example would be great. – Marius Jun 06 '19 at 02:56
  • Not sure how to add plots in a simple way, but I've added subsetted code to make 4 plots. Hope this helps and thanks in advance! –  Jun 06 '19 at 03:19

0 Answers0