I am trying to use plotly for interactive visuals in my shiny app. However, I am running into a problem when trying to add a custom tooltip to the graph. If i set the tooltip
argument in ggplotly()
then my geom_line()
is ignored in my output but the tooltip works.
Here is a MRE:
library(shiny)
library(ggplot2)
library(plotly)
library(tidyquant) #only using the palette_dark() function
library(magrittr)
graph <- data %>%
ggplot(aes(date, result, text = paste0("Site: ", site, "\n",
"Quarter: ", quarter, "\n",
"Year: ", year, "\n",
"Result: ", result))) +
facet_wrap(~site) +
geom_point(color = palette_dark()[1]) +
geom_line(color = palette_dark()[1]) +
theme_bw()
ggplotly(graph,
tooltip = "text")
This gives me this:
If I move the data =
and aes()
calls from the ggplot
call to the individual geom
s then The lines still aren't shown:
graph <- ggplot() +
facet_wrap(~site) +
geom_point(data = data,
aes(date, result,
text = paste0("Site: ", site, "\n",
"Quarter: ", quarter, "\n",
"Year: ", year, "\n",
"Result: ", result)),
color = palette_dark()[1]) +
geom_line(data = data,
aes(date, result,
text = paste0("Site: ", site, "\n",
"Quarter: ", quarter, "\n",
"Year: ", year, "\n",
"Result: ", result)),
color = palette_dark()[1]) +
theme_bw()
ggplotly(graph,
tooltip = "text")
This gives me the same output with the tooltips still working. However, now I receive two warnings:
Warning: Ignoring unknown aesthetics: text
Warning: Ignoring unknown aesthetics: text
If I remove the text=
argument from just the geom_line()
function then the lines are plotted but the tooltip no longer works:
Question:
How do I get the custom tooltip to work while still plotting my geom_line()
?
My data:
data <- structure(list(site = c("Site 1", "Site 1", "Site 1", "Site 1",
"Site 2", "Site 2", "Site 2", "Site 2",
"Site 3", "Site 3", "Site 3", "Site 3",
"Site 4", "Site 4", "Site 4", "Site 4",
"Site 5", "Site 5", "Site 5", "Site 5"),
parameter = c("Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter",
"Testing Parameter", "Testing Parameter"),
year = c(2016, 2017, 2017, 2017, 2016, 2017, 2017, 2017,
2016, 2017, 2017, 2017, 2016, 2017, 2017, 2017,
2016, 2017, 2017, 2017),
quarter = c(4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L),
result = c(22.87, 31.78, 54.76, 44.3, 27.78, 34.04, 53.27,
46.83, 19.83, 34.05, 31.18, 35.7, 24.11, 33.57,
47.5, 40.53, 29.4, 34.6, 53.18, 46.16),
count = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
date = structure(c(17075, 17167, 17257, 17348, 17075,
17167, 17257, 17348, 17075, 17167,
17257, 17348, 17075, 17167, 17257,
17348, 17075, 17167, 17257, 17348),
class = "Date")),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -20L),
.Names = c("site", "parameter", "year", "quarter", "result",
"count", "date"))