如何将数据分解为特定值以便在基础 R 中绘制不同的线?

问题描述 投票:0回答:1

我有女性基础体温的数据。我有三栏:日期、体温和月经周期的日期。

我想用线温度(y 轴)相对于“她的周期日”(x 轴)来绘制。对于每个“第一天”(月经日),我希望线条断裂并可能改变颜色, 无需将数据集分成几部分。数据很乱,不是每个月都有记录,但我们可以看到,如果日期减少,那么就已经是不同的时期了。

数据如下:

Date    Temp    DayofCycle

17/06/2024  98.06   16
18/06/2024  98.42   17
19/06/2024  98.68   18
21/06/2024  98.97   20
22/06/2024  98.63   21
24/06/2024  98.16   23
25/06/2024  98.73   24
27/06/2024  98.29   26

28/06/2024  97.87   1
01/07/2024  98.26   4
03/07/2024  97.94   6
04/07/2024  98.07   7
05/07/2024  97.41   8
08/07/2024  97.99   11
09/07/2024  98.06   12
10/07/2024  97.92   13
11/07/2024  98.28   14
12/07/2024  98.17   15
13/07/2024  98.22   16
14/07/2024  98.64   17
16/07/2024  98.88   19
18/07/2024  98.32   21
21/07/2024  98.44   24
24/07/2024  98.46   27

25/07/2024  98.17   1
27/07/2024  98.11   3
28/07/2024  98.16   4
29/07/2024  98.66   5
31/07/2024  98.04   7
03/08/2024  97.73   10  
05/08/2024  97.35   12
06/08/2024  97.36   13
08/08/2024  98.01   14

我想使用基本 R 来绘图(目前没有 ggplot)。

r date plot
1个回答
0
投票

我使用了 Gregor 的滞后组度量,但跳过了循环并简单地制作了三个数据集。这是您要找的吗?

menst<-structure(list(Date = c("17/06/2024", "18/06/2024", "19/06/2024", 
"21/06/2024", "22/06/2024", "24/06/2024", "25/06/2024", "27/06/2024", 
NA, "28/06/2024", "01/07/2024", "03/07/2024", "04/07/2024", "05/07/2024", 
"08/07/2024", "09/07/2024", "10/07/2024", "11/07/2024", "12/07/2024", 
"13/07/2024", "14/07/2024", "16/07/2024", "18/07/2024", "21/07/2024", 
"24/07/2024", NA, "25/07/2024", "27/07/2024", "28/07/2024", "29/07/2024", 
"31/07/2024", "03/08/2024", "05/08/2024", "06/08/2024", "08/08/2024"
), Temp = c("98.0", "98.4", "98.6", "98.9", "98.6", "98.1", "98.7", 
"98.2", NA, "97.8", "98.2", "97.9", "98.0", "97.4", "97.9", "98.0", 
"97.9", "98.2", "98.1", "98.2", "98.6", "98.8", "98.3", "98.4", 
"98.4", NA, "98.1", "98.1", "98.1", "98.6", "98.0", "97.7", "97.3", 
"97.3", "98.0"), CycleDay = c("16", "17", "18", "20", "21", "23", 
"24", "26", NA, "1", "4", "6", "7", "8", "11", "12", "13", "14", 
"15", "16", "17", "19", "21", "24", "27", NA, "1", "3", "4", 
"5", "7", "10", "12", "13", "14")), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -35L))

menst$Date<-as.Date(menst$Date,format="%d/%m/%Y") # format date
menst$CycleDay<-as.numeric(menst$CycleDay) # format cycle day
menst<-menst %>% arrange(Date)  # ensure date order
menst$group = cumsum(c(-1, sign(diff(menst$CycleDay))) == -1)

menst1<-menst[menst$group==1,]  # data set for each cycle
menst2<-menst[menst$group==2,]
menst3<-menst[menst$group==3,]


plot(menst$Temp,menst$CycleDay,
 xlab="Basal Temperature", ylab="Cycle Day",
 main="So much science...",
 font.main=9)
lines(menst1$Temp,menst1$CycleDay,type="l", add=TRUE,"darkgreen")
lines(menst2$Temp,menst2$CycleDay,type="l", add=TRUE,col='orange')
lines(menst3$Temp,menst3$CycleDay,type="l", add=TRUE,col='blue')
© www.soinside.com 2019 - 2024. All rights reserved.