3
votes

I was using ggplot for some time until I came across the Plotly library and started to use it for my plots. I still use ggplot functions to create the plot and at the end the ggplotly function to transform it.

I'm using a geom_tile and a geom_text layer in my plot to create a heatmap. I set the "text" property in the geom_tile to the text I want to display in the tooltip and pass tooltip="text" in the ggplotly function. The text I want to show inside the rectangle is the one set in the geom_text layer using "label" property.

When the plot is rendered I can see both tooltips, separately, one with the text I set in the text property in geom_tile, and the other one repeating the text in the label property in the geom_text layer.

How can I get rid of this last tooltip?

Thanks in advance!

Here's my code:

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 Answers

1
votes

ggplotly() will automatically create hover text - it's part of it's "magic". To overwrite, you'll have to use 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