如何使用Plotly去除默认工具提示?

3

我之前一直在使用ggplot,直到我接触到Plotly库并开始在我的图表中使用它。我仍然使用ggplot函数创建图表,并最终使用ggplotly函数进行转换。

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

当绘制图表时,我可以分别看到两个工具提示,一个显示我在geom_tile中设置的文本属性,另一个重复显示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")
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接