创建新的文本参数时,带有ggplotly的统计平滑工具提示失败

问题描述

我正在做一个Shiny应用程序,但是出于制作一个最小的可复制示例的精神,我放弃了Shiny部分。这只是上下文。

我想用ggplotly()函数作图。在我尝试更改工具提示之前,它工作正常。 否则,我的代码将如下所示:

library(ggplot2)
library(plotly)

n <- 100
samples <- n
act.rec <- n
while(n != 0) {
  rand <- ceiling(runif(n,2))
  act <- length(rand[rand == 2])
  act.rec <- c(act.rec,act)
  n <- n-act
  samples <- c(samples,n)
}
test <- data.frame(act = c(act.rec,0),samples = c(samples,hl = 0:length(samples))
test2 <- test
test2$samples <- 100-test$samples
test <- rbind(cbind(test,dec = "Not decayed"),cbind(test2,dec = "Decayed"))

p <- ggplot(test,aes(x = hl,y = samples,colour = dec))+
  stat_smooth(method = "nls",size = 0.8,formula = y ~ SSasymp(x,Asym,R0,lrc),se = FALSE)+
      geom_point(shape = 4,size = 3)+
      theme_minimal()+
      scale_colour_discrete(name = "Decay Status")+
      xlab("Half Lives")+
      ylab("Atoms")

ggplotly(p)

这是放射性元素半衰期的模拟(同样是上下文)。 之后,我尝试在text geom中添加geom_point()参数。那很好如果我将鼠标悬停在十字架上,它会告诉我我想要的东西:

[...]
geom_point(shape = 4,size = 3,aes(text = paste(
        "Half-lives: ",hl,"\nAtoms: ",samples,"\nDecay Status: ",dec
      ) ))+
[...]

ggplotly(p,tooltip = "text")

只是快照:

enter image description here

但是,当我尝试对stat_smooth执行相同操作时,它消失了。

[...]
stat_smooth(method = "nls",se = FALSE,aes(text = paste(
                "Half-lives: ",dec
                  ) ))+
[...]

ggplotly(p,tooltip = "text")

enter image description here

顺便说一句,我只是做ggplotly(p),它具有相同的效果,只是确实存在的工具提示将两次显示所有内容,一次是我想要的,一次是之前的。但这不是重点。

我已经阅读了多个帖子,例如this one,from which I got the idea to use label instead of textthis one,which hasn't been answered yet。 从字面上看,使用label并没有帮助我。该行没有消失,但是工具提示不起作用。

enter image description here

我必须说,我不知道下一步该怎么做。我真的希望有人能找出问题所在,或者也许geom_smooth(或stat_smooth)是特例。无论哪种方式,请先谢谢。

PS(编辑2):我还发现,如果我没有在text中放入任何变量,则可以正常工作:

[...]
stat_smooth(method = "nls",aes(text = paste(
                "Half-lives: "
                  ) ))+
[...]
ggplotly(p)

enter image description here

编辑:我当前的代码是:

library(ggplot2)
library(plotly)

n <- 100
samples <- n
act.rec <- n
while(n != 0) {
  rand <- ceiling(runif(n,aes(label = paste(
                "Half-lives: ",dec
                  ) ))+
      geom_point(shape = 4,dec
      ) ))+
      theme_minimal()+
      scale_colour_discrete(name = "Decay Status")+
      xlab("Half Lives")+
      ylab("Atoms")

ggplotly(p)

解决方法

我不确定为什么,但是stat_smoothtooltip内定义aes文本时似乎有问题。一种解决方法是侵入plotly_build对象,如下所示。

n <- 100
samples <- n
act.rec <- n
while(n != 0) {
  rand <- ceiling(runif(n,2))
  act <- length(rand[rand == 2])
  act.rec <- c(act.rec,act)
  n <- n-act
  samples <- c(samples,n)
}
test <- data.frame(act = c(act.rec,0),samples = c(samples,hl = 0:length(samples))
test2 <- test
test2$samples <- 100-test$samples
test <- rbind(cbind(test,dec = "Not decayed"),cbind(test2,dec = "Decayed"))
#text <- paste("Half-lives: ",test$hl,"\nAtoms: ",test$samples,"\nDecay Status: ",test$dec )

p <- ggplot(test,aes(x = hl,y = samples,colour = dec)) +
  geom_point(shape = 4,size = 3) +
  stat_smooth(method = "nls",size = 0.8,formula = y ~ SSasymp(x,Asym,R0,lrc),se = FALSE) +
  theme_minimal()+
  scale_colour_discrete(name = "Decay Status")+
  xlab("Half Lives")+
  ylab("Atoms")

pp <- plotly_build(p)

pp31 <- pp$x$data[[1]]
pp32 <- pp$x$data[[2]]
pp33 <- pp$x$data[[3]]
pp34 <- pp$x$data[[4]]

pp$x$data[[1]]$text <- paste("Half-lives: ",pp31$x,"<br>Atoms: ",pp31$y,"<br>Decay Status: ",pp31$name)
pp$x$data[[2]]$text <- paste("Half-lives: ",pp32$x,pp32$y,pp32$name)
pp$x$data[[3]]$text <- paste("Half-lives: ",pp33$x,pp33$y,pp33$name)
pp$x$data[[4]]$text <- paste("Half-lives: ",pp34$x,pp34$y,pp34$name)

plotly_build(pp)

output