我有一个看起来像这样的宽表:
ID Test_11 LVL11 Score_X_11 Score_Y_11 Test_12 LV12 Score_X_12 Score_Y_12
1 A I 100 NA NA NA 100 100
2 A II 90 100 B II 90 85
3 NA NA NA NA B II 90 NA
4 A III 100 80 A III 75 75
5 B I NA 90 NA NA 60 50
6 B I 70 100 NA NA NA NA
7 B II 85 NA A I 60 60
用来排序的表格看起来像这样
Test_11 A
Test_11 B
Test_12 A
Test_12 B
第二张表告诉我们,对于Test_11,有两个版本,A和B(与Test_12相同)。
我正在尝试创建一系列箱形图,以图形化显示Test_11和Test_12的每种组合及其各自版本(A,B)的分布。因此,对于Test_11 == A,创建的箱线图将具有三个组(I,II,III),然后是来自子集的结果图形信息,其中Test_11 == A,然后对于Test_11 == B,Test_12 == A ,并且Test_12 == B。在此示例中,总共应该创建4个图。
我在R中拥有的是:
z <- subset(df, df$Test_11=="A")
plot(z$LVL11, z$Score_X_11, varwidth = TRUE, notch = TRUE, xlab = 'LVL',
ylab = 'score')
我想要(但无法弄清楚该怎么做)的方法是编写一个for循环,该循环为我做子集,以便我可以为包含几十种这些组合的实际数据集自动执行此操作。
感谢您的帮助和指导。
也许您应该在循环之前将所有逻辑向量保存在data.frame或矩阵中:
selections <- matrix(nrow = nrow(df), ncol = 4)
selections[,1] <- df$Test_11 == "A"
selections[,2] <- df$Test_11 == "B"
selections[,3] <- df$Test_12 == "A"
selections[,4] <- df$Test_12 == "B"
# etc...
par(mfcol = c(2, 2)) # here you should customize at will...
for (i in 1:4) {
z <- subset(df, selections[,i])
plot(z$LVL11, z$Score_X_11, varwidth = TRUE,
notch = TRUE, xlab = 'LVL',
ylab = 'score')
}
您可以更改代码,因此可以使用而不是z$Score_X_11
使用z[,string]
。的值string
应使用paste
(或其他字符串操作函数)构造。例如:
v <- c("X", "Y")
n <- c("11", "12")
for (i in 1:2) {
for (j in 1:2) {
string <- paste("Score", v[i], n[i], sep = "_")
print(string)
}
}
这些z$LVLXX
值将使用类似的推论,因此您应该能够找到一种适合该值的方法。
我对使用网格图形的经验不是很丰富(就像在其他anwser中一样),但是我对ggplot2有所了解,所以我决定接受挑战并尝试一下。它不是很好,但是至少可以起作用:
# df <- read.table("data.txt", header = TRUE, na.string = "NA")
require(reshape2)
require(ggplot2)
# Melt your data.frame, using the scores as the "values":
mdf <- melt(df[,-1], id = c("LVL11", "LV12", "Test_11", "Test_12"))
# loop through level types:
for (lvl in c("LVL11", "LV12")) {
# looping through values of test11
for (test11 in c("A", "B")) {
# Note the use of subset before ggplot
p <- ggplot(subset(mdf, Test_11 == test11), aes_string(x=lvl, y="value"))
# I added the geom_jitter as in the example given there were only a few points
g <- p + geom_boxplot(aes(fill = variable)) + geom_jitter(aes(shape = variable))
print(g) # it is necessary to print p explicitly like this in order to use ggplot in a loop
# Finally, save each plot with a relevant name:
savePlot(paste0(lvl, "-t11", test11, ".png"))
# (note that savePlot has some problems with RStudio iirc)
}
# Same as before, but with test_12
for (test12 in c("A", "B")) {
p <- ggplot(subset(mdf, Test_12 == test12), aes_string(x=lvl, y="value"))
g <- p + geom_boxplot(aes(fill = variable)) + geom_jitter(aes(shape = variable))
print(g)
savePlot(paste0(lvl, "-t12", test12, ".png"))
}
}
如果有人知道如何使用网格图形,或者facet_grid
在这种情况下,那么我可以将所有grahpics放在一张图像中,我很想听听如何使用。
干杯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句