Search code examples
rggplot2r-plotly

Plotly R How to apply unsymmetrical error Bars on Time Series plot


Hello I am new to plotly in R.

My data is time series with a 95 % credible interval around each point. This credible interval is not symmetric. The packages I have loaded

library(ggplot2)
library(stringr)
library(dplyr)
library(plotly)

I would like to replicate this ggplot with plotly

ggplot(summarydata, mapping = aes(x = as.Date(Date), y = c.50.))+
  geom_errorbar(aes(ymin = c.2.5., ymax = c.97.5.), color ="grey", alpha = .90)+
  geom_point()+
  scale_x_date(date_labels = "%m-%Y")

enter image description here

My attempts to replicate this in plotly have not succeed

summarydata %>% 
  plot_ly() %>% 
  add_markers(x = ~as.Date(Date), y = ~c.50.,
              error_y = ~list(symmetric = FALSE,
                              arrayminus = c.2.5.,
                              array = c.97.5.,
                              color = '#000000'))

enter image description here

As you can see the plots error bars are wildly different. I am not sure why this is the case. The closest I've gotten to replicated my ggplot is with the following

summarydata %>% 
  plot_ly() %>% 
  add_markers(x = ~as.Date(Date), y = ~c.50.) %>% 
  add_markers(x= ~as.Date(Date), y = ~c.97.5., symbol = I("line-ew"), size = 1.5, color = I('rgba(128,128,128, .5)')) %>% 
  add_markers(x= ~as.Date(Date), y = ~c.2.5., symbol = I("line-ew"), size = 1.5, color = I('rgba(128,128,128, .5)'))

enter image description here

This adds the ticks for the bars as their own marker but when mousing over the plot it is a bit busy to look at. Eventually I am developing a shiny app that will allow users to click on the points and examine them further with supporting graphs. This further business is not ideal

Here is a sample of what my vectors look like that I am working with

summarydata$c.50.[1:10]
 [1] 0.6745592 0.6517352 0.6289426 0.7397868 0.7482902 0.6996212 0.6609487        NA        NA        NA

summarydata$Date[1:10]
 [1] "2021-07-07" "2021-07-10" "2021-07-21" "2021-07-23" "2021-07-24" "2021-07-27" "2021-07-28" "2020-04-12" "2020-04-16" "2020-04-18"

summarydata$c.97.5.[1:10]
 [1] 0.6879280 0.7240666 0.6506357 0.7425815 0.7500839 0.7026279 0.7130590        NA        NA        NA

summarydata$c.2.5.[1:10]
 [1] 0.6606896 0.6062380 0.6065747 0.7367133 0.7463247 0.6953307 0.5981074        NA        NA        NA

Solution

  • I figured it out. First off error_y needs the change value and not the value itself

      add_markers(x = ~as.Date(Date), y = ~c.50.,
                  error_y = ~list(symmetric = FALSE,
                                  arrayminus = c.50. - c.2.5., #The change value
                                  array = c.97.5. - c.50.,
                                  color = '#000000')
    
    

    My second problem is that error_y does not like missing values in their array. x and y have missing and plotly can handle that but for error_y it cannot (I think)

    So the complete plot the mimics the ggplot.

    summarydata %>%
      filter_all(all_vars(!is.na(.))) %>%  #error y does not like missing values.
      plot_ly() %>% 
      add_markers(x = ~as.Date(Date), y = ~c.50.,
                  error_y = ~list(symmetric = FALSE,
                                  arrayminus = c.50. - c.2.5.,
                                  array = c.97.5. - c.50.,
                                  color = '#000000'))
    

    enter image description here