1

我想从具有多个因素和每个因素的多个水平的数据中创建一个格子箱图。

对于格子图中的每个图,我希望 x 轴是两个因素及其水平的组合,而 y 轴是值。

下面我粘贴了一些示例数据,这些数据显示了如何为因子创建格点图,但不能为因子及其水平组合创建格子图。我不想要 3D 图,如果可能的话,我想使用箱线图。

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

任何建议都会有很大帮助!

4

2 回答 2

1

使用 lattice 你可以试试这个:

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

带输出 在此处输入图像描述

于 2016-11-22T05:26:22.297 回答
1

在约翰的帮助下,我到了下面。带有 facet_wrap 的 ggplot 是要走的路。通过这种方式可以快速轻松地绘制多级格子图。

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")
于 2016-11-22T03:18:52.650 回答