我有一些数据,正在尝试制作带有抖动点叠加的箱线图。我的问题在于要点,所以我们会坚持下去。
这是数据:
> dput(test)
structure(list(var1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("A", "B", "C", "D",
"E", "F", "G", "H", "I"), class = "factor"), var2 = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("V1",
"V2", "V3", "V4", "V5", "V6", "V7"), class = "factor"), response1 = c(5L,
6L, 5L, 5L, 5L, 5L, 4L, 6L, 6L, 5L, 5L, 6L, 6L, 4L, 1L, 1L, NA,
1L, NA, NA, 1L, 1L, 1L, NA, 1L, NA, NA, 1L, 5L, 5L, 4L, 5L, 3L,
2L, 3L, 1L, 1L, NA, 1L, NA, NA, 1L, NA, NA, 2L, NA, 3L, 1L, NA,
NA, NA, 4L, NA, 4L, 5L, NA, NA, NA, 1L, NA, 1L, 1L, NA), response2 = c(2L,
2L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 5L, 5L, NA,
5L, NA, NA, 5L, 5L, 5L, NA, 5L, NA, NA, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, NA, 5L, NA, NA, 5L, NA, NA, 5L, NA, 5L, 5L, NA,
NA, NA, 5L, NA, 5L, 5L, NA, NA, NA, 5L, NA, 5L, 5L, NA), response3 = c(4L,
5L, 1L, 1L, 4L, 1L, 1L, 4L, 5L, 1L, 1L, 5L, NA, 1L, 4L, NA, NA,
NA, 3L, 2L, NA, 4L, NA, NA, NA, 3L, NA, NA, 4L, NA, 1L, NA, 3L,
NA, 2L, 4L, NA, NA, NA, NA, NA, NA, NA, 2L, 1L, 1L, NA, NA, 1L,
NA, 3L, 1L, NA, NA, NA, 1L, NA, 3L, 1L, NA, NA, NA, 1L)), .Names = c("var1",
"var2", "response1", "response2", "response3"), class = "data.frame", row.names = c(NA,
-63L))
我曾经reshape2
融化我的数据以对绘图命令进行刻面/简化:
library(reshape2)
test_melted <- melt(test, id.var = c("var1", "var2"), na.rm = T)
这是我创建的情节:
library(ggplot2)
p <- ggplot(test_melted, aes(x = var1, y = value)) + geom_point()
p <- p + facet_grid(~variable) + coord_flip()
p <- p + geom_jitter(position = position_jitter(width=0.2, height = 0.2))
p
这产生了这个:
看起来很正常,但后来我注意到每个方面/因素级别的分数似乎比应有的要多。我缩小到只有一个级别var1
test_subset <- test_melted[test_melted$var1 == "E", ]
nrow(test_subset)
[1] 18
summary(test_subset)
var1 var2 variable value
E :18 V1:3 response1:7 Min. :1
A : 0 V2:2 response2:7 1st Qu.:3
B : 0 V3:3 response3:4 Median :5
C : 0 V4:2 Mean :4
D : 0 V5:3 3rd Qu.:5
F : 0 V6:2 Max. :5
(Other): 0 V7:3
所以,我们应该总共绘制 18 个点(7 代表response1
,7 代表response2
,4 代表response3
。让我们试试吧:
p <- ggplot(test_subset, aes(x = var1, y = value)) + geom_point()
p <- p + facet_grid(~variable) + coord_flip()
p <- p + geom_jitter(position = position_jitter(width=0.2, height = 0.2))
p
我数了response1
刻面 11 个点,8 英寸response2
和 8 英寸response3
。
这一定是我想念的愚蠢的东西。我已经用点图做了很多刻面,但从未发生过这种情况(或从未注意到!)。
我尝试过的事情
- 移除
coord_flip()
test_subset <- droplevels(test_subset)
以防空因子水平弄乱了某些东西- 玩
facet_grid(~variable)
vs.facet_grid(.~variable)
vs.facet_grid(variable~)
vs.facet_grid(variable~.)
最后一点,根据我是否分面,我得到不同数量的分数。使用刻面,我得到11 + 8 + 8 = 27
,如果我删除facet_grid(~variable)
,我得到 23。
感谢您的任何建议!