0

I want to visualize actual dates format instead of numbers (transformed by default) using ggplotly graphs when I put the cursor over the data points. Thanks in advance!

Here is a simple example:

 require(plotly)
 require(ggplot2)
    x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
    y <- c(25, 50, 75, 100)
    x_lab <- "date"
    y_lab <- "score"
    (mydata <- as.data.frame(cbind(x,y)))
    mydata$x <- as.Date(mydata$x, "%d/%m/%Y")
    ggplot(mydata, aes(x=x, y=y)) +
      geom_point()
   ggplotly()

Click here to see the plot

  • Your problem is here: `mydata <- as.data.frame(cbind(x,y))`. Using `mydata <- data.frame(x = c("01/01/2007","04/03/2008","28/11/2008","13/06/2009"), y = c(25, 50, 75, 100))` follow with `mydata$x <- as.Date(mydata$x, "%d/%m/%Y")` works. – MLavoie May 16 '18 at 23:57
  • Thanks MLavoie, but actually although the x-axis is showed in the right date format when I put the cursor over the points in the ggploty graph it stillshows the x coordinate as numeric dates. – Sergio Nolazco May 17 '18 at 00:28
  • have a look at this: https://stackoverflow.com/questions/50142512/number-instead-of-date-on-the-density-plot-in-r/50143617#50143617 – MLavoie May 17 '18 at 08:55

1 Answers1

0

Using plot_ly() function instead of ggplotly() solves the problem!!

Here is the code with the same data:

require(ggplot)
require(plotly)
x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
y <- c(25, 50, 75, 100)
x_lab <- "date"
y_lab <- "score"
mydata <- as.data.frame(cbind(x,y))
mydata$x <- as.Date(mydata$x, "%d/%m/%Y")
str(mydata)
plot_ly(mydata, x = ~x, y = ~y, type = 'scatter', mode = 'markers',
        marker = list(size = 10))

Click here too see the new plot

Alternatively, it can still be done using ggplotly!

require(ggplot)
require(plotly)
x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
y <- c(25, 50, 75, 100)
x_lab <- "date"
y_lab <- "score"
mydata <- as.data.frame(cbind(x,y))
mydata$x <- as.Date(mydata$x, "%d/%m/%Y")
myplot <- ggplot(mydata, aes(x=x, y=y, label1=x, label2=y)) +
  geom_point()
ggplotly(myplot, tooltip = c("label1", "label2"))

New plot using ggplotly function!

  • Thanks to @Vedha Viyash on this one! By answering another question posted in this online community, he showed me how to plot these data using this function. – Sergio Nolazco May 17 '18 at 09:42
  • Yes @MLavoie, I know. I did it before in ggplot, however the plot constructed with my real data involves hundreds of points. In that case, plotly package is better to visualize one-by-one point coordinates. In this simple example does not matter since labels would not be overlapped. – Sergio Nolazco May 18 '18 at 02:03
  • @MLavoie unless you can do it in ggplot using shiny or something that makes it interactive. It is possible? – Sergio Nolazco May 18 '18 at 02:05
  • Your original question was with `ggplotly`. I simply meant that if you follow this [link](https://stackoverflow.com/questions/50142512/number-instead-of-date-on-the-density-plot-in-r/50143617#50143617) you will be able to fix your date problem you are having. – MLavoie May 18 '18 at 08:26
  • Thanks MLavoie! It works. I am including that in the answer. – Sergio Nolazco May 18 '18 at 09:09