gglikert {ggstats} | R Documentation |
Plotting Likert-type items
Description
Usage
gglikert(
data,
include = dplyr::everything(),
weights = NULL,
y = ".question",
variable_labels = NULL,
sort = c("none", "ascending", "descending"),
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = totals_include_center,
factor_to_sort = ".question",
exclude_fill_values = NULL,
data_fun = NULL,
add_labels = TRUE,
labels_size = 3.5,
labels_color = "auto",
labels_accuracy = 1,
labels_hide_below = 0.05,
add_totals = TRUE,
totals_size = labels_size,
totals_color = "black",
totals_accuracy = labels_accuracy,
totals_fontface = "bold",
totals_include_center = FALSE,
totals_hjust = 0.1,
y_reverse = TRUE,
y_label_wrap = 50,
reverse_likert = FALSE,
width = 0.9,
facet_rows = NULL,
facet_cols = NULL,
facet_label_wrap = 50
)
gglikert_data(
data,
include = dplyr::everything(),
weights = NULL,
variable_labels = NULL,
sort = c("none", "ascending", "descending"),
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = TRUE,
factor_to_sort = ".question",
exclude_fill_values = NULL,
data_fun = NULL
)
gglikert_stacked(
data,
include = dplyr::everything(),
weights = NULL,
y = ".question",
variable_labels = NULL,
sort = c("none", "ascending", "descending"),
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = FALSE,
factor_to_sort = ".question",
data_fun = NULL,
add_labels = TRUE,
labels_size = 3.5,
labels_color = "auto",
labels_accuracy = 1,
labels_hide_below = 0.05,
add_median_line = FALSE,
y_reverse = TRUE,
y_label_wrap = 50,
reverse_fill = TRUE,
width = 0.9
)
Arguments
data |
a data frame |
include |
variables to include, accepts tidy-select syntax |
weights |
optional variable name of a weighting variable, accepts tidy-select syntax |
y |
name of the variable to be plotted on |
variable_labels |
a named list or a named vector of custom variable labels |
sort |
should the factor defined by |
sort_method |
method used to sort the variables: |
sort_prop_include_center |
when sorting with |
factor_to_sort |
name of the factor column to sort if |
exclude_fill_values |
Vector of values that should not be displayed
(but still taken into account for computing proportions),
see |
data_fun |
for advanced usage, custom function to be applied to the
generated dataset at the end of |
add_labels |
should percentage labels be added to the plot? |
labels_size |
size of the percentage labels |
labels_color |
color of the percentage labels ( |
labels_accuracy |
accuracy of the percentages, see
|
labels_hide_below |
if provided, values below will be masked, see
|
add_totals |
should the total proportions of negative and positive answers be added to plot? This option is not compatible with facets! |
totals_size |
size of the total proportions |
totals_color |
color of the total proportions |
totals_accuracy |
accuracy of the total proportions, see
|
totals_fontface |
font face of the total proportions |
totals_include_center |
if the number of levels is uneven, should half of the center level be added to the total proportions? |
totals_hjust |
horizontal adjustment of totals labels on the x axis |
y_reverse |
should the y axis be reversed? |
y_label_wrap |
number of characters per line for y axis labels, see
|
reverse_likert |
if |
width |
bar width, see |
facet_rows , facet_cols |
A set of variables or expressions quoted by
|
facet_label_wrap |
number of characters per line for facet labels, see
|
add_median_line |
add a vertical line at 50%? |
reverse_fill |
if |
Details
Combines several factor variables using the same list of ordered levels (e.g. Likert-type scales) into a unique data frame and generates a centered bar plot.
You could use gglikert_data()
to just produce the dataset to be plotted.
If variable labels have been defined (see labelled::var_label()
), they will
be considered. You can also pass custom variables labels with the
variable_labels
argument.
Value
A ggplot2
plot or a tibble
.
See Also
vignette("gglikert")
, position_likert()
, stat_prop()
Examples
library(ggplot2)
library(dplyr)
likert_levels <- c(
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
)
set.seed(42)
df <-
tibble(
q1 = sample(likert_levels, 150, replace = TRUE),
q2 = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
q3 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q4 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
q6 = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
) %>%
mutate(across(everything(), ~ factor(.x, levels = likert_levels)))
gglikert(df)
gglikert(df, include = q1:3)
gglikert(df, sort = "ascending")
gglikert(df, sort = "ascending", sort_prop_include_center = TRUE)
gglikert(df, sort = "ascending", sort_method = "mean")
gglikert(df, reverse_likert = TRUE)
gglikert(df, add_totals = FALSE, add_labels = FALSE)
gglikert(
df,
totals_include_center = TRUE,
totals_hjust = .25,
totals_size = 4.5,
totals_fontface = "italic",
totals_accuracy = .01,
labels_accuracy = 1,
labels_size = 2.5,
labels_hide_below = .25
)
gglikert(df, exclude_fill_values = "Neither agree nor disagree")
if (require("labelled")) {
df %>%
set_variable_labels(
q1 = "First question",
q2 = "Second question"
) %>%
gglikert(
variable_labels = c(
q4 = "a custom label",
q6 = "a very very very very very very very very very very long label"
),
y_label_wrap = 25
)
}
# Facets
df_group <- df
df_group$group <- sample(c("A", "B"), 150, replace = TRUE)
gglikert(df_group, q1:q6, facet_rows = vars(group))
gglikert(df_group, q1:q6, facet_cols = vars(group))
gglikert(df_group, q1:q6, y = "group", facet_rows = vars(.question))
# Custom function to be applied on data
f <- function(d) {
d$.question <- forcats::fct_relevel(d$.question, "q5", "q2")
d
}
gglikert(df, include = q1:q6, data_fun = f)
gglikert_stacked(df, q1:q6)
gglikert_stacked(df, q1:q6, add_median_line = TRUE, sort = "asc")
gglikert_stacked(df_group, q1:q6, y = "group", add_median_line = TRUE) +
facet_grid(rows = vars(.question))