R中的二因子格盒图
我想从具有多个因子和每个因子的多个级别的数据创建一个格子盒图 对于格子图中的每个图,我希望x轴是两个因子及其级别的组合,y轴是值 下面我粘贴了一些示例数据,展示了如何为因子创建晶格图,而不是为因子及其级别组合创建晶格图。我不想要3D绘图,如果可能的话,我想使用箱线图R中的二因子格盒图,r,boxplot,lattice,trellis,R,Boxplot,Lattice,Trellis,我想从具有多个因子和每个因子的多个级别的数据创建一个格子盒图 对于格子图中的每个图,我希望x轴是两个因子及其级别的组合,y轴是值 下面我粘贴了一些示例数据,展示了如何为因子创建晶格图,而不是为因子及其级别组合创建晶格图。我不想要3D绘图,如果可能的话,我想使用箱线图 library(plyr) library(reshape2) library(lattice) col_t <- c("Factors","Levels",LETTERS[1:10]) data1 <- rnorm
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()
库(plyr)
图书馆(E2)
图书馆(格子)
在约翰的帮助下,我到达了下面。ggplot和facet_wrap是一种方式。用这种方法进行多级点阵绘图既快速又简单
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")
库(plyr)
图书馆(E2)
图书馆(格子)
col\u t使用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()
All\u Graph为什么不使用ggplot2facet\u grid
函数。“这容易多了!”约翰说,主要是因为我直到现在才知道它的存在!我会检查一下,看看它是否符合我的需要。你知道为什么要使用lattice vs facet_grid吗?我的大部分工作(甚至是R Shining)都是使用ggplot2,而不是真正的lattice用户。但这里有一个参考,顺便说一句,我一直在为ggplot2图表添加各种注释文本,效果非常好。我认为ggplot2是目前用于二维静态绘图的最佳工具。对于动态js图表,我认为plotly、rbokeh、ggvis、ggplot2+Rshiny都很好。因此,我一直在尝试facet_grid和facet_wrap,但我仍然无法将x轴绘图作为同一子图中的因子级别。在上面的示例代码中,我希望每个绘图的x轴为“MW MX MY MZ NW NX NY…etc”,对于facet_网格和facet_wrap,它似乎不支持同一绘图中同一轴上的因子和级别。