plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Tooltip of ggplotly's geom_area not updating along the x axis

Open todd-davidson-beis opened this issue 1 year ago • 2 comments

Recently, the hover text/tooltip when using geom_area() only shows the values for the first element along the x axis - in the example below, the geom_col graph has the expected tooltips, but the geom_area is stuck with the first value.

library(tidyverse)
economics %>% {ggplot(., aes(x=date,y=pop)) + geom_area()} %>% plotly::ggplotly()
economics %>% {ggplot(., aes(x=date,y=pop)) + geom_col()} %>% plotly::ggplotly()

The problem seems to be in ggplotly() as the native plot_ly() approach still works as expected:

plotly::plot_ly(x=~economics$date, y=~economics$pop, type='scatter', mode='none', stackgroup='one')

In a more complicated graph (with fill groups and facets) other elements seem to update, but the x and y values are stick at the first element still - again, compare geom_area to geom_col results:

economics_status <- economics %>% mutate(pop = pop - unemploy) %>% pivot_longer(cols = c(pop,unemploy), names_to="status", values_to="number")

economics_status %>% 
    {ggplot(., aes(x=date,y=number, fill=status)) + geom_area()} %>% plotly::ggplotly()

economics_status %>% 
    {ggplot(., aes(x=date,y=number, fill=status)) + geom_col()} %>% plotly::ggplotly()

I'm getting the error using R 4.2.2, plotly version 4.10.1. Rolling back using devtools, I am getting the same error back to plotly 4.9.4 - but anything earlier won't load any output at all, so presumably clashing with something else in the ecosystem. It may be relevant that htmlwidgets is at 1.6.2 and htmltools is 0.5.5.

However, all worked fine with R 4.1.3 and plotly 4.10.0 ... which had htmlwidgets at 1.5.4 and htmltools at 0.5.3. But rolling back htmlwidgets with the other settings above does not fix things.

todd-davidson-beis avatar May 25 '23 13:05 todd-davidson-beis

Thanks. Looks like this problem was introduced in the v3.4.0 of ggplot2. ggplotly() should handle this better, but in the meantime, it can be fixed with the following:

remotes::install_version("ggplot2", "3.3.6")

cpsievert avatar May 25 '23 18:05 cpsievert

Thank you, Carson! A colleague of mine was also seeing this behaviour and managed to figure out that setting geom_area(stat = "identity") would rectify it. It seems the default in ggplot2 is now stat = "align" - the change was in a commit from July 2022 but only got released in 3.4.0 in Nov 2022.

todd-davidson-beis avatar May 30 '23 10:05 todd-davidson-beis