R plot_ly():根据时间数据向绘图添加多条垂直线

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

我需要一些帮助来根据时间数据绘制多条垂直线。 我的时间数据数据框定义如下:

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) %>%

我知道如何为单个日期执行此操作,正如您在我上面的代码中看到的那样,我在其中使用了以下内容:

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

我的情节实际上是这样的: Rplot

所以,我的问题是:如何绘制与我的数据框中的时间数据完全匹配的多条垂直线df.dateYears

您可以简单地在循环中使用 add_trace

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

我建议使用循环和您在问题中提到的日期的原始向量来使用这种方法。我已经添加了示例数据并修改了您的日期向量,但是对于您的原始数据,它必须可以正常工作:

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")
}

输出: