Search code examples
rplotlydensity-plotr-plotlyggplotly

number instead of date on the density plot in R


I have a problem. I am trying to draw a density plot in R with the plotly package. But when I hover the mouse over the plot I get number format of date 17623 instead 2018-04-02. Any ideas how to solve this problem?

Date <- seq(as.Date(Sys.Date()-30), by=1, len=31)
Value <- runif(31)
dataTable <- data.frame(Date, Value)

density_plot = ggplot(dataTable, aes(x=Date, y = Value)) +
  geom_density(stat="identity", fill = "#4156C5") + 
  labs(fill = "Value", x = "Date", y = "Frequency")
ggplotly(density_plot)

enter image description here


Solution

  • Based on this: how to choose variable to display in tooltip when using ggplotly?

    You could try this:

    density_plot = ggplot(dataTable, aes(x=Date, y = Value, label1= Date, label2 = Value)) +
      geom_density(stat="identity", fill = "#4156C5") + 
      labs(fill = "Value", x = "Date", y = "Frequency")
    
    ggplotly(density_plot, tooltip = c("label1", "label2"))
    

    enter image description here