在 R 中创建相对时间点的绘图

Min*_*ina 3 plot r ggplot2

我有一个像这样的数据框:

   ID Var1 Var2 Month BeforeAfter
1   1   23    4     1       FALSE
2   1   41    5     2       FALSE
3   1   32    2     3       FALSE
4   1   58    1     4        TRUE
5   1   60    7     5        TRUE
6   2   12    3     1       FALSE
7   2   34    4     2       FALSE
8   2   55    5     3        TRUE
9   2   49    5     4        TRUE
10  2   60    6     5        TRUE
11  2   64    9     6        TRUE
Run Code Online (Sandbox Code Playgroud)

我想创建一个图(例如ggplot)来显示 的某种处理之前和之后Var1。因此,我希望治疗时间/x 轴为 0,并且与治疗相关的月份应在其附近。这些FALSE值表示治疗前的时间,这些TRUE值表示治疗后的时间,而第一个TRUE表示治疗时间。所以我期望类似这样的输出:

在此输入图像描述

我寻找解决方案,但没有真正找到解决方案,而且由于我是 RI 新手,不知道从哪里开始,所以我很高兴收到任何建议。

谢谢你!


编辑:更大的数据样本:

larger_sample <- data.frame(ID=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11),
                     Var1= c(121,134,101,17,90,71,40,93,59,34,38,85,127,92,78,51,116,51,39,105,118,142,13,123,53,101,59,128,37,126,94,46,131,131,134,37,148,60,120,55,28,102,28,137,51,87,69,113,63,46,98,59,30,44,16,124,58,137,30,135,50,79,122,69,93,89,129,124,91,29,90,145,126,103,93,102,79,15,83,64,20,133,68,49,27,127,144,24,124,138,133,60,135,37,126,22,87,144,105,26,150,150,48,88,118,48,125,61,149,48,56,46,93,80,78,28,74,36,97,31,36,81,69,19,128,118,56,62,93,40,96,23,119,101,16,113,57,90,101,149,31,48,103,83,50,20,40,94,140,91,16,121,106,130,109,46,12,135,62,148,144,70,107,117,24,69,13,14,34,35,59,129,20,18,90,139,13,94,38,41,65,68,45,48,128,52,62,96,70,44,47,25,43,52,68,102,67,59,144,51,109,100,41,63,87,46,145,12,138,63,123,36,102,66,72,65,62,113,50,53,80,91,44,119,29,141,14,117,87,121,121,117,35,139,33,116,144,67,149,80,128,113,59,30,29,147,61,141,20,61,150,114,134,134,46,20,19,130,66,120,143,70,121,96,124,38,144,84,97,148,31,120,132,86,71,107,19,13,104,111,82,144,56,43,115,85,123,108,150,73,67,146,35,143,127,136,56,46,40,136,45,60,79,84,59,87,42,121,73,50,40,46,90,107,47,14,34,146,87,71,85,12,75,143,41,31,25,136,16,20,96,21,60,69,25,39,38,63,17,43,90,67,48),
                     Month= c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26),
                     BeforeAfter= c(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE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E,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE))
Run Code Online (Sandbox Code Playgroud)

All*_*ron 5

你可以这样做:

library(tidyverse)

df %>%
  group_by(ID) %>%
  mutate(Month = Month - Month[which(BeforeAfter)[1]], ID = factor(ID)) %>%
  ggplot(aes(x = Month, y = Var1, linetype = ID)) +
  geom_line() +
  geom_point() +
  geom_vline(xintercept = 0) +
  xlab("Time relative to starting treatment (months)")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


使用的数据

df <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L), Var1 = c(23L, 41L, 32L, 58L, 60L, 12L, 34L, 55L, 49L, 60L, 
64L), Var2 = c(4L, 5L, 2L, 1L, 7L, 3L, 4L, 5L, 5L, 6L, 9L), Month = c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L), BeforeAfter = c(FALSE, 
FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11"))
Run Code Online (Sandbox Code Playgroud)

编辑

要获得每个时间点的平均值和置信区间,最好在绘图之前总结数据:

larger_sample %>%
  group_by(ID) %>%
  mutate(Month = Month - Month[which(BeforeAfter)[1]]) %>%
  group_by(Month) %>%
  filter(n() > 2) %>%
  summarise(Mean = mean(Var1),
            upper = Mean + 1.96 * sd(Var1)/sqrt(n()),
            lower = Mean - 1.96 * sd(Var1)/sqrt(n())) %>%
  ggplot(aes(x = Month, y = Mean)) +
  geom_line() +
  geom_point() +
  geom_ribbon(aes(ymin = lower, ymax = upper), fill = NA, colour = "black",
              linetype = 2) +
  geom_vline(xintercept = 0) +
  xlab("Time relative to starting treatment (months)")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

创建于 2023-08-08,使用reprex v2.0.2