1

This is my first question here if I've made any mistake on posting it or explaining my issue I will be happy to hear any feedback.

I'm building a tool in R using Shiny, ggplot and Plotly. I've got an example from plotly library where I can see that's possible to have a tooltip when using ggplot to render a graph.

Follows my example code:

library(shiny) library(ggplot2) library(plotly) library(dplyr) library(reshape) ui <- fluidPage( plotlyOutput("distPlot") ) server <- function(input, output) { output$distPlot <- renderPlotly({ dat <- data.frame( time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), total_bill = c(14.89, 17.23) ) p <- ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + geom_bar(stat="identity") p <- p + geom_bar(stat = "identity") ggplotly(p) }) } shinyApp(ui = ui, server = server) 

When I use the above code the tooltip does not show up but if I remove the fill = X2 parameter the tooltip appears:

 p <- ggplot(data=dat, aes(x=time, y=total_bill)) + geom_bar(stat="identity") 

In my application I need to have the fill parameter to separate the "groups" that I have by color and show the legend.

I have searched for many "workarounds" on the internet using javascript and other solutions, but since it's a native feature from plotly I would like to have this working in a simpler way.

Thanks in advance for any help!!

1
  • Try g <- plotly_build(p). Then look at the structure of g, you should find g$x$data[[i]]$text. Look at what the value is there and changing it. Commented Mar 22, 2017 at 16:03

1 Answer 1

2

This is kind of a known issue, you can use this to fix it:

p <- ggplotly(p) for (i in 1:length(p$x$data)){ p$x$data[[i]]$text <- c(p$x$data[[i]]$text, "") } p 
Sign up to request clarification or add additional context in comments.

1 Comment

Hello @GGamba thanks for your answer, the first option works fine, however, the second doesn't. I believe this is a bug and I couldn't find a workaround.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.