2016-11-22 2 views
1

Ich möchte ein Gitter-Box-Diagramm aus Daten erstellen, die mehrere Faktoren und mehrere Ebenen für jeden Faktor hat.Zwei-Faktor-Gitter-Boxplots in R

Für jedes Diagramm im Gitterdiagramm möchte ich, dass die x-Achse die Kombination beider Faktoren und ihrer Ebenen und die y-Achse die Werte darstellt.

Unten habe ich einige Beispieldaten eingefügt, die zeigen, wie ich das Gitterdiagramm für die Faktoren erstellen kann, aber nicht für die Faktoren und ihre Ebenenkombinationen. Ich möchte keine 3D-Plots und möchte Boxplots verwenden, wenn es überhaupt möglich ist.

library(plyr) 
library(reshape2) 
library(lattice) 

col_t <- c("Factors","Levels",LETTERS[1:10]) 
data1 <- rnorm(1000) 
dm <- matrix(data1, nrow = 100, ncol = 10, byrow = TRUE) 
    df <- data.frame(dm) 

facs <- c(rep("M",25), rep("N",25), rep("O",25), rep("P",25)) 
levs <- c(rep(c("W","x","Y","Z"),25)) 
    df <- cbind(facs,levs,df) 
     colnames(df) <- col_t 

dfm <- melt(df, id.vars=c("Factors", "Levels")) 
    head(dfm) 

# Creates the Lattice plot where the rnorm data is on the y-axis and the A factors are 
# on the x-axis for every Variable in variable  
    All_Graph <- bwplot(value ~ Factors | variable, 
         data=dfm, 
         scales=list(relation="free",x=list(cex=1.1),y=list(cex=1.25)), 
         main= list("All Metric Values", cex=2.5), 
         xlab=list("Treatments", cex=2.5), 
         ylab=list("Metric Value", cex=2.5), 
         do.out = FALSE, 
         col="black", 
         coef=4 
    ) 
    trel_wid <- 960 
    trellis.device(device="png", filename="All Var Plots.png", width= trel_wid, height= trel_wid*1.5) 
    print(All_Graph) 
    dev.off() 

# Now I'd like to create a plot of each level of each factor. Where the x-axis is A*B 
# and the y-axis is the rnorm data 

    All_Graph <- bwplot(value ~ Factors*Levels | variable, 
         data=dfm, 
         scales=list(relation="free",x=list(cex=1.1),y=list(cex=1.25)), 
         main= list("All Metric Values", cex=2.5), 
         xlab=list("Treatments", cex=2.5), 
         ylab=list("Metric Value", cex=2.5), 
         do.out = FALSE, 
         col="black", 
         coef=4 
    ) 
    trel_wid <- 960 
    trellis.device(device="png", filename="All Var+Lev Plots.png", width= trel_wid, height= trel_wid*1.5) 
    print(All_Graph) 
    dev.off() 

Alle Vorschläge wären eine große Hilfe!

+0

Warum ggplot2 nicht 'facet_grid' Funktion verwenden. Es ist viel einfacher. – John

+0

@John Hauptsächlich, weil ich nicht wusste, dass es bis jetzt existierte! Ich werde es überprüfen und sehen, ob es das tut, was ich brauche. Weißt du zufällig, warum man Gitter gegen facet_grid verwendet? – Nathan

+0

Ich benutze meistens ggplot2 für die meisten meiner Arbeit (auch für R Shiny), nicht wirklich ein Gitter Benutzer. Aber hier ist eine Referenz http://stackoverflow.com/questions/2759556/r-what-are-the-pros-and-cons-of-using-lattice-versus-ggplot2 – John

Antwort

1

Mit Hilfe von John kam ich nach unten. ggplot mit facet_wrap war der richtige Weg. Es ist schnell und einfach, Multi-Level-Gitter-Plots auf diese Weise zu tun.

library(plyr) 
library(reshape2) 
library(lattice) 

col_t <- c("Factors","Levels",LETTERS[1:10]) 
data1 <- rnorm(1000) 
dm <- matrix(data1, nrow = 100, ncol = 10, byrow = TRUE) 
    df <- data.frame(dm) 

facs <- c(rep("M",25), rep("N",25), rep("O",25), rep("P",25)) 
levs <- c(rep(c("W","x","Y","Z"),25)) 
    df <- cbind(facs,levs,df) 
     colnames(df) <- col_t 

dfm <- melt(df, id.vars=c("Factors", "Levels")) 
    head(dfm) 


    sp <- ggplot(dfm, aes(x=Factors, y=value, fill=Levels)) + 
      geom_boxplot(coef=4, outlier.shape = NA, position = "dodge", alpha = 1, 
        lwd = 1, fatten = 0.75) 

    sp + facet_wrap(~variable, ncol=3, scales="free") 
1

Mit Gitter können Sie dies versuchen:

All_Graph <- bwplot(value ~ Factors : Levels | variable,  # use interaction 
        data=dfm, 
        scales=list(relation="free",x=list(cex=1.1),y=list(cex=1.25)), 
        main= list("All Metric Values", cex=2.5), 
        xlab=list("Treatments", cex=2.5), 
        ylab=list("Metric Value", cex=2.5), 
        do.out = FALSE, 
        col="black", 
        coef=4 
) 
trel_wid <- 1500 
trellis.device(device="png", filename="All Var+Lev Plots.png", width= trel_wid, height= trel_wid*1.5) 
print(All_Graph) 
dev.off() 

mit Ausgang enter image description here