gpt4 book ai didi

r - ggplot2 绘制比数据框中更多的点,geom_point + facet_grid

转载 作者:行者123 更新时间:2023-12-04 10:57:26 25 4
gpt4 key购买 nike

我有一些数据,正在尝试制作带有抖动点叠加的箱线图。我的问题是分数,所以我们会坚持下去。

这是数据:

> 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

这产生了这个:

enter image description here

看起来很正常,但后来我注意到每个方面/因素级别的点数似乎比应有的要多。我将 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 个点(response1 7 个,response2 7 个,response3 4 个。让我们试试看:

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

enter image description here

我在 response1 中数了 11 分刻面,8 英寸 response2 , 和 8 在 response3 .

这一定是我想念的愚蠢的东西。我已经用点图做了很多分面,但从未发生过(或从未注意到!)。

我尝试过的事情

  • 删除 coord_flip()
  • test_subset <- droplevels(test_subset)以防空因子水平出现问题
  • facet_grid(~variable)facet_grid(.~variable)facet_grid(variable~)facet_grid(variable~.)

最后一点,根据我是否分面,我得到不同数量的分数。通过分面,我得到 11 + 8 + 8 = 27 ,如果我删除 facet_grid(~variable) ,我得到 23。

感谢您的任何建议!

最佳答案

问题不是因为分面,而是因为在您的绘图中使用了两个几何图形。所以 geom_point 会在一个地方绘制所有点,然后 geom_jitter 会在随机位置再次绘制它们。这就是为什么您可以在每个图中多看到一个点。

如果您删除对 geom_point 的调用,一切都会恢复正常:

p <- ggplot(test_subset, aes(x = var1, y = value))
p <- p + facet_grid(~variable) + coord_flip()
p <- p + geom_jitter(position = position_jitter(width=0.2, height = 0.2))
p

enter image description here

关于r - ggplot2 绘制比数据框中更多的点,geom_point + facet_grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14677352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com