I am having trouble using this approach to specify the traces and control the hover displays with a geom_smooth
plot. There are many similar questions answered here (most related is ggplotly - only return tooltip hover text on certain geom objects) but the solutions don't seem to work in my case.
Goal: display only the y
value in the tooltip for all of the smoother lines (mean and both se)
Problem: the trace for the smoother mean line seems to be 4, and for the smoother se lines seems to be 5. But changing the hoverinfo
for 4 does nothing, and changing the hoverinfo
for 5 changes the tooltip for the smoother mean, but not the smoother se
Reprex below, followed by screenshots of the problem
library(ggplot2)
library(plotly)
library(listviewer)
y <- c(2,7,3,4,2,4,5,5,1,4,4,7,4,2,2,3,7,4,1,3,6,11,3,4,3,3,3,2,0,2,1,0,3,1,1,2)
x <- c(lubridate::ymd("2018-12-01"),
lubridate::ymd(paste0("2019-", seq(1, 12, 1), "-01")),
lubridate::ymd(paste0("2020-", seq(1, 12, 1), "-01")),
lubridate::ymd(paste0("2021-", seq(1, 11, 1), "-01")))
df <- tibble(date=x, count=y) %>%
mutate(year = lubridate::year(date),
month.abb = month.abb[lubridate::month(date)])
p <- ggplot(df, aes(x = date, y = count)) +
geom_point(size=1.5, shape=21, stroke=0.25,
aes(fill = factor(year),
text = paste0(month.abb, ": ", count))) +
geom_smooth(span=.8, col="grey")
plotly_json(ggplotly(p)) # to identify the traces
# only demonstrating the effect of changing trace 5
ggplotly(p, tooltip = "text") %>%
style(hoverinfo = "y", traces = 5)
Screenshots
This shows the smoother mean would appear to be trace 4, but changing trace 5 hoverinfo controls the display, setting it to show "y" only
This shows the smoother se would appear to be trace 5, but changing trace 5 hoverinfo has no effect on the display for the se lines