I have a base-barchart plot with working tooltips.
I want to add a line-chart by clicking a radioButton on top of the barchart, also with hovering tooltips, which is not working as I expected.
text argument to the additional line, the line is not showing up, but when I hover over the pot, the labels of the line show.text argument (_line 28_), the line shows, but no hovering tooltips, as expected.tooltip = "text" from the ggplotly call (_line 33_), the line and the tooltips shows up, but the formatting is not like I want it to be. 
library(shiny)
library(ggplot2)
library(plotly)
dfN <- data.frame(
time_stamp = seq.Date(as.Date("2018-04-01"), as.Date("2018-07-30"), 1),
val = runif(121, 100,1000),
col = "green", stringsAsFactors = F
)
ui <- fluidPage(
br(),br(),
checkboxInput("zus1", value = FALSE, label = HTML("add Line")),
plotlyOutput("plt")
)
server <- function(input, output, session) {
output$plt <- renderPlotly({
key <- highlight_key(dfN)
p <- ggplot() +
geom_col(data = key, aes(x = plotly:::to_milliseconds(time_stamp), y = val, fill=I(col),
text=paste("Datum: ", time_stamp, "<br>")))
if (input$zus1 == TRUE) {
p <- p + geom_line(data = dfN, aes(x = plotly:::to_milliseconds(dfN$time_stamp), y = val
## This text is not showing up, but hovering shows the labels
# , text=paste("Value: ", val)
), color="red")
}
## Removing tooltip = "text" shows the additional line and hover-labels but with horrible formatting.
ggplotly(p, source = "Src") %>%
highlight(selectize=F, on="plotly_click", off = "plotly_doubleclick", color = "blue") %>%
layout(xaxis = list(tickval = NULL, ticktext = NULL, type = "date"))
})
}
shinyApp(ui, server)
Here's a more minimal example of the same issue:
library(plotly)
dfN <- data.frame(
time_stamp = seq.Date(as.Date("2018-04-01"), as.Date("2018-07-30"), 1),
val = runif(121, 100, 1000),
stringsAsFactors = F
)
p <- ggplot(dfN) +
geom_line(aes(x = time_stamp, y = val, text = paste("Value: ", val)))
ggplotly(p)
Turns out you can 'fix' it by adding group = 1 to the aesthetic mapping
p <- ggplot(dfN) +
geom_line(aes(x = time_stamp, y = val, group = 1, text = paste("Value: ", val)))
ggplotly(p)
This is a pretty bad bug that we should fix
Most helpful comment
Here's a more minimal example of the same issue:
Turns out you can 'fix' it by adding
group = 1to the aesthetic mappingThis is a pretty bad bug that we should fix