21

I am using ggplotly to show an interactive time-series plot. The x axis is in date format, yet the hover tool tip in plotly is converting the date format to a numeric (screenshot attached). Any ideas on how to get the date to show as a proper date in the tooltip?

Below is a short piece of the code:

 output$ggplot <- renderPlotly({

plotbycity<-df_postgres %>% group_by(city, date, bedroooms) %>%
  filter(city %in% input$checkGroup & bedroooms==input$radio) %>%
  summarise(count=n(),rent=median(rent)) %>%
  ungroup() 

plotbycity$date<-as.Date(plotbycity$date)


# Error handling
plotbycity<-plotbycity[!is.na(plotbycity$city),]
if (is.null(plotbycity)) return(NULL)

#plotbycity<-ungroup(plotbycity)
#dat <- dat[c("rent", "bedroooms", "date", "City")]
#dat <- melt(dat,id.vars=c("date", "City", "bedroooms"),na.rm=TRUE)   #

# draw the line plot using ggplot
 gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
                             text = paste('obs: ', count))) +
  geom_line() +
  ggtitle("Monthly Rents")
#   #theme_hc(bgcolor = "darkunica") +
#   #scale_fill_hc("darkunica")
 
 p <- ggplotly(gg, tooltip = c("x", "y", "text"))

dev.doc
  • 569
  • 3
  • 12
  • 18
user5831311
  • 273
  • 2
  • 8

1 Answers1

30

If you use just text in your tooltip, you can render a more complex tooltip by using the text element you pass to ggplot. You just need to call as.Date and use some <br> html tags as follows:

# draw the line plot using ggplot
gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
    text = paste('Rent ($):', rent,
                 '<br>Date: ', as.Date(date),
                 '<br>Obs: ', count))) +
    geom_line() +
    ggtitle("Monthly Rents")

p <- ggplotly(gg, tooltip = c("text"))

Hope that helps!

jadianes
  • 516
  • 5
  • 10
  • 1
    Great stopgap solution. Though it seems like a something ggplotly should understand (data type), therefore a bug worth submitting... – Mike Dolan Fliss Sep 01 '17 at 17:43
  • Totally agree @MikeDolanFliss – jadianes Sep 13 '17 at 10:05
  • 1
    Also, in case you have multiple variable this solution does not let you write the name of the variable in the tooltip, although the tooltip gets a different color for each variable. See https://www.dropbox.com/s/zts1vghb004f6an/ggvisDateProblem.R?dl=0 – user2955884 Sep 29 '17 at 05:32
  • The name of the variable is part of the tooltip. I'm not sure I see the problem your pointing at... – jadianes Sep 29 '17 at 07:57
  • 6
    If you tried this solution and your lines disappear, follow-up with this: https://stackoverflow.com/questions/46241436/geom-line-not-plotting-when-tooltip-argument-set-in-ggplotly?rq=1 – Khashir May 03 '18 at 17:22
  • 3
    @Khashir Thanks for the comment, literally did the "text = ... " solution and then my lines disappeared... and I started cursing that now I had another problem haha – BigTimeStats Jun 09 '18 at 21:05
  • 1
    My lines disappeared as well initially. I found if I add a geom_point() layer after the geom_line, and put the text aes in the geom_point the lines stay put. – MelissaG Jul 16 '19 at 20:11