使用 grid.arrange 绘图轴消失

Plot axis disappeared using grid.arrange

我使用 grid.arrange 函数组合了 ggplot 中的两个图。这是我的数据和重现性图(抱歉代码太长)。

数据

A.res.NaKICdAs.ala <- structure(list(Sp.treat. = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Alaria SW", "Saccharina SW"), class = "factor"), Time = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("0", "1", "2", "6", "22"), class = "factor"), Variable = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("I", "Cd", "In_As", "Na", "K"), class = "factor"), Mean = c(36.98467232, 21.68376028, 20.954434679, 20.7031359646667, 19.7695525446667, 0.349925884, 0.294117496666667, 0.276897168, 0.276185600333333, 0.279122115333333, 0.0370796666666667, NA, NA, NA, 0.0190413333333333, 0.680971488333333, NA, NA, NA, 0.729457392666667, 0.724050683333333, NA, NA, NA, 0.608330233666667), SE = c(0.357906321196698, 9.27958124864396, 8.98474351056553, 9.40876289795883, 8.72221484760717, 0.00659470678107073, 0.00918677299717626, 0.00648317710630559, 0.0259496261515117, 0.0075124182419973, 0.00619826312589082, NA, NA, NA, 0.000866792811332545, 0.022967449828317, NA, NA, NA, 0.0181145041679162, 0.0268980616191359, NA, NA, NA, 0.0506355945752796)), .Names = c("Sp.treat.", "Time", "Variable", "Mean", "SE"), row.names = c(1L, 2L, 3L, 4L, 5L, 11L, 12L, 13L, 14L, 15L, 21L, 22L, 23L, 24L, 25L, 31L, 32L, 33L, 34L, 35L, 41L, 42L, 43L, 44L, 45L), class = "data.frame")


A.res.NaKICdAs.sac <- structure(list(Sp.treat. = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Alaria SW", "Saccharina SW"), class = "factor"), Time = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("0", "1", "2", "6", "22"), class = "factor"), Variable = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("I", "Cd", "In_As", "Na", "K"), class = "factor"), Mean = c(810.863352133333, 743.683662433333, 726.950466166667, 701.886173766667, 682.410174266667, 0.035753728, NA, NA, NA, 0.034127785, 0.0260796666666667, NA, NA, NA, 0.0387836666666667, 0.589278043333333, NA, NA, NA, 0.587419524666667, 1.061116211, NA, NA, NA, 0.990471003), SE = c(48.8246699563615, 69.907614192301, 3.77872433099016, 26.2138687732639, 31.989844934656, 0.00480420359657675, NA, NA, NA, 0.0015194237725027, 0.00145939146374249, NA, NA, NA, 0.0108081020586924, 0.0222855614976663, NA, NA, NA, 0.0111485020599773, 0.0857969594362861, NA, NA, NA, 0.0554241104850764)), .Names = c("Sp.treat.", "Time", "Variable", "Mean", "SE"), row.names = c(6L, 7L, 8L, 9L, 10L, 16L, 17L, 18L, 19L, 20L, 26L, 27L, 28L, 29L, 30L, 36L, 37L, 38L, 39L, 40L, 46L, 47L, 48L, 49L, 50L), class = "data.frame")

地块

A.NaKI.ala.plot <- ggplot(subset(A.res.NaKICdAs.ala, Variable %in% c("Na", "K")), aes(x=Time, y=Mean, fill=Variable))
A.NaKI.ala.plot <- A.NaKI.ala.plot + geom_bar(position="dodge", colour="black", stat="identity", width=0.8)
A.NaKI.ala.plot <- A.NaKI.ala.plot + geom_errorbar(position=position_dodge(width=0.8), aes(ymin = Mean - SE, ymax = Mean + SE), linetype = "solid", width=0.3)  # add error bars (do so before geom_point so the points are on top of the error bars)
A.NaKI.ala.plot <- A.NaKI.ala.plot + scale_x_discrete("\nStorage time (h)", breaks = c(0,1,2,6,22)) # have tick marks for each sampling time
A.NaKI.ala.plot <- A.NaKI.ala.plot + scale_y_continuous("% FW\n", limits = c(0,1.2), breaks=seq(0,1.2, by = 0.2), expand = c(0, 0))  
A.NaKI.ala.plot <- A.NaKI.ala.plot + scale_fill_manual(values=c("grey 60","grey 30"), labels=c("Na", "K")) # explicitly have AE_FW=white, AE_SW=black
A.NaKI.ala.plot <- A.NaKI.ala.plot + guides(fill = guide_legend(override.aes = list(colour = NULL))) # Remove slashes in legend
A.NaKI.ala.plot <- A.NaKI.ala.plot + annotate("text", x = 3, y = 1.18, label = "A", family="Times New Roman", fontface="bold", size=3.57)
A.NaKI.ala.plot <- A.NaKI.ala.plot + theme_bw() # make the theme black-and-white rather than grey (do this before font changes, or it overrides them)
A.NaKI.ala.plot <- A.NaKI.ala.plot + labs(title = "\nA. esculenta")
A.NaKI.ala.plot <- A.NaKI.ala.plot + theme(
                                   text=element_text(family="Times New Roman"),
                                   plot.title = element_blank(), # use theme_get() to see available options
                                   axis.text = element_text(size=10),
                                   axis.title.x = element_text(size=10, vjust=0, face="bold"),
                                   axis.title.y = element_text(size=10, angle=90, vjust=1, face="bold"),
                                   axis.line.x = element_line(size = 0.5, colour = "black", linetype = "solid"),
                                   axis.line.y = element_line(size = 0.5, colour = "black", linetype = "solid"),
                                   panel.grid.major = element_blank(), # switch off major gridlines
                                   panel.grid.minor = element_blank(), # switch off minor gridlines
                                   panel.border = element_blank(), # switch off frame
                                   legend.position = "top", # manually position the legend (numbers being from 0,0 at bottom left of whole plot to 1,1 at top right)
                                   legend.title = element_blank(), # switch off the legend title
                                   legend.text = element_text(size=10),
                                   legend.key.size = unit(0.8, "lines"),
                                   legend.key = element_blank() # switch off the rectangle around symbols in the legend
) 
A.NaKI.ala.plot


A.NaKI.sac.plot <- ggplot(subset(A.res.NaKICdAs.sac, Variable %in% c("Na", "K")), aes(x=Time, y=Mean, fill=Variable))
A.NaKI.sac.plot <- A.NaKI.sac.plot + geom_bar(position="dodge", colour="black", stat="identity", width=0.8)
A.NaKI.sac.plot <- A.NaKI.sac.plot + geom_errorbar(position=position_dodge(width=0.8), aes(ymin = Mean - SE, ymax = Mean + SE), linetype = "solid", width=0.3)  # add error bars (do so before geom_point so the points are on top of the error bars)
A.NaKI.sac.plot <- A.NaKI.sac.plot + scale_x_discrete("\nStorage time (h)", breaks = c(0,1,2,6,22)) # have tick marks for each sampling time
A.NaKI.sac.plot <- A.NaKI.sac.plot + scale_y_continuous("\n", limits = c(0,1.2), breaks=seq(0,1.2, by = 0.2), expand = c(0, 0))  
A.NaKI.sac.plot <- A.NaKI.sac.plot + scale_fill_manual(values=c("grey 60", "grey 30"), labels=c("Na", "K")) # explicitly have AE_FW=white, AE_SW=black
A.NaKI.sac.plot <- A.NaKI.sac.plot + guides(fill = guide_legend(override.aes = list(colour = NULL))) # Remove slashes in legend
A.NaKI.sac.plot <- A.NaKI.sac.plot + annotate("text", x = 3, y = 1.18, label = "B", family="Times New Roman", fontface="bold", size=3.57)
A.NaKI.sac.plot <- A.NaKI.sac.plot + theme_bw() # make the theme black-and-white rather than grey (do this before font changes, or it overrides them)
A.NaKI.sac.plot <- A.NaKI.sac.plot + labs(title = "\nS. latissima")
A.NaKI.sac.plot <- A.NaKI.sac.plot + theme(
                                           text=element_text(family="Times New Roman"),
                                           plot.title = element_blank(), # use theme_get() to see available options
                                           axis.text = element_text(size=10),
                                           axis.title.x = element_text(size=10, vjust=0, face="bold"),
                                           axis.title.y = element_text(size=10, angle=90, vjust=1, face="bold"),
                                           axis.line.x = element_line(size = 0.5, colour = "black", linetype = "solid"),
                                           axis.line.y = element_line(size = 0.5, colour = "black", linetype = "solid"),
                                           panel.grid.major = element_blank(), # switch off major gridlines
                                           panel.grid.minor = element_blank(), # switch off minor gridlines
                                           panel.border = element_blank(), # switch off frame
                                           legend.position = "top", # manually position the legend (numbers being from 0,0 at bottom left of whole plot to 1,1 at top right)
                                           legend.title = element_blank(), # switch off the legend title
                                           legend.text = element_text(size=10),
                                           legend.key.size = unit(0.8, "lines"),
                                           legend.key = element_blank() # switch off the rectangle around symbols in the legend
) 
A.NaKI.sac.plot

合并地块

legend.NaKI <- get_legend(A.NaKI.ala.plot) # Save the legend
A.NaKI.ala.plot <- A.NaKI.ala.plot + theme(legend.position="none") # Remove legends from individual plots
A.NaKI.sac.plot <- A.NaKI.sac.plot + theme(legend.position="none")



grid.NaKI <- grid.arrange(legend.NaKI, A.NaKI.ala.plot, A.NaKI.sac.plot,  ncol=2, nrow = 2, 
                          layout_matrix = rbind(c(1,1), c(2,3)),
                          widths = c(2.7, 2.7), heights = c(0.2, 2.5))

当在 grid.plot 中组合时,两个图的 X 和 Y 轴都消失了,尽管它们在分别显示 plot1 和 plot2 时正常显示。

这很奇怪,因为完全相同的代码上个月运行良好。我同时更新了 R 版本,这会是我问题的根源吗?

据我所知,这与 grid.arrange 无关。考虑这个更小的例子,

library(ggplot2)

p <- qplot(1,1) + theme_bw() +  theme(
  axis.line.x = element_line(size = 0.5, colour = "black", linetype = "solid"),
  axis.line.y = element_line(size = 0.5, colour = "black", linetype = "solid"),
  panel.border = element_blank() # switch off frame
) 

p

p <- p + theme(legend.position="none")

p  

可能是 ggplot2 主题系统中的错误(/功能)。