Graph Error Bars Easily with Plotly and R!

If you don’t have a Plotly username and key you can sign up for free here: https://plot.ly/feed/

If it’s your first time using Plotly- uncomment both the install package line and the set_credentials line (and add your username and key). Add the plotly library.

#install.packages("plotly")
library(plotly)
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: RJSONIO
## Loading required package: ggplot2
py <- plotly()
#set_credentials_file(username = 'your_username', key = 'your_key')

Vertical Error Bars (by value and percent) on a Line Graph

Add Data: x-axis, y-axis line 1, y-axis line 2, standard error values (previously calculated to be used with line 1).

city = c("Halifax", "Montreal", "Quebec", "Ottawa", "Toronto", "Calgary", "Vancouver")
snow = c(230.5, 217.5, 315.9, 235.7, 115.4, 126.7, 48.2)
rain = c(145.2, 97.9, 123.0, 94.4, 79.3, 41.3, 119.9)
stan_err = c(8.2, 14.1, 17.7, 9.7, 7.2, 11.1, 4.2)

Create line 1 (mean snow) and set vertical error bars using the standard error array defined in the data above. This will result in an error bar that represents +1 standard error value above and -1 standard error value below the mean.

trace1 <- list(
        x = city, 
        y = snow, 
        #Vertical Symmetric Error Bars by value
        error_y = list(
                  type = "data", 
                  array = stan_err), 
        name = "Snow",
        type = "scatter")

Create line 2 (mean rain). For this line, we’ll create error bars with a percent value instead of a dara array. Define type as percent and set the value to the desired percent.

trace2 <- list(
        x = city,
        y = rain,
         #Vertical Symmetric Error Bars by percent
        error_y = list(
                  type = "percent", 
                  value = 15), 
        name = "Rain",
        type = "scatter")

Define Data

data <- list(
        trace1,
        trace2)

Define any layout information and lables that you want to add.

layout <- list(
          title = "Mean and Standard Error of Annual Precipitation in Canadian Cities",
          xaxis = list(
                  title = "City"),
          yaxis = list(
                  title = "cm"))

Add Plotly code and define your filename:

response <- py$plotly(data, 
                      kwargs = list(
                              layout = layout, 
                              filename = "YOUR_FILENAME", 
                              fileopt = "overwrite"))

Horizontal, Asymetric Error Bars on a Bar Graph

Add Data

month = c("Dec", "Nov", "Oct", "Sep", "Aug", "July", "June", "May", "April", "Mar", "Feb", "Jan")
month_snow = c(49, 19, 3, 0, 0, 0, 0, 1, 14, 37, 41.5, 53)
high_conf_int = c(1, 0.5, 1.5, 1, 0, 0, 0, .5, 3.5, 2, 1.5, 3)
low_conf_int = c(3, 4, 1, 0, 0, 0, 0, .75, 3, 2.25, 1, 2)

Create a horizontal bar graph. In error_x set symmetric equal to FALSE and add 2 arrays: array = Upper_Error_Bar_Values and arrayminus = Lower_Error_Bar_Values

data <- list(
        x = month_snow, 
        y = month, 
        #Horizontal Asymmetric Error Bars
        error_x = list(
                  type = "data", 
                  symmetric = FALSE, 
                  arrayminus = low_conf_int,
                  array = high_conf_int), 
        orientation = "h",
        type = "bar")

Add layout information and define your filename:

layout <- list(
          title = "Snowfall in Montreal by Month",
          xaxis = list(
                  title = "95% Confidence Interval and Mean Snowfall in cm"),
          yaxis = list(
                  title = "Month"))

response <- py$plotly(data,
                      kwargs = list(
                              layout = layout,
                              filename = "YOUR_FILENAME",
                              fileopt = "overwrite"))