简体   繁体   English

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

[英]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我的情节实际上是这样的: Rplot

So, my question is: How do I plot multiple vertical lines exactly matching the time data in my data frame df.dateYears ?所以,我的问题是:如何绘制与数据框df.dateYears的时间数据完全匹配的多条垂直线?

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:输出:

在此处输入图片说明

You can simply use add_trace in a loop:您可以简单地在循环中使用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

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM