Miko
Miko

Reputation: 496

R plot_ly(): adding multiple vertical lines to a plot based on time data

I need some help plotting multiple vertical lines based on time data. My data frame with the time data is defined as following:

v.years <- as.Date(c("2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01", "2008-01-01", "2009-01-01",
                     "2010-01-01", "2011-01-01", "2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01",
                     "2016-01-01", "2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01"))
df.dateYears <- as.data.frame(v.years)

My code for the plot looks like this:

p <- plot_ly(dt.allDataFvsS, x = dt.allDataFvsS$date, y = dt.allDataFvsS$meanDifference, mode = 'lines',
             type = 'scatter', line = list(color = "#007d3c"), text = ~forwardProduct, 
             hovertemplate = paste("<b>%{text} vs. Spot</b><br>", "%{xaxis.title.text}:  %{x}<br>",
                                   "%{yaxis.title.text}:  %{y}<br><extra></extra>")) %>%
     add_trace(x = as.Date("2018-10-01"), type = 'scatter', mode = 'lines',
               line = list(color = "red", dash = "dash"), text = "Price Zone Separation",
               hovertemplate = paste("<b>%{text}</b><br>", "%{xaxis.title.text}:  %{x}<br><extra></extra>")) %>%
     layout(title = "Average Price Difference Forward vs. Spot", xaxis = list(title = "Date"), 
            yaxis = list(title = "EUR/MWh"), showlegend = FALSE) %>%

I know how to do it for a single date as you can see in my code above, where I used following:

trace_add(x = as.Date("2018-10-01"), ...)

My plot looks actually like this: Rplot

So, my question is: How do I plot multiple vertical lines exactly matching the time data in my data frame df.dateYears?

Upvotes: 3

Views: 2319

Answers (2)

Duck
Duck

Reputation: 39595

I would suggest this approach using a loop and the original vector of dates you mentioned in the question. I have added example data and modified your date vector but with your original data it must work fine:

library(plotly)
library(dplyr)
set.seed(123)
#Sample data
mydata <- data.frame(date=seq(as.Date('2017-01-01'),as.Date('2021-12-31'),length.out = 30),
                     meanDifference=round(runif(30,0,15),0),stringsAsFactors = F)
#Plot
p <- plot_ly(mydata, x = ~date, y = ~meanDifference, mode = 'lines',
             type = 'scatter', line = list(color = "#007d3c"), text = ~meanDifference)
#New data
v.years <- as.Date(c("2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01"))
#Add lines with loop
for(i in 1:length(v.years))
{
  p <- p %>%
    add_trace(x = v.years[i], type = 'scatter', mode = 'lines',
              line = list(color = "red", dash = "dash"), text = "Price Zone Separation")
}

Output:

enter image description here

Upvotes: 2

ismirsehregal
ismirsehregal

Reputation: 33417

You can simply use add_trace in a loop:

library(plotly)

dates <- seq(from = as.Date("2004-01-01"), to = as.Date("2020-12-31"), by = 12)
dt.allDataFvsS <- data.frame(date = dates, meanDifference = seq_along(dates))

v.years <- as.Date(c("2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01", "2008-01-01", "2009-01-01",
                     "2010-01-01", "2011-01-01", "2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01",
                     "2016-01-01", "2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01"))
df.dateYears <- as.data.frame(v.years)


p <- plot_ly(dt.allDataFvsS, x = dt.allDataFvsS$date, y = dt.allDataFvsS$meanDifference, mode = 'lines',
             type = 'scatter', line = list(color = "#007d3c"), text = ~"forwardProduct", 
             hovertemplate = paste("<b>%{text} vs. Spot</b><br>", "%{xaxis.title.text}:  %{x}<br>",
                                   "%{yaxis.title.text}:  %{y}<br><extra></extra>")) %>%
  add_trace(x = as.Date("2018-10-01"), type = 'scatter', mode = 'lines',
            line = list(color = "red", dash = "dash"), text = "Price Zone Separation",
            hovertemplate = paste("<b>%{text}</b><br>", "%{xaxis.title.text}:  %{x}<br><extra></extra>")) %>%
  layout(title = "Average Price Difference Forward vs. Spot", xaxis = list(title = "Date"), 
         yaxis = list(title = "EUR/MWh"), showlegend = FALSE)


for(v.year in df.dateYears$v.years){
  p <- add_trace(p, x = as.Date(v.year, origin = "1970-01-01"), type = 'scatter', mode = 'lines',
            line = list(color = "red", dash = "dash"), text = "Price Zone Separation",
            hovertemplate = paste("<b>%{text}</b><br>", "%{xaxis.title.text}:  %{x}<br><extra></extra>"))
}

p

result

Upvotes: 1

Related Questions