我想将工具提示添加到ggsurvplot
. 我ggiraph
用来显示情节。由于ggiraph
没有geom_step_interactive
,我正在操作我的数据输出ggsurvplot
以获取阶跃函数值并使用geom_line_interactive
在我的 ggsurvplot 上覆盖一条线并添加一个工具提示。这是正常工作的代码:
library(ggiraph)
library(survminer)
library(survival)
#Function to get the step function points
step_func <- function(data, direction="hv", x , y) {
direction <- match.arg(direction, c("hv", "vh"))
data <- as.data.frame(data)[order(data[, x]), ]
n <- nrow(data)
if (n <= 1) {
# Need at least one observation
return(data[0, , drop = FALSE])
}
if (direction == "vh") {
xs <- rep(1:n, each = 2)[-2*n]
ys <- c(1, rep(2:n, each = 2))
} else {
ys <- rep(1:n, each = 2)[-2*n]
xs <- c(1, rep(2:n, each = 2))
}
return(data.frame(
x = data[,x][xs],
y = data[,y][ys],
data[xs, setdiff(names(data), c(x, y))]
))
}
fit<- survfit(Surv(time, status) ~ sex, data = lung )
g <- ggsurvplot(fit, data = lung, risk.table = TRUE,
risk.table.pos= "out", risk.table.y.text = TRUE)
dat1 <- plyr::ddply(g$plot$data, "strata", step_func, "hv", "time", "surv")
dat1$tooltip <- paste0("Time:", dat1$x)
gg<- g$plot+geom_line_interactive(data = dat1, aes(x= x, y=y, colour = strata, tooltip= tooltip ), size = .75)
ggiraph(code = print(gg))
我希望工具提示不仅显示时间,还显示生存概率,为此我将代码稍微更改如下:
dat1$tooltip <- paste0("Time:", dat1$x, "Surv:", dat1$y )
这会导致工具提示消失。
我究竟做错了什么?