【问题标题】:How can I get rid of default tooltips using Plotly?如何使用 Plotly 摆脱默认工具提示?
【发布时间】:2017-02-08 05:36:02
【问题描述】:

我使用 ggplot 有一段时间了,直到我遇到了 Plotly 库并开始将它用于我的绘图。我仍然使用 ggplot 函数来创建绘图,最后使用 ggplotly 函数对其进行转换。

我在绘图中使用 geom_tile 和 geom_text 图层来创建热图。我将 geom_tile 中的“text”属性设置为要在工具提示中显示的文本,并在 ggplotly 函数中传递 tooltip="text"。我想在矩形内显示的文本是 geom_text 层中使用“label”属性设置的文本。

当绘图被渲染时,我可以分别看到两个工具提示,一个是我在 geom_tile 的 text 属性中设置的文本,另一个是在 geom_text 层的 label 属性中重复文本。

我怎样才能摆脱最后一个工具提示?

提前致谢!

这是我的代码:

gg<-ggplot(dataframe, aes(x=varX,y=varY))+
    geom_tile(aes(fill=cm,text=paste("#",casos)))+
    geom_text(aes(label=cm,size=casos),family = "arial", color="#111111")+
    scale_fill_gradient2(low="#DD3333",mid="#EEEE99",high="#00AA00")+ 
    labs(x="varX",y="varY",fill="CM")+ scale_size(range = c(2.5, 5),guide='none') +
    theme(text=element_text(family = "arial", color="#666666", size=11) ,title=element_text(family = "arial", color="#666666", face="bold", size=12), axis.text=element_text(family = "arial", color="#666666", size=9),axis.text.x=element_text(angle=330))
ggplotly(gg, tooltip="text")

【问题讨论】:

    标签: r ggplot2 shiny plotly


    【解决方案1】:

    ggplotly() 将自动创建悬停文本 - 它是“魔法”的一部分。要覆盖,您必须使用plotly_build()

    library(plotly)
    library(ggplot2)
    
    gg <- ggplot(mtcars, aes(x = cyl, y = gear)) +
      geom_tile(aes(fill = mpg))
    
    pb <- plotly_build(gg)
    
    # check structure of pb with str(pb) and see that the text element is a matrix
    mtext <- as.character(seq(1,9,1)))
    
    pb$data[[1]]$text <- matrix(mtext, 3, 3)
    
    pb
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2021-02-10
      • 2021-09-06
      相关资源
      最近更新 更多