英文:
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
表示处理时间。因此,我期望的输出类似于这样:
我搜索了解决方案,但没有找到什么真正有用的结果,因为我对 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:
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,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE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E,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 %>%
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)")
Data used
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"))
EDIT
To get the mean and confidence intervals at each time point, it would be best to summarise your data before plotting:
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)")
<sup>Created on 2023-08-08 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论