plot_bars
creates bar charts to visualise the frequency and percentage of responses for a given variable,
optionally grouped by another variable. It supports weighting, custom rounding of percentage labels, and flexible styling options.
Usage
plot_bars(
data,
yVar,
group = NULL,
weight = NULL,
round_decimals = 1,
colours = colour_pal("catExtended"),
title = NULL,
subtitle = NULL,
width = 0.6,
limit = NULL,
base_size = 10,
base_font = ""
)
Arguments
- data
A data frame containing survey data.
- yVar
The variable to be visualized on the y-axis.
- group
(Optional) A variable to group the data by, creating separate plots or facets for each group.
- weight
(Optional) A variable containing weights to apply to each response.
- round_decimals
The number of decimal places to round the percentage labels to. Default is 1.
- colours
A named vector of colours to apply to the bars, corresponding to
yVar
categories.- title
The title for the plot.
- subtitle
The subtitle for the plot.
- width
The width of the bars in the plot. Default is 0.6.
- limit
(Optional) The upper limit for the y-axis. If not specified, it is automatically calculated as the maximum percentage plus 10.
- base_size
Base font size for text elements in the plot. Default is 10.
- base_font
Base font family for text elements in the plot.
Details
This function leverages ggplot2
to create aesthetically pleasing bar charts for visualising survey data. It includes features such as automatic percentage calculation, optional grouping by another variable, weighted responses, and customizable aesthetics including colours, fonts, and title/subtitle text. The function is particularly useful for summarizing categorical data and highlighting differences across groups or categories.
Examples
if (FALSE) { # \dontrun{
data <- survey_data
plot_bars(data,
yVar = "question1",
group = "demographic_group",
weight = "survey_weight",
round_decimals = 2,
colours = colour_pal("catExtended"),
title = "Survey Question 1 Responses",
subtitle = "Grouped by Demographic",
width = 0.5)
} # }