0

How can i get a date on hover instead of a number in plotly?

My sample code

require(plotly)
require(tidyverse)

dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")
dat <- tibble(a = rnorm(5), b = as.Date(dates, "%m/%d/%y")) 

p <- dat %>% 
  ggplot(aes(b, a)) +
  geom_line()

ggplotly(p)

Results in: I would expect b to be Feb 01 instead of 8066. How can i get this?

enter image description here

C8H10N4O2
  • 16,948
  • 6
  • 87
  • 123
Rentrop
  • 19,848
  • 8
  • 66
  • 97
  • closely related question -- https://stackoverflow.com/questions/44475567/r-error-in-date-visualization-of-ggplotly?rq=1 -- suggests installing the dev version of `ggplot2` from github -- otherwise, workaround suggested below – C8H10N4O2 Aug 28 '17 at 17:21

1 Answers1

1

As a workaround, you can use the basic plot_ly function, as in:

plot_ly(dat,
        x = ~b,
        y = ~a) %>% add_lines()

which displays the date correctly on hover.

enter image description here

C8H10N4O2
  • 16,948
  • 6
  • 87
  • 123
  • you can also create [custom hover text](https://plot.ly/r/text-and-annotations/#custom-hover-text) with `plot_ly`. I don't use ggplotly though, so no idea how to solve in that package. – C8H10N4O2 Aug 28 '17 at 17:16