2017-04-19 3 views
0

Ich habe zwei Datenbanken in verschiedenen Größen, dt und dt1. Ich möchte den Befehl grid.arrange aus dem gridExtra Paket verwenden, um und g2 Seite an Seite anzuzeigen. Wenn es möglich ist, würde ich auch gerne und g2 mit dem facet_grid oder facet_wrap Befehl oder mit gridExtra aber mit der von facet_grid\facet_wrap Visual sehen. Ich habe eine lange Suche im Internet durchgeführt und konnte diese Grafiken nicht mit meinem Code unten erhalten.Graphen nebeneinander mit `gridExtra` und mehreren Facetten

set.seed(000) 
m <- matrix(rnorm(1000,0,1),1000,1) 
dt <- data.frame(m) 
names(dt) <- c("X") 

library(ggplot2) 

g1 <- ggplot(dt, aes(x=X)) 
g1 <- g1+geom_histogram(aes(y=..density..),  # Histogram with density instead of count on y-axis 
         binwidth=.5, 
         colour="black", fill="white",breaks=seq(-2, 2, by = 0.1)) 
g1 <- g1 + stat_function(fun=dnorm, 
         color="black",geom="area", fill="gray", alpha=0.1, 
         args=list(mean=mean(dt$X), 
            sd=sd(dt$X))) 
g1 <- g1+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE) 
g1 <- g1+ geom_vline(aes(xintercept=mean(dt$X, na.rm=T), linetype="Valor Estimado"),show.legend =TRUE) 
g1 <- g1+ scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot 
g1 <- g1+ xlab(expression(paste(gamma[1])))+ylab("Densidade") 
g1 <- g1+ theme(plot.margin=unit(c(0.5, 0.5, 0.5, 0.5), units="line"), 
       legend.position = "top", 
       legend.justification = c(0,0), 
       legend.box.just = "top", 
       legend.margin = margin(0,0,-10,-5), 
       legend.title=element_blank(), 
       legend.direction = "horizontal", 
       legend.background = element_rect(fill="transparent", size=.5, linetype="dotted")) 
g1 <- g1+ guides(linetype = guide_legend(override.aes = list(size = 1))) 


# Adjust key height and width 
g1 = g1 + theme(
    legend.key.height = unit(.6, "cm"), 
    legend.key.width = unit(1, "cm")) 

# Get the ggplot Grob 
gt = ggplotGrob(g1) 

# grid.ls(grid.force(gt)) # To get a list of editable grobs 

# Edit the relevant keys 
library(grid) 
gt <- editGrob(grid.force(gt), gPath("key-1-[3,7]-[1,2]"), 
       grep = TRUE, global = TRUE, 
       x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
       x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

# Draw it 
grid.newpage() 
g1 <- grid.draw(gt) 

m1 <- matrix(rnorm(2000,0,1),2000,1) 
dt1 <- data.frame(m1) 
names(dt1) <- c("Z") 
library(ggplot2) 

g2 <- ggplot(dt1, aes(x=Z)) 
g2 <- g2+geom_histogram(aes(y=..density..),  # Histogram with density instead of count on y-axis 
         binwidth=.5, 
         colour="black", fill="white",breaks=seq(-2, 2, by = 0.1)) 
g2 <- g2 + stat_function(fun=dnorm, 
         color="black",geom="area", fill="gray", alpha=0.1, 
         args=list(mean=mean(dt1$Z), 
            sd=sd(dt1$Z))) 
g2 <- g2+ geom_vline(aes(xintercept=0, linetype="Valor Verdadeiro"),show.legend =TRUE) 
g2 <- g2+ geom_vline(aes(xintercept=mean(dt1$Z, na.rm=T), linetype="Valor Estimado"),show.legend =TRUE) 
g2 <- g2+ scale_linetype_manual(values=c("dotdash","solid")) # Overlay with transparent density plot 
g2 <- g2+ xlab(expression(paste(gamma[1])))+ylab("Densidade") 
g2 <- g2+ theme(plot.margin=unit(c(0.5, 0.5, 0.5, 0.5), units="line"), 
       legend.position = "top", 
       legend.justification = c(0,0), 
       legend.box.just = "top", 
       legend.margin = margin(0,0,-10,-5), 
       legend.title=element_blank(), 
       legend.direction = "horizontal", 
       legend.background = element_rect(fill="transparent", size=.5, linetype="dotted")) 
g2 <- g2+ guides(linetype = guide_legend(override.aes = list(size = 1))) 


# Adjust key height and width 
g2 = g2 + theme(
    legend.key.height = unit(.6, "cm"), 
    legend.key.width = unit(1, "cm")) 

# Get the ggplot Grob 
gt2 = ggplotGrob(g2) 

# grid.ls(grid.force(gt)) # To get a list of editable grobs 

# Edit the relevant keys 
library(grid) 
gt2 <- editGrob(grid.force(gt2), gPath("key-1-[3,7]-[1,2]"), 
       grep = TRUE, global = TRUE, 
       x0 = unit(0, "npc"), y0 = unit(0.5, "npc"), 
       x1 = unit(1, "npc"), y1 = unit(0.5, "npc")) 

# Draw it 
grid.newpage() 
g2 <- grid.draw(gt2) 

#library(gridExtra) 
#grid.arrange(grid.draw(gt),grid.draw(gt2)) 
+0

ich verstehe die Frage nicht, können Du machst es bitte klarer? ("aber mit der von") – baptiste

Antwort

1

Macht dies, was Sie suchen grid.arrange(gt, gt2, ncol = 2)?

(g1 und g2 in Ihrem Code sind beide NULL weil man sie durch den Aufruf grid.draw erstellt werden, was Passt nicht alles zurück)

facet_wrap nutzen zu können, müssen Sie alle Ihre Daten in einem Datenrahmen erhalten mit lang~~POS=TRUNC:

library(tidyr) 
df <- cbind.data.frame(dt, dt1) 
df <- gather(df, key = "db", value = "value") 

Dann Grundstück:

p <- ggplot(df, aes(x = value)) + 
geom_histogram(aes(y = ..density..), 
       binwidth = .5, 
       breaks = seq(-2, 2, by = .1)) + 
facet_wrap(~ db) 
+0

Danke @wibom! Ich war in der Lage, mein Problem mit Ihrer Hilfe zu lösen! – fsbmat

Verwandte Themen