Adds and customizes error bars or shaded areas in roboplot()
s.
Use this function to visually represent uncertainty in the data by adding error bars or shaded areas, based on the specified type.
Usage
set_confidence_interval(
type = NULL,
confidence = NULL,
bar_color = NULL,
opacity = NULL,
smoothing = NULL,
start_type = "flat",
stop_type = "flat",
show_legend = TRUE
)
Arguments
- type
Character. Specifies the type of confidence display: "bars" for error bars or "area" for shaded areas.
- confidence
Symbol or character. Columns from
d
inroboplot()
that provide the labels (if character or factor) or values (if numeric) for error areas and bars.- bar_color
Characters. Colors for error bars. Must be hexadecimal colors or valid CSS colors. Only applicable when
type = "bars"
.- opacity
Numeric. Opacity of the shaded area, ranging from 0 (completely transparent) to 1 (completely opaque). Only applicable when
type = "area"
.- smoothing
Numeric. Smoothing factor for the shaded area, typically between 0 and 1. Only applicable when
type = "area"
.- start_type
Currently inactive. Character. Defines how the shaded area starts when
type = "area"
. Use "flat" or "point". Default is "flat".- stop_type
Currently inactive. Character. Defines how the shaded area ends when
type = "area"
. Use "flat" or "point". Default is "flat".- show_legend
Logical. Whether to show the legend item for the shaded area. Default is TRUE.
Details
set_confidence_interval()
replaces the deprecated set_errorbars()
function. Use this function to add either error bars or shaded areas to your plots by setting the type
parameter.
When type = "bars"
, you can specify error_y
, and ycolor
to customize the appearance of error bars.
When type = "area"
, you can adjust the opacity
and smoothing
of the shaded area to represent the confidence range visually.
Examples
# When the error is symmetrical for each time point, use `set_confidence_interval()`
# use `set_confidence_interval()` to add error bars to a plot where the confidence
# interval is a numeric column. The given value will be added to or subtracted from
# the trace the confidence interval value relates to by `roboplot()` param `color`.
d <- energiantuonti |>
dplyr::filter(Alue == "Venäjä", Suunta == "Tuonti") |>
dplyr::mutate(confidence_interval = sd(value))
d |>
roboplot(
Suunta,
pattern = Alue,
title = "Energian tuonti",
subtitle = "Virhepalkeilla",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval(type = "bars")
)
# Or use `type = "area"` to add confidence area to a plot.
d |>
roboplot(Suunta,
title = "Energian tuonti",
subtitle = "Luottamusalueella",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval("area"))
# Change the color of the error bars
d |>
roboplot(
Suunta,
title = "Energian tuonti",
subtitle = "Luottamusalueella",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval("bars", bar_color = "green")
)
# Change opacity and smoothing of the confidence area, hide the area from legend,
# and pass the column name explicitly if it not called "confidence_interval".
d |>
dplyr::rename(conf = confidence_interval) |>
roboplot(
Suunta,
title = "Energian tuonti",
subtitle = "Luottamusalueella",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval(
"area",
confidence = conf,
opacity = 0.1,
smoothing = 0.9,
show_legend = FALSE
)
)
# Create custom confidence intervals using `set_confidence_interval()`.
# The column used for the confidence interval should be a character or factor,
# describing the labels of the confidence areas. The color of the areas correspond
# to the `color` parameter of `roboplot`. The main trace should be refererred
# either as `NA` or the name of your y-axis column. For this example, arbitrary
# confidence areas are created.
d <- energiantuonti |>
dplyr::filter(Alue == "Venäjä") |>
dplyr::mutate(`95%hi` = purrr::map_dbl(value,~ .x + .x * runif(1) * 0.2),
`95%lo` = purrr::map_dbl(value,~ .x - .x * runif(1) * 0.2),
`95%` = purrr::map2_dbl(`95%hi`,`95%lo`,mean),
`70%hi` = purrr::map_dbl(`95%hi`, ~ .x + .x * runif(1) * 0.3),
`70%lo` = purrr::map_dbl(`95%lo`, ~ .x - .x * runif(1) * 0.3),
`70%` = purrr::map2_dbl(`70%hi`,`70%lo`,mean)
) |>
tidyr::pivot_longer(dplyr::where(is.numeric), names_to = "confidence_interval") |>
dplyr::mutate(confidence_interval = confidence_interval |>
stringr::str_remove("(hi|lo)$") |>
forcats::fct_relevel(c("95%"))
) |>
dplyr::filter(confidence_interval == "value" | time >= "2020-01-01")
d |>
roboplot(
Suunta,
"Confidence interval",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval("area", start_type = "point")
)
# Start the area from the last point of the trace where you have no confidence interval values.
d |>
roboplot(
Suunta,
"Confidence interval",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval("area", start_type = "point")
)
# Works when you want to skip the line of the trace, only draw the area.
d |>
dplyr::filter(Suunta == "Tuonti", confidence_interval != "value") |>
roboplot(
Suunta,
"Confidence interval",
caption = "Tilastokeskus",
confidence_interval = set_confidence_interval("area")
)
# For error_bars, you can also use a character or factor column if the values
# won't be symmetrical for each time point. For more than one level of confidence,
# use `type = "area"`.
d |>
dplyr::filter(confidence_interval %in% c("value","95%")) |>
roboplot(
Suunta,
"Confidence interval",
caption = "Tilastokeskus",
pattern = Alue,
confidence_interval = set_confidence_interval("bars")
)