2016-11-22 9 views
1

여러 요인과 각 요인에 대해 여러 수준이있는 데이터에서 격자 상자 그림을 만들고 싶습니다.두 팩터 격자 boxplots in R

격자 플롯의 각 플롯에 대해 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() 

어떤 제안이라도 도움이 될 것입니다.

+0

출력 와 함께. 훨씬 쉽습니다. – John

+0

@ 존 대부분은 내가 지금까지 존재했는지 몰랐기 때문에! 나는 그것을 체크하고 그것이 내가 필요한 것을하는지 볼 것이다. 왜 대 격자 대 facet_grid를 사용하는지 알고 있습니까? – Nathan

+0

저는 주로 격자 사용자가 아니라 대부분의 작업 (심지어 R Shiny)에서도 ggplot2를 주로 사용했습니다. 하지만 여기에 참조 http://stackoverflow.com/questions/2759556/r-what-are-the-pros-and-cons-of-using-lattice-versus-ggplot2 – John

답변

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

이 시도 할 수 있습니다 격자를 사용하여 :

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

ggplot2`facet_grid` 기능을 사용하지 왜 enter image description here