【问题标题】:Remove column name from hover text从悬停文本中删除列名
【发布时间】:2016-08-06 15:52:47
【问题描述】:

如何删除下图中悬停文本中的cyl:

library(plotly)
ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(),
         tooltip = c("colour"))

【问题讨论】:

  • 如何尝试查看此link。也许会有所帮助

标签: r plotly ggplotly


【解决方案1】:

可能有更优雅的方法,但您可以尝试:

b <- ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(),
          tooltip = c("colour"))
p <- plotly_build(b) 
p$data[[1]]$text
library(stringr)
p$data[[1]]$text <- str_sub(p$data[[1]]$text,-2,-1)
p

您只需将悬停时显示的文本替换为您想要的其他内容。在您的示例中,我只是提取了最后一个数字。

【讨论】:

    【解决方案2】:

    您可以使用text 美学来显示不带列名的值:

    library(plotly)
    ggplotly(ggplot(
      mtcars, 
      aes(mpg, hp, colour = cyl, text = cyl)) + geom_point(),
      tooltip = c("text")
    ))
    

    您还可以使用text 粘贴字符串或样式,而不会将此代码显示为“列名”:

    ggplotly(ggplot(
      mtcars, 
      aes(mpg, hp, colour = cyl, text = paste("Cylinders: ", cyl)) + 
        geom_point(),
      tooltip = c("text")
    )) 
    

    它的文档记录不是很好,但请参阅here 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多