我想生成4名患者(PatientID)的x-y线图,这些患者随着时间的推移(Age_0 - Age_10)检测CRP水平(CRP_0 - CRP_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是年龄,Y是CRP。例如,Age_0与CRP_0是每个PatientID的一个数据点。然后,第二个数据点是Age_1与CRP_1等等。最后,我想要一个这样的情节:
如果你可以帮我解决这个问题,我会很高兴。非常感谢你。
答案 0 :(得分:2)
你可以这样做(虽然我觉得它可能不是最有效的方式):
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
的点是缺失值,但非年龄和CRP=0
的点是有效点。
答案 1 :(得分:2)
我无法看到您提供的图片,但这是一个想法,
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()