ggplotly() does not display geom_vline / geom_hline when data is POSIXct
Asked Answered
B

1

9

I am trying to make a graph with "time markers". These time markers are vertical lines for certain dates. Time data are POSIXct format. I would like to use the awesome interactive interface of Plotly and use my ggplot objects in it.

The problem is that these "time markers" doesn't show in after using ggplotly(). I ave already tried with plotly::add_segments() but it does not work. Here are two reproductible examples :

1. With non-POSIXct data it works fine

# dummy dataset
df2 = data.frame(id = 1:10, measure = runif(10, 0, 20))
events2 = data.frame(number = c(2,3,8))
# ggplot graph
p2 = ggplot() + geom_line(data = df2, aes(x = id, y = measure))  +
  geom_vline(data = events2, aes(xintercept = events2$number), color = "red")
p2
# plotly graph that displays the geom_vline properly
ggplotly(p2)

2. With POSIXct data is doesn't display the correct result

# dummy dataset
df = data.frame(date = seq(as.POSIXct("2017-07-01", tz = "UTC", format = "%Y-%m-%d"),
                           as.POSIXct("2018-04-15", tz = "UTC", format = "%Y-%m-%d"),
                           "1 month"),
                measure = runif(10, 0, 20))
events = data.frame(date_envents = as.POSIXct(c("2017-10-12", "2017-11-12", "2018-03-15"), tz = "UTC", format = "%Y-%m-%d"))
# ggplot graph
p = ggplot() + geom_line(data = df, aes(x = date, y = measure))  +
  geom_vline(data = events, aes(xintercept = events$date), color = "red")
p
# plotly graph that does not display the geom_vline properly
ggplotly(p)

I have seen some workaround (like this one : Add vertical line to ggplotly plot) but it is "complicated". Is there a more simple way to solve this problem ?

I am using Windows 10 with R version 3.5.0, RStudio and the following packages : library(tidyverse) and library(plotly)

Bonaventure answered 10/10, 2018 at 7:38 Comment(0)
T
16

A simple workaround is to set the xintecept of the geom_vline to numeric.

sample data

df = data.frame(date = seq(as.POSIXct("2017-07-01", tz = "UTC", format = "%Y-%m-%d"),
                           as.POSIXct("2018-04-15", tz = "UTC", format = "%Y-%m-%d"),
                           "1 month"),
                measure = runif(10, 0, 20))
events = data.frame(date_envents = as.POSIXct(c("2017-10-12", "2017-11-12", "2018-03-15"), tz = "UTC", format = "%Y-%m-%d"))

code

p = ggplot() + geom_line(data = df, aes(x = date, y = measure))  +
  geom_vline(data = events, aes(xintercept = as.numeric(events$date)), color = "red")

result

ggplotly(p)

enter image description here

Traverse answered 10/10, 2018 at 7:56 Comment(4)
thanks for this, that's great. The only problem is that the label on the vertical lines shows the numeric transformation rather than the date. Is there a way to force the label to the date format?Bardo
@Bardo probably as easy as using as.Date on the labelsTraverse
sadly no, i did try that but it looks like label is not an accepted parameter in geom_vline. I tried both geom_vline(xintercept = as.numeric(my_dates), label = as.Date(tmy_dates)) and vline(xintercept = as.numeric(my_dates), aes(label = as.Date(my_dates)))Bardo
@Bardo See my answer here for a working solution. Seems like the text aesthetic is able to do the trick.Rambunctious

© 2022 - 2024 — McMap. All rights reserved.