Search code examples
rggplotly

Changing the tooltip in ggplotly


I am trying to alter the tooltip in ggplotly. At the moment the tooltip is showing Data1 and reorder(Data2, Data1). I wish this to show Data2 and Data1. My approach is below.

   gg <- ggplot(df)+
        geom_point(aes(x = Data1, y = reorder(Data2, Data1))) +
    
    ggplotly(gg,tooltip = c("Data2", "Data1"))

I feel I've followed the official documentation: https://www.rdocumentation.org/packages/plotly/versions/4.9.3/topics/ggplotly I've also followed the various methods available here: https://plotly-r.com/controlling-tooltips.html

Yet this does not seem to change anything. Any direction is welcomed.


Solution

  • Try this

    df <- cars
    names(df) <- c("Data1", "Data2")
    yy <- names(df)[2]
    ###  define what you want to display in tooltip
    text <- paste("Data2:",df$Data2, "\nData1:", df$Data1)
    
    gg <- ggplot(df) +
      geom_point(aes(x = Data1, y = reorder(Data2, Data1), text=text)) + labs(y=yy)
      
      ggplotly(gg,tooltip = c("text"))
    

    output