0

I would like to add a vertical line on a special date to my plot. I am using the plot_ly() function from the plotly package in R.

 p <- plot_ly(dt.allDataFvsS, x = dt.allDataFvsS$date, y = dt.allDataFvsS$meanDifference, mode = 'lines',
              type = "scatter", line = list(color = " #007d3c")) %>%
      layout(title = "Average Price Difference Forward vs. Spot", xaxis = list(title = "Date"),
             yaxis = list(title = "EUR / MWh"))
 

     

The date has the following form: e.g. "2018-10-01" ("Y-M-D")

My plot looks like this:

PLOT

So, my question is how do I get a black vertical line at date "2018-10-01" ? Is it also possible to have vertical lines at the beginning of each year? Or of every quarter (Jan - Apr - Jul - Oct) of each year?

I would be very happy if someone could help me? Unfortunately, after a day of browsing the internet, I couldn't find anything that worked.

MikiK
  • 398
  • 6
  • 19
  • https://stackoverflow.com/questions/34093169/horizontal-vertical-line-in-plotly does this SO question solve your problem? There a few answers that address a similar issue (I think) – Mike Sep 01 '20 at 13:08
  • Thank you for your information! @vestland – MikiK Sep 02 '20 at 05:34
  • @MichaelaKordasch You're welcome! And welcome to the forum! Do not get discouraged if this question gets closed as a duplicate. Keep learning [tag:plotly] and don't hesitate to ask another question. – vestland Sep 02 '20 at 05:46

2 Answers2

0

As no data is provided to reproduce your issue, I would suggest next approach using add_trace() function where you define the coordinates for the line:

p <- plot_ly(dt.allDataFvsS, x = dt.allDataFvsS$date, y = dt.allDataFvsS$meanDifference, mode = 'lines',
             type = "scatter", line = list(color = " #007d3c")) %>%
  add_trace(x =as.Date("2018-10-01"),type = 'scatter', mode = 'lines',
            line = list(color = 'black'),name = '') %>%
  layout(title = "Average Price Difference Forward vs. Spot", xaxis = list(title = "Date"),
         yaxis = list(title = "EUR / MWh"))

Let me know if that works!

Duck
  • 39,058
  • 13
  • 42
  • 84
  • Thank you sooooo much, @Duck! Do you possibly know whether I have to enter the quarters manually or whether there is a command that does it automatically? – MikiK Sep 01 '20 at 13:13
  • @MichaelaKordasch Hi Michaela, there is a way to do that. You can define a dataframe with the dates you want the vertical lines (it can be specific days or the quarters you mentioned) and then use the same `add_trace()` to pass the dates and you will have the lines. I hope that was clear and helpful for you :) – Duck Sep 01 '20 at 13:16
  • @MichaelaKordasch If you think this answer was useful, you could accept it by clicking the tick on the left side of this answer. It is up to you :) – Duck Sep 01 '20 at 13:27
  • Hi @Duck :) Thank you for your help. I have now created a data frame like this: ```v.years <- as.Date(c("2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01")) dt.dateYears <- as.data.frame(v.years) ```, but with ```add_trace(data = dt.dateYears, type = 'scatter', mode = 'lines', line = list(color = "black"), name = '')``` this doesn't plot the lines. – MikiK Sep 02 '20 at 04:54
  • @MichaelaKordasch Hi Michaela, I have answered your new question with the issue you mentioned. I sketched a solution. You can adjust for your original data. I hope that helps :) – Duck Sep 02 '20 at 12:31
  • thank you !!!! Very nice from you, now its working perfectly :) – MikiK Sep 02 '20 at 12:45
  • @MichaelaKordasch Amazing Michaela, for sure you are pretty smart and will design awesome `plotly` visualizations :) – Duck Sep 02 '20 at 12:47
0

Here is another approach based on shapes rather than adding another trace (you can see the difference when zooming out + there is no legend item for the vertical line):

library(plotly)

dates <- seq(from = as.Date("2018-08-01"), to = as.Date("2018-12-31"), by = 1)
dt.allDataFvsS <- data.frame(date = dates, meanDifference = sin(seq_along(dates)*0.1))

vline <- function(x = 0, color = "red") {
  list(
    type = "line", 
    y0 = 0, 
    y1 = 1, 
    yref = "paper",
    x0 = x, 
    x1 = x, 
    line = list(color = color)
  )
}

p <- plot_ly(dt.allDataFvsS, x = dt.allDataFvsS$date, y = dt.allDataFvsS$meanDifference, mode = 'lines',
             type = "scatter", line = list(color = " #007d3c")) %>%
  layout(title = "Average Price Difference Forward vs. Spot", xaxis = list(title = "Date"),
         yaxis = list(title = "EUR / MWh"))

p %>% layout(shapes = list(vline(as.Date("2018-10-01"))))

result

Based on this answer.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • do you know whether I have to enter the quarters of beginning of years manually or whether there is a command that does it automatically? – MikiK Sep 02 '20 at 04:26
  • In the underlying JS library they just updated the time format option to [support quarters](https://github.com/plotly/plotly.js/pull/5026). However, I don't know when the plotly R library will be updated. Accordingly up to know this is manual work, I guess. – ismirsehregal Sep 02 '20 at 05:29
  • Moreover [this](https://stackoverflow.com/a/23046072/9841389) might be helpful. – ismirsehregal Sep 02 '20 at 06:47