1

I'd like to create a lattice box plot from data that has multiple factors and multiple levels for each factor.

For each plot in the lattice plot I'd like the x axis to be the combination of both factors and their levels and the y axis to be the values.

Below I've pasted some example data that shows how I can create the lattice plot for the factors but not for the factors and their level combinations. I do not want 3D plots and I'd like to use boxplots if at all possible.

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

Any suggestions would be a huge help!

Nathan
  • 323
  • 3
  • 13
  • Why not use ggplot2 `facet_grid` function. It's much easier. – John Nov 22 '16 at 02:33
  • @John Mostly because I didn't know that existed until right now! I'll check it out and see if it does what I need. Do you happen to know why one would use lattice vs facet_grid? – Nathan Nov 22 '16 at 02:40
  • I've been using mostly ggplot2 for most of my work (even for R Shiny), not really a lattice user. But here is a ref http://stackoverflow.com/questions/2759556/r-what-are-the-pros-and-cons-of-using-lattice-versus-ggplot2 – John Nov 22 '16 at 02:45
  • BTW, I've been adding all kinds of annotation texts to ggplot2 charts and that works really fine. I think ggplot2 is currently the best tool for 2d static plots. For dynamic js charts, I think plotly, rbokeh, ggvis, ggplot2+Rshiny are all good. – John Nov 22 '16 at 02:50
  • So, Ive been trying facet_grid and facet_wrap, but I still can't make the x-axis plot my factors*levels in the same subplot. In the above example code I'd like the x-axis for each plot to read "M*W M*X M*Y M*Z N*W N*X N*Y... etc" For facet_grid and facet_wrap it doesn't seem to support factors and level on the same axis in the same plot. – Nathan Nov 22 '16 at 03:03
  • If `facet_grid(.~Factors+Levels)` doesn't work for you, I believe you still have to manipulate the summary data set(like using `melt` in your case or `dplyr`) for the final plot using either lattice or ggplot2. – John Nov 22 '16 at 03:14
  • You're the man John. Thanks for your help. I've posted the code that worked. I also could have done it using "interactions" command outside of ggplot. It works great now. Thanks for your help. – Nathan Nov 22 '16 at 03:20

2 Answers2

1

With the help of John I got to the below. ggplot with facet_wrap was the way to go. Its fast and easy to do multi-level lattice plots this way.

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")
Nathan
  • 323
  • 3
  • 13
1

Using lattice you can try this:

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

with output enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63