简体   繁体   English

为每个Y值绘制具有不同X值的XY线图

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

I'd like to generate xy line graph for 4 patients (PatientID) that were samples over time (Age_0 - Age_10) for CRP level (CRP_0 - CRP_10). 我想生成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))

So, I'd like to plot a line graph in which X is Age and Y is CRP. 因此,我想绘制一个折线图,其中X是Age,Y是CRP。 For instance, Age_0 vs CRP_0 is one data point for each PatientID. 例如,Age_0 vs CRP_0是每个PatientID的一个数据点。 Then, second data point is Age_1 vs CRP_1 and etc. At the end, I'd like to have a plot something like this: 然后,第二个数据点是Age_1 vs CRP_1等。最后,我想绘制一个类似这样的图:

在此处输入图片说明

I'd be happy if you can help me out for this. 如果您能帮助我,我会很高兴。 Thank you very much. 非常感谢你。

You can do like this (though I feel that it's probably not the most efficient way): 您可以这样做(尽管我觉得这可能不是最有效的方法):

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

Here I assumed that the points with Age=0 are missing values, but points with non-null age and CRP=0 are valid points. 在这里,我假设Age=0的点是缺失值,但age无效且CRP=0的点是有效点。

在此处输入图片说明

I can't see the image you provide but here is an idea, 我看不到您提供的图片,但这是一个主意,

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