我正在尝试将工具提示添加到ggsurvplot
。我正在使用ggiraph
来显示情节。我添加了一个我在下面尝试做的最小例子。如果我在ggsurvplot
中没有添加任何xlim,我的代码似乎可行。以下是有效的代码:
library(survminer)
library(survival)
library(ggiraph)
fit<- survfit(Surv(time, status) ~ sex, data = lung )
g <- ggsurvplot(fit, data = lung)
tooltip <- paste0("Time:", g$plot$data$time, "<br/>", "Survival Prob:", g$plot$data$surv)
x <- g$plot+geom_point_interactive(aes(tooltip = tooltip))
ggiraph(print(x), zoom_max = 5)
现在我想限制x轴值,所以我将参数xlim = c(0, 800)
添加到ggsurvplot,如下所示:
library(survminer)
library(survival)
library(ggiraph)
fit<- survfit(Surv(time, status) ~ sex, data = lung )
g <- ggsurvplot(fit, data = lung, xlim = c(0,800))
tooltip <- paste0("Time:", g$plot$data$time, "<br/>", "Survival Prob:", g$plot$data$surv)
x <- g$plot+geom_point_interactive(aes(tooltip = tooltip))
ggiraph(print(x), zoom_max = 5)
这给了我以下错误:
Error: length(ids) == length(str) is not TRUE
如何解决此错误?
答案 0 :(得分:1)
这是ggiraph的一个问题。添加xlim
会触发裁剪,不会绘制某些点,但工具提示变量不会被裁剪;我会尽力解决这个问题。
解决方法是在将数据发送到geom_point_interactive之前过滤数据:
library(survminer)
library(survival)
library(ggiraph)
fit<- survfit(Surv(time, status) ~ sex, data = lung )
g <- ggsurvplot(fit, data = lung, xlim = c(0,800))
data_ <- g$plot$data
data_ <- base::transform(data_, tooltip = sprintf("Time: %.0f<br/>Survival Prob: %.3f", time, surv) )
data_ <- data_[data_$time < 800, ]
x <- g$plot + geom_point_interactive(data = data_, aes(time, surv, tooltip = tooltip))
ggiraph(print(x), zoom_max = 5)