matrix_freq {surveyexplorer} | R Documentation |
Matrix Frequency Plot
Description
Generate a grouped bar chart displaying the frequency distribution of
responses for a categorical variable. The function supports optional
subgrouping of data using the group_by
variable, exclusion of specific
subgroups with 'subgroups_to_exclude,' and data weighting with the 'weights'
parameter. Users can also choose to exclude NA values from the questions
prior to analysis using the 'na.rm' parameter.
Usage
matrix_freq(
dataset,
question,
response_order = NULL,
group_by = NULL,
subgroups_to_exclude = NULL,
weights = NULL,
na.rm = FALSE,
colors = NULL
)
Arguments
dataset |
The input dataframe (or tibble) of survey questions |
question |
The columns that contain each of the response options for a question, can be selected by using tidyselect semanatics or providing a vector of column names or numbers |
response_order |
An optional vector specifying the order of factor levels for the response categories. This parameter is particularly useful for ensuring that the response categories are presented in a specific, meaningful order when plotting. For instance, in surveys or questionnaires where responses range from strongly disagree to strongly agree, setting response_order allows the categories to be displayed in this logical sequence rather than an alphabetical or random order. |
group_by |
Optional variable to group the analysis. If provided, the frequencies and counts will be calculated within each subgroup. |
subgroups_to_exclude |
Optional vector specifying subgroups to exclude from the analysis. |
weights |
Optional variable containing survey weights. If provided, frequencies and counts will be weighted accordingly. |
na.rm |
Logical indicating whether to remove NA values from |
colors |
Optional vector specifying colors for each response category. |
Value
A ggplot2 object representing a grouped bar chart displaying the frequency distribution of responses for the specified categorical variable. The chart supports grouping, weighting, and exclusion of subgroups.
See Also
Other matrix questions:
matrix_likert()
,
matrix_mean()
,
matrix_table()
Examples
#Array question (1-5)
matrix_freq(berlinbears, dplyr::starts_with('p_'))
#remove NA category
matrix_freq(berlinbears, dplyr::starts_with('p_'), na.rm = TRUE)
#Use `group_by` to partition the question into several groups
matrix_freq(berlinbears, dplyr::starts_with('p_'), group_by = species,
subgroups_to_exclude = c('panda bear', NA ), na.rm = TRUE)
#Categorical input
matrix_freq(berlinbears, dplyr::starts_with('c_'), group_by = is_parent, na.rm = TRUE)