0

I have multiple time-series plots. An example plot and code can be found below. I construct the plot using ggplot2 and make it interactive using ggplotly().

However, date format on the smoothed curve get lost. Interactive chart shows date as some numeric values.

How can I fix the problem?

Thank you very much

enter image description here

structure(list(Date = structure(c(15736, 15764, 15795, 15825, 
15856, 15886), class = "Date"), CLI = c(99.93, 100.3, 100.96, 
100.71, 100.62, 101.15)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))


plot5 <- df %>% 
  ggplot(aes(x = Date, y = CLI))+
  geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4")+
  scale_x_date(date_breaks = "6 month", date_labels =  "%m/%y")+
  theme_pander()+
  geom_line(stat='smooth', method = "glm", alpha=0.5, color = "firebrick2", formula = y ~ poly(x, 5))+
  geom_ribbon(stat='smooth',method = "glm", se=TRUE,formula = y ~ poly(x, 5), alpha=0.01)+
  labs(x = "Date",
       y = "Composite Leading Indicator")
ggplotly(plot5)
Enes
  • 59
  • 5
  • Please consider adding sample data using `dput(df)` to reproduce the problem – Vishal A. Feb 07 '22 at 12:45
  • @VishalA. Hi, I attached it the dput() output. Please let me know if the format is not correct. Thanks. – Enes Feb 07 '22 at 12:54
  • What you mean by date format get lost? On the axis or do you mean the date format in the tooltip? In the last case have a look at this related issue and solution: [ggplotly showing numbers instead of date labels](https://stackoverflow.com/questions/70202425/ggplotly-showing-numbers-instead-of-date-labels/70204746#70204746) – stefan Feb 07 '22 at 13:18
  • Hi @stefan, I mean the tooltip. For the blue curve, the tooltip shows date properly. But it becomes a number for the smoothed (red) curve. – Enes Feb 07 '22 at 13:27

1 Answers1

1

Adapting my answer on this post to your case one option to get the date format in the tooltip would be to make use of the text aesthetic to manually create the tooltip and convert the numbers to proper dates like so:

plot <- df %>%
  ggplot(aes(x = Date, y = CLI)) +
  geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4") +
  scale_x_date(date_labels = "%m/%y") +
  # theme_pander()+
  geom_line(aes(text = paste(
    "date: ", as.Date(..x.., origin = "1970-01-01"), "<br>",
    "y:", ..y..
  )), stat = "smooth", method = "glm", alpha = 0.5, color = "firebrick2", formula = y ~ poly(x, 5)) +
  geom_ribbon(stat = "smooth", method = "glm", se = TRUE, formula = y ~ poly(x, 5), alpha = 0.01) +
  labs(
    x = "Date",
    y = "Composite Leading Indicator"
  )
ggplotly(plot, tooltip = c("text"))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Thanks. I tried it. It fixed the problem but caused the tooltip for the blue curve to get lost. So, I just copied the `aes(text = "..")` part into the first `geom_line`. The tooltip for both curves works properly now. But, I am curious if there is a better way to do that. – Enes Feb 07 '22 at 14:18
  • Aw. Sorry. Maybe there is an easier or out-of-the-box approach. But my guess is that there isn't. – stefan Feb 07 '22 at 15:07