Create a bar chart for grouped numeric values.
Usage
plot_bar(
data,
x,
y,
position = "dodge",
facet_var = NULL,
facet_scale = "free",
facet_nrow = NULL,
facet_ncol = NULL,
color = NULL,
flip = FALSE,
reorder = FALSE,
title = NULL,
subtitle = NULL,
xlab = NULL,
ylab = NULL,
caption = NULL,
bar_size = 0.75,
bar_color = "grey35",
bar_alpha = 1,
theme_set = theme_tscv(),
theme_config = list(),
...
)Arguments
- data
A
data.frame,tibble, ortsibblein long format.- x
Unquoted column in
dataused on the x-axis.- y
Unquoted column in
datacontaining numeric values shown on the y-axis.- position
Character value defining the bar position. Common values are
"dodge"and"stack".- facet_var
Optional unquoted column in
dataused for faceting.- facet_scale
Character value defining facet axis scaling. Common values are
"free","fixed","free_x", and"free_y".- facet_nrow
Optional integer. Number of rows in the facet layout.
- facet_ncol
Optional integer. Number of columns in the facet layout.
- color
Optional unquoted column in
dataused to map bar fill color.- flip
Logical value. If
TRUE, the plot is flipped usingggplot2::coord_flip().- reorder
Logical value. If
TRUE, addtidytext::scale_x_reordered()for reordered facet labels.- title
Character value. Plot title.
- subtitle
Character value. Plot subtitle.
- xlab
Character value. Label for the x-axis.
- ylab
Character value. Label for the y-axis.
- caption
Character value. Plot caption.
- bar_size
Numeric value defining the bar border line width.
- bar_color
Character value defining the bar fill color. Ignored when
coloris supplied.- bar_alpha
Numeric value between
0and1defining bar transparency.- theme_set
A complete
ggplot2theme.- theme_config
A named
listwith additional arguments passed toggplot2::theme().- ...
Currently not used.
Details
plot_bar() is a convenience wrapper around ggplot2::geom_bar()
with stat = "identity". It is intended for data that already contains
summarized values, for example accuracy metrics, counts, or grouped summary
statistics.
The arguments x, y, facet_var, and color are
passed as unquoted column names.
If color is supplied, bar fill colors are mapped to that variable and
bar_color is ignored. If color is not supplied, all bars are
drawn using bar_color.
The argument position controls how bars are displayed when
color is supplied. Common values are "dodge" and
"stack".
If flip = TRUE, the x-axis and y-axis are swapped using
ggplot2::coord_flip().
If reorder = TRUE, tidytext::scale_x_reordered() is added.
This is useful when the x-axis has been reordered within facets with
tidytext::reorder_within().
Additional theme settings can be supplied through theme_config. This
should be a named list of arguments passed to ggplot2::theme().
See also
Other data visualization:
plot_density(),
plot_histogram(),
plot_line(),
plot_point(),
plot_qq(),
scale_color_tscv(),
scale_fill_tscv(),
theme_tscv(),
tscv_cols(),
tscv_pal()
Examples
library(dplyr)
context <- list(
series_id = "series",
value_id = "value",
index_id = "index"
)
data <- M4_monthly_data |>
filter(series %in% c("M23100", "M14395"))
stats <- summarise_stats(
.data = data,
context = context
)
plot_bar(
data = stats,
x = series,
y = mean,
title = "Average Value by Series",
xlab = "Series",
ylab = "Mean"
)
acf_data <- estimate_acf(
.data = data,
context = context,
lag_max = 12
)
plot_bar(
data = acf_data,
x = lag,
y = value,
facet_var = series,
title = "Autocorrelation by Series",
subtitle = "Sample autocorrelation up to lag 12",
xlab = "Lag",
ylab = "ACF"
)
