简体   繁体   English

ggiraph中geom_line_interactive缺少工具提示

[英]Tooltip missing for geom_line_interactive in ggiraph

I wanted to add tooltip to ggsurvplot . 我想将工具提示添加到ggsurvplot I am using ggiraph to display the plot. 我正在使用ggiraph来显示情节。 Since ggiraph does not have geom_step_interactive , I am manipulating my the data output of ggsurvplot to get the step function values and using geom_line_interactive to overlay a line on my ggsurvplot and add a tooltip. 由于ggiraph没有geom_step_interactive ,我操纵的输出我的数据ggsurvplot得到阶跃函数值,并使用geom_line_interactive重叠在我的ggsurvplot线和添加工具提示。 Here is the code that is working correctly: 这是正常工作的代码:

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

With the above code I get the following plot with tooltip as displayed in the image: 使用上面的代码,我得到了如下图,其中显示了图像中显示的工具提示: 在此输入图像描述

I want the tooltip not only show the time but also the survival probability, for that I change the code slightly as follows: 我希望工具提示不仅显示时间而且还显示生存概率,因为我稍微更改了代码,如下所示:

dat1$tooltip <- paste0("Time:", dat1$x, "Surv:", dat1$y )

This causes the tooltip to disappear. 这会导致工具提示消失。

What am I doing wrong? 我究竟做错了什么?

It seems the geom_line_interactive tooltips vanish when they get too long. 似乎geom_line_interactive工具提示在它们变得太长时会消失。 You can truncate the values result to keep them short. 您可以截断值结果以使它们保持简短。 The following works: 以下作品:

dat1$tooltip <- paste0("Time:", dat1$x, " Surv:", round(dat1$y,2) )

but setting the value to 3 causes them to vanish. 但将值设置为3会导致它们消失。 I am not completely sure of this because the tooltips are extremely fiddly and seemingly only appear when the the mouse is in exactly the right place. 我并不完全确定这一点,因为工具提示非常繁琐 ,似乎只有当鼠标位于正确的位置时才会出现。 It could be that all of them over a certain length are suppressed. 可能是所有这些都超过一定长度被抑制。

I did notice however that the whole tooltip thing works much better if you use geom_point_interactive instead of geom_line_interactve and with larger size parameter values. 我也注意到然而,整个工具提示事情的作品,如果你使用更好的geom_point_interactive代替geom_line_interactve并具有较大size的参数值。 That is probably what you want to do. 这可能就是你想要做的。

Could be that geom_line_interactive is ignoring the size parameter. 可能是geom_line_interactive忽略了size参数。

This figure is with geom_point_interactive , the round parameter above set to 3, and the size set to 1.75, and it works much better. 这个数字是geom_point_interactive ,上面的round参数设置为3, size设置为1.75,它的效果要好得多。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM