Creates bar plots with Insper's visual identity. Supports grouped, stacked, and filled bars, value labels, and horizontal or vertical orientation.
Usage
insper_barplot(
data,
x,
y,
fill = NULL,
position = "dodge",
palette = "main",
zero = TRUE,
text = FALSE,
text_size = 4,
text_color = "black",
stack_vjust = 0.5,
label_formatter = scales::comma,
...
)Arguments
- data
A data frame.
- x
<
data-masked> Variable for the x-axis.- y
<
data-masked> Variable for the y-axis.- fill
Fill aesthetic. Accepts one of the options below.
A bare column name, mapped to fill (e.g.,
fill = gear).A quoted color string, used as a static fill (e.g.,
fill = "blue").NULL(default), which uses Insper red.
A mapped variable produces grouped, stacked, or filled bars, depending on
position.- position
Character. Position adjustment for bars:
"dodge"(default),"stack","fill", or"identity".- palette
Character. Palette for mapped variables. Default is
"main".- zero
Logical. If
TRUE(default), draws a reference line at zero.- text
Logical. If
TRUE, adds value labels to the bars. Default isFALSE.- text_size
Numeric. Size of the value labels. Default is 4.
- text_color
Character. Color of the value labels. Default is
"black". On stacked and filled bars, the default picks white or dark text for each segment based on its fill; any other value is used as is.- stack_vjust
Numeric. Vertical position of labels within stacked and filled segments, from 0 (bottom) to 1 (top). Default is 0.5 (center).
- label_formatter
Function used to format value labels. Default is
scales::comma(). It must accept anaccuracyargument. Filled bars ignore it and always show percentages.- ...
Additional arguments passed to
ggplot2::geom_col(), such aswidthoralpha.
Details
Orientation follows the variable types. A categorical x with a numeric
y gives vertical bars; a numeric x with a categorical y gives
horizontal bars. Value labels and the zero line follow the orientation.
Examples
if (FALSE) { # has_insper_fonts()
# Simple bar plot with default color
insper_barplot(mtcars, x = factor(cyl), y = mpg)
# With text labels showing values
insper_barplot(mtcars, x = factor(cyl), y = mpg, text = TRUE)
# Stacked bars with centered text labels
data <- data.frame(
category = rep(c("A", "B", "C"), each = 2),
group = rep(c("X", "Y"), 3),
value = c(10, 15, 20, 25, 30, 35)
)
insper_barplot(data, x = category, y = value, fill = group,
position = "stack", text = TRUE)
# Filled bars with automatic percentage formatting
insper_barplot(data, x = category, y = value, fill = group,
position = "fill", text = TRUE)
}
