2017-01-20 4 views
0

Ich habe zwei Plots von ggplot mit der Funktion grid.arrange kombiniert. Hier sind meine Daten und die Plots für Reproduzierbarkeit (sorry für sperrigen Code).Die Plotachse verschwand mit grid.arrange

Daten

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") 

Stücke

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 

Kombination Plots

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)) 

Wenn im grid.plot die X- und Y-Achse der beiden Parzellen verschwunden kombiniert, obwohl sie in der Regel zeigen wenn plot1 und plot2 getrennt angezeigt werden.

Dies ist seltsam, da der gleiche Code letzten Monat einwandfrei funktionierte. Ich habe die R-Version in der Zwischenzeit aktualisiert, kann dies die Ursache meines Problems sein?

+1

Schwieriger zu antworten mit nicht reproduzierbarem Beispielcode (was sind 'legend.plot', etc.)? – setempler

+0

Kann nicht ohne reproduzierbares Beispiel antworten, aber Sie brauchen 'ncol' und' nrow' nicht, da Sie die 'layout_matrix' präzisieren – timat

+0

Ich habe den Beitrag mit den Daten und dem Code aktualisiert, den ich tatsächlich verwende. – Pierrick

Antwort

0

Es hat nichts mit grid.arrange zu tun, soweit ich das beurteilen kann. Betrachten Sie dieses minimalistische Beispiel

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 

enter image description here

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

p 

enter image description here

Vermutlich einen Fehler (/ Feature) im ggplot2 Thema System.

+0

Es hat wahrscheinlich mit der Deaktivierung von legende.position zu tun, obwohl es die Achse beeinflussen sollte. Vielen Dank – Pierrick

Verwandte Themen