Create plot with relative time point in R

huangapple go评论92阅读模式
英文:

Create plot with relative time point in R

问题

我有一个类似这样的数据框:

   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

我想创建一个图表(例如使用ggplot),以显示对Var1进行某种处理之前和之后的情况。因此,我希望处理时间/ x 轴位于 0,相对于处理的月份应该围绕它。FALSE 值表示处理之前的时间,TRUE 值表示处理之后的时间,而第一个 TRUE 表示处理时间。因此,我期望的输出类似于这样:

Create plot with relative time point in R

我搜索了解决方案,但没有找到什么真正有用的结果,因为我对 R 还不熟悉,不知道从哪里开始,请给我一些建议。

谢谢!

英文:

I have a dataframe like this:

   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

I want to create a plot (for example with ggplot) to show the before and after a certain treatment for Var1. Thus, I want that the treatment time/x-axis to be at 0 and the months relative to the treatment should be around it. The FALSE values indicate the time before the treatment and the TRUE values the time after the treatment, whereas the first TRUE indicates the treatment time. So I expect an output similar to this:

Create plot with relative time point in R

I searched for a solution, but nothing really came up and as I am new to R I do not know where to start, so I would be happy about any suggestions.

Thank you!


Edit: Larger data sample:

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,TRU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UE,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,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,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,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))

答案1

得分: 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("相对于开始治疗的时间(月)")

数据使用如下:

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"))

编辑:

要在每个时间点上获取均值和置信区间,最好在绘图之前对数据进行汇总:

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("相对于开始治疗的时间(月)")
英文:

You could do:

library(tidyverse)

df %&gt;%
  group_by(ID) %&gt;%
  mutate(Month = Month - Month[which(BeforeAfter)[1]], ID = factor(ID)) %&gt;%
  ggplot(aes(x = Month, y = Var1, linetype = ID)) +
  geom_line() +
  geom_point() +
  geom_vline(xintercept = 0) +
  xlab(&quot;Time relative to starting treatment (months)&quot;)

Create plot with relative time point in R


Data used

df &lt;- 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 = &quot;data.frame&quot;, row.names = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, 
&quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;11&quot;))

EDIT

To get the mean and confidence intervals at each time point, it would be best to summarise your data before plotting:

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

Create plot with relative time point in R

<sup>Created on 2023-08-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年8月8日 20:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定