Using the following reprex, when hovering over the upper or lower line, the tooltip keeps showing the same values. Why is that and is there any workaround?
library(ggplot2)
library(plotly)
df <- data.frame(a = c(2020,2021,2022,2020,2021,2022),
b = c('Product A','Product A','Product A','Product B','Product B','Product B'),
c = c(500,540,630,70,140,100))
plot <- ggplot(df, aes(x = a, y = c, fill = b)) +
geom_area(position = 'stack')
# geom_bar(stat = 'identity', position = 'stack')
ggplotly(plot)
If you comment out the geom_area row and include the geom_bar row, you will notice the problem disappears.
This is due to a change in
ggplot2(introduced sometime in the last year or so) which for ageom_areanow usesstat="align"and does some interpolation. From the docs (?geom_area)Apprarently this change is not supported by plotly (and I'm wondering whether this feature can be supported at all). As a result the
textattribute will default to the value of the first data point (figured that out by inspecting theplotly_json()object and when trying to manually add the tooltip text).But as the docs suggest you can fix that by switching to
stat="identity":