Plotting confidence interval from the data in Ploty R

Trying to add the error bars on the bar graph.

library(plotly)
library(plyr)

year <- c("1993","1994","1995","1996")
incident <- c(123.2,489.2,281.3,892.1)
lower_int <- c(32.1,23.1,11.1,34.2)
upper_int <- c(45.3,43.5,25.6,59.0)
data <- data.frame(year, incident, lower_int, upper_int)

fig <- plot_ly(data, x = ~year, y = ~incident, type = 'bar',
        error_y = ~list(y_min = ~lower_int,
                        y_max = ~upper_int))


fig

This code just plots the bar chart but the intervals are not shown on the bars.

>Solution :

You can use array and arrayminus and remove the ~ preceding the list

fig <- plot_ly(data, x = ~year, y = ~incident, type = 'bar',
               error_y = list(
                 color = "black",
                 array = upper_int,
                 arrayminus = lower_int))

fig

enter image description here

Leave a Reply