我正在尝试将多个多面ggplots安排到一个多面板图中。我有1个未刻面的图,因为我们没有像其他物种那样可以跨物种进行比较的数据。下面是一个繁琐的数据框和编写一些数字的代码。
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
下面是用于生成附加图形的代码。如您所见,最下面一行是弄乱的地方。我需要将刻面图与上面的图对齐,并将单个图与Montipora capitata(成对的左侧图)对齐。
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))