我试图使用ggplot从包survidINRI重现该图。下面是文档中提供的示例。
#--- sample data (pbc in survival package) ---
D=subset(pbc, select=c("time","status","age","albumin","edema","protime","bili"))
D$status=as.numeric(D$status==2)
D=D[!is.na(apply(D,1,mean)),]
mydata=D[1:100,]
t0=365*5
indata1=mydata;
indata0=mydata[,-7]
covs1<-as.matrix(indata1[,c(-1,-2)])
covs0<-as.matrix(indata0[,c(-1,-2)])
#--- inference ---
x<-IDI.INF(mydata[,1:2], covs0, covs1, t0, npert=200)
#--- Graphical presentation of the estimates ---
IDI.INF.GRAPH(x)
编辑函数IDI.INF.GRAPH,我编写了以下代码:
library(ggplot2)
library(tidyr)
s <- x$point$cc
event <- 1 - x$point$FX
nevent <- 1 - x$point$GX
Xlim <- c(min(s[event > 0.001 | nevent > 0.001]),
max(s[event < 0.999 | nevent < 0.999]))
Ylim <- c(0, 1)
class <- ifelse(nevent > event, "blue", "red")
data_plot <- data.frame(event, nevent, s, class, min = event, max = nevent)
data_plot <- data_plot %>% gather(key = curve, value = value, event:nevent)
gp <- ggplot(data_plot, aes(x = s, y = value)) +
geom_line(aes(linetype = curve), size = 1.2) +
geom_ribbon(aes(x = s, ymin = min, ymax = max, fill = class)) +
scale_linetype("C.D.F.", labels = c("Event", "Non-Event")) +
labs(y = "P(D < s)", x = "s") +
theme(legend.position = "bottom") +
theme_bw()
因为我有两个不连续的区域,所以geom_ribbon似乎不够用。然后我尝试了geom_polygon:
s <- x$point$cc
event <- 1 - x$point$FX
nevent <- 1 - x$point$GX
Xlim <- c(min(s[event > 0.001 | nevent > 0.001]),
max(s[event < 0.999 | nevent < 0.999]))
Ylim <- c(0, 1)
class <- ifelse(nevent > event, "blue", "red")
data_plot <- data.frame(event, nevent, s, class)
data_plot <- data_plot %>% gather(key = curve, value = value, event:nevent)
ggplot(data_plot, aes(x = s, y = value)) +
geom_line(aes(linetype = curve), size = 1.2) +
geom_polygon(data = subset(data_plot, class == "blue"),
aes(x = s, y = value, group = class), fill = "blue") +
geom_polygon(data = subset(data_plot, class == "red"),
aes(x = s, y = value), fill = "red") +
scale_linetype("C.D.F.", labels = c("Event", "Non-Event")) +
labs(y = "P(D < s)", x = "s") +
theme(legend.position = "bottom") +
theme_bw()
我知道我没有正确使用geom_polygon,但我找不到解决方案。有什么想法吗?