Dieison Medinger Dieison Medinger -4 years ago 267
R Question

Plotly and ggplot2 hover text in barplot from R not working

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!!

Answer Source

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

p <- ggplotly(p)

for (i in 1:nrow(df.m)){
    p$x$data[[i]]$text <- c(p$x$data[[i]]$text, "") 
}
p
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download