抖动的geom_line和geom_point

3
我有一个数据集,其中geom_line和geom_point存在显著重叠,我希望对它们进行微调。在过去,这个解决方案(如何同时微调geom_line和geom_point?)对我很有用,但出于某种原因,我无法在此数据集上使用它。
以下是我的数据:
dput(test)
structure(list(individual_code = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 
12L, 12L, 12L, 12L, 12L, 12L), .Label = c("852", "858", "860", 
"876", "879", "881", "883", "893", "908", "927", "940", "945"
), class = "factor"), storage_temp = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("23", "7"), class = "factor"), time_point = c(12, 
14, 16, 18, 20, 22, 12, 14, 16, 18, 20, 22, 12, 14, 16, 18, 20, 
22, 12, 14, 16, 18, 20, 22, 12, 14, 16, 18, 20, 22, 12, 14, 16, 
18, 20, 22, 12, 14, 16, 18, 20, 22, 12, 14, 16, 18, 20, 22, 12, 
14, 16, 18, 20, 22, 14, 16, 18, 20, 22, 12, 14, 16, 18, 20, 22, 
12, 14, 16, 18, 20, 22), Q10 = c(13.6570247933884, 2.96984924623116, 
3.60106382978723, 2.18942731277533, 2.8705035971223, 0.966304347826087, 
4.57042253521127, 2.59362549800797, 7.48427672955975, 2.98943661971831, 
4.6779303062302, 5.2092050209205, 0.583657587548638, 0.38768115942029, 
0.891025641025641, 1.63664596273292, 3.39024390243902, 0.964705882352941, 
2.07727272727273, 3.35548172757475, 2.61574074074074, 2.32717678100264, 
4.74789915966386, 3.40068493150685, 2.48206599713056, 2.24120603015075, 
4.00229357798165, 2.55284552845528, 2.01652892561983, 1.6096256684492, 
2.93939393939394, 2.57309941520468, 2.34586466165414, 3.35555555555556, 
3.10169491525424, 3.44052863436123, 6.94339622641509, 6.48412698412698, 
3.68548387096774, 3.05286343612335, 2.67857142857143, NA, 1.82513661202186, 
2.66666666666667, 2.63414634146341, 1.93916349809886, 2.07174887892377, 
2.01941747572815, 6.82882882882883, 4.05882352941176, 2.54929577464789, 
2.27272727272727, 2.15730337078652, 2.65060240963855, 3.5531914893617, 
1.91964285714286, NA, NA, 5.15566625155666, 5.70212765957447, 
7.19266055045872, 4.97814207650273, 4, 6.12162162162162, 4.1701030927835, 
6.078125, 2.33333333333333, 2.91836734693878, 2.42307692307692, 
2.33152173913043, 1.75)), row.names = c(NA, -71L), class = "data.frame")

我的工作代码有太多的重叠部分:

library(ggplot2)

ggplot(data = Q10_summary, aes(
      x = time_point, y = Q10, color = storage_temp
    )) +
    Alex_Theme +
    geom_point(
      data = therm_sen_final,
      aes(time_point, Q10, group = storage_temp),
      alpha = 0.25,
      color = 'black',
      #position = position_jitter(width = 0.25)
    ) +
    geom_line(
      data = therm_sen_final,
      aes(group = individual_code, linetype = storage_temp),
      color = 'black',
      alpha = 0.25,
      #position = position_jitter(width = 0.25)
    )

看起来是这样的:

输入图像描述

然后当我包含position_jitter()时(在上面的代码中被注释掉),我得到了这个:

输入图像描述


1
嗨,Alex,你在代码中调用了两个数据框Q10_summary和therm_sen_final,但是你只提供了一个名为test的数据框,是否有遗漏? - StupidWolf
1个回答

5
您可以在position_jitter中传递参数seed,以使得geom_pointgeom_line之间产生相同的抖动效果。
ggplot(data = test, aes(
  x = time_point, y = Q10, color = storage_temp
)) +
  geom_point(
    data = test,
    aes(time_point, Q10, group = storage_temp),
    alpha = 0.25,
    color = 'black',
    position = position_jitter(width = 0.25, seed = 123)
  ) +
  geom_line(
    data = test,
    aes(group = individual_code, linetype = storage_temp),
    color = 'black',
    alpha = 0.25,
    position = position_jitter(width = 0.25, seed = 123)
  )

在此输入图像描述

这回答了你的问题吗?


啊,当然……因为抖动本质上是随机的。标记为已回答。 - Alex
@Alex,没错。你也可以在 ggplot2 中设置 x 和 y 位置的抖动效果,并将其转换为 ggplot2(这是在 position_jitter 添加 seed 参数之前人们所做的),但现在通过将 seed 合并到 position_jitter 中,像这样做更加简单明了。 - dc37

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接