簡體   English   中英

為每個Y值繪制具有不同X值的XY線圖

[英]Plotting X-Y line graph with different X values for each Y value

我想生成4位患者(PatientID)的xy線圖,這些患者是CRP水平(CRP_0-CRP_10)隨時間(Age_0-Age_10)的樣本。

PatientID   Age_0   Age_1   Age_2   Age_3   Age_4   Age_5   Age_6   Age_7   Age_8   Age_9   Age_10  CRP_0   CRP_1   CRP_2   CRP_3   CRP_4   CRP_5   CRP_6   CRP_7   CRP_8   CRP_9   CRP_10
1   22  24  24  28  30  31  0   0   0   0   0   3   9   2   1   0   1   0   0   0   0   0
3   27  28  29  32  33  35  0   0   0   0   0   2   10  2   1.2 0.1 0   0   0   0   0   0
4   37  38  39  40  42  43  44  45  0   0   0   8   0   7   7   0   0   7   2   0   0   0
5   33  35  36  38  39  40  41  0   0   0   0   2   2   5   0.2 0   0   0   0   0   0   0

structure(list(PatientID = c(1L, 3L, 4L, 5L), Age_0 = c(22L,
27L, 37L, 33L), Age_1 = c(24L, 28L, 38L, 35L), Age_2 = c(24L,
29L, 39L, 36L), Age_3 = c(28L, 32L, 40L, 38L), Age_4 = c(30L,
33L, 42L, 39L), Age_5 = c(31L, 35L, 43L, 40L), Age_6 = c(0L,
0L, 44L, 41L), Age_7 = c(0L, 0L, 45L, 0L), Age_8 = c(0L, 0L,
0L, 0L), Age_9 = c(0L, 0L, 0L, 0L), Age_10 = c(0L, 0L, 0L, 0L
), CRP_0 = c(3L, 2L, 8L, 2L), CRP_1 = c(9L, 10L, 0L, 2L), CRP_2 = c(2L,
2L, 7L, 5L), CRP_3 = c(1, 1.2, 7, 0.2), CRP_4 = c(0, 0.1, 0,
0), CRP_5 = c(1L, 0L, 0L, 0L), CRP_6 = c(0L, 0L, 7L, 0L), CRP_7 = c(0L,
0L, 2L, 0L), CRP_8 = c(0L, 0L, 0L, 0L), CRP_9 = c(0L, 0L, 0L,
0L), CRP_10 = c(0L, 0L, 0L, 0L)), .Names = c("PatientID", "Age_0",
"Age_1", "Age_2", "Age_3", "Age_4", "Age_5", "Age_6", "Age_7",
"Age_8", "Age_9", "Age_10", "CRP_0", "CRP_1", "CRP_2", "CRP_3",
"CRP_4", "CRP_5", "CRP_6", "CRP_7", "CRP_8", "CRP_9", "CRP_10"
), class = "data.frame", row.names = c(NA, -4L))

因此,我想繪制一個折線圖,其中X是Age,Y是CRP。 例如,Age_0 vs CRP_0是每個PatientID的一個數據點。 然后,第二個數據點是Age_1 vs CRP_1等。最后,我想繪制一個類似這樣的圖:

在此處輸入圖片說明

如果您能幫助我,我會很高興。 非常感謝你。

您可以這樣做(盡管我覺得這可能不是最有效的方法):

library(ggplot2); library(reshape2): library(dplyr)
d1=df %>% select(matches("Age|PatientID")) %>% melt(id.vars=1, value.name="Age") %>% select(-variable)
d2=df %>% select(matches("CRP|PatientID")) %>% melt(id.vars=1, value.name="CRP") %>% select(-variable, -PatientID)
toplot = cbind(d1, d2) %>% arrange(PatientID, Age) %>% mutate(PatientID=factor(PatientID))
ggplot(toplot[toplot$Age!=0,]) + aes(x=Age, y=CRP, color=PatientID) + geom_point(pch=15, size=4) + geom_line(size=1.5) + coord_cartesian(xlim=c(0, 50)) + xlab("Time")

在這里,我假設Age=0的點是缺失值,但age無效且CRP=0的點是有效點。

在此處輸入圖片說明

我看不到您提供的圖片,但這是一個主意,

df1 <- data.frame(PatientID = df$PatientID, age = stack(df[,grep('Age', names(df))])[,1],
                   CRP = stack(df[,grep('CRP', names(df))])[,1], stringsAsFactors = FALSE)

head(df1)
#  PatientID age CRP
#1         1  22   3
#2         3  27   2
#3         4  37   8
#4         5  33   2
#5         1  24   9
#6         3  28  10

#converting PatientID to factor,
df1$PatientID <- as.factor(df1$PatientID)

#Plot,

library(ggplot2)
ggplot(df1, aes(x = age, y = CRP, color = PatientID))+geom_line()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM