Reputation: 52
I have a table of rates and confidence intervals I want to plot. Some of the rates (and their CI's) are suppressed according to data quality rules. When I plot series with missing values, the error bar values are assigned to the incorrect rate and the preceding rate is given (+ 0 / - 0) error bar values.
Reprex:
reprex <- tibble(year = as_factor(c(2016, 2017, 2018, 2019)),
rate = c(NA, 0.153, 0.123, NA),
lcl = c(NA, 0.0813, 0.0612, NA),
ucl = c(NA, 0.261, 0.219, NA)) %>%
mutate(difflow = rate-lcl,
diffhi = ucl-rate)
plot <- plot_ly()
plot <- add_trace(plot,
data = reprex,
connectgaps = F,
x = ~year,
y = ~rate, mode = 'markers+lines', type = "scatter",
error_y = ~list(type = "data",
array = ucl-rate,
arrayminus = rate-lcl,
color = "black"))
In the above plot, rates for 2016 and 2019 are correctly missing. The error confidence limits for 2017 are (+0 / -0) and the limits plotted for 2018 (+0.108 / -0.0717) match the values for diffhi and difflow of 2017. How do I correct this?
EDIT: I tried wrapping the array and arrayminus values in na.omit(). This works for the above reprex, but fails when additional NA's and data are introduced. Below, the 2017 rate now has 2018's confidence intervals and 2018 has no error bars. This is different from before when Plotly assigned error bar values of zero. Now they are just missing.
reprex <- tibble(year = as_factor(c(2013, 2014, 2015, 2016, 2017, 2018, 2019)),
rate = c(3, 2, NA, NA, 0.153, 0.123, NA),
lcl = c(2, 1, NA, NA, 0.0813, 0.0612, NA),
ucl = c(4, 5, NA, NA, 0.261, 0.219, NA)) %>%
mutate(difflow = rate-lcl,
diffhi = ucl-rate)
plot <- plot_ly()
plot <- add_trace(plot,
data = reprex,
connectgaps = F,
x = ~year,
y = ~rate, mode = 'markers+lines', type = "scatter",
error_y = ~list(type = "data",
array = na.omit(ucl-rate),
arrayminus = na.omit(rate-lcl),
color = "black"))
plot
Upvotes: 0
Views: 510
Reputation: 887651
We could replace the NA
with 0
library(dplyr)
library(tidyr)
reprex <- reprex %>%
mutate(across(where(is.numeric), replace_na, 0))
Applying the OP's code gives
Upvotes: 1