Home > Blockchain >  Plotting confidence interval from the data in Ploty R
Plotting confidence interval from the data in Ploty R

Time:01-18

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.

CodePudding user response:

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

CodePudding user response:

Please update the array code as below

array=lower_int - upper_int

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


fig

enter image description here

  • Related