I came along the gglikert() function in R from the ggsstats library here. They used sampling data and create a data frame called df. I am using the same as given in the link.(the df not the df_dk). Ok, if i run in R the following code :
p = ggstats::gglikert(df, sort = "descending",
sort_method = c("prop"))
p
i receive a plot :
which is sorted in an ascending order and the "prop" sort according to the proportion of answers higher than the centered level. I want the plot to be sorted according to very left proportions that are the combination (sum) of the two lower levels. (i.e Respectively the percentages on the very right in the sum of the two lower categories. )
how can i achieve this in R ?
Here is one possible option which uses reorder
and an ifelse
to reorder the variable mapped on y
using the counts (aka the sum
) of Strictly disagree
and disagree
answers. Under the hood ggstats::gglikert
reshape the data to long where the question id's are stored in a column named .question
and the answers in a column named .answer
:
library(ggplot2)
library(ggstats)
ggstats::gglikert(df) +
aes(y = reorder(.question,
ifelse(
.answer %in% c("Strongly disagree", "Disagree"),
1, 0
),
FUN = sum
))
DATA
likert_levels <- c(
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
)
set.seed(42)
library(dplyr, warn = FALSE)
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)))