i have a data frame in R with likert scale responses . Some questions are part of a broader theme. The partial data frame :
df
# A tibble: 264 × 3
Theme Question response
<fct> <chr> <fct>
1 Capability Do you believe that the following attributes define XYZ? Dedicated "Neutral"
2 Capability Do you believe that the following attributes define XYZ? Dedicated "Dissatisfied"
3 Capability Do you believe that the following attributes define XYZ? Dedicated "Very \n Dissatis…
4 Capability Do you believe that the following attributes define XYZ? Dedicated "Satisfied"
5 Capability Do you believe that the following attributes define XYZ? Dedicated "Dissatisfied"
6 Capability Do you believe that the following attributes define XYZ? Dedicated "Very \n Satisfie…
7 Capability Do you believe that the following attributes define XYZ? Credible "Neutral"
8 Capability Do you believe that the following attributes define XYZ? Credible "Very \n Dissatis…
9 Capability Do you believe that the following attributes define XYZ? Credible "Neutral"
10 Capability Do you believe that the following attributes define XYZ? Credible "Dissatisfied"
# ℹ 254 more rows
i want to plot it by theme (facet). Doing so :
df = df %>%
dplyr::mutate(row = row_number()) %>%
pivot_wider(names_from = Question, values_from = response) %>%
dplyr::select(-c(row))
df
v1 = gglikert(df, -Theme,
add_totals = TRUE,
facet_rows = vars(Theme),
totals_color = "black",
data_fun = data_fun
) +
labs(y = NULL) +
theme(
text = element_text(size = 10),
strip.text = element_text(color = "black", face = "bold")
) +
theme(strip.text.y = element_text(angle = 0)) +
facet_wrap(
facets = vars(Theme),
labeller = labeller(Theme = label_wrap_gen(width = 10)),
ncol = 1,
scales = "free_y",
strip.position = "top"
) +
scale_y_discrete(
position = "left",
labels = function(x) str_wrap(sub(".*?\\.", "", x), width = 50)
)+
scale_fill_manual(values = custom_colors, guide = guide_legend(nrow = 1))
v1
i receive :
There are some empty rows in each facets which are obviously not the way i want it. Of course some questions are not part of all the themes.
Any help ?
Data
library(tibble)
library(dplyr)
library(tidyr)
library(ggstats)
library(stringr)
set.seed(123) # For reproducibility
# Original DF with 6 participant responses
DF <- tibble(
Theme = c(
"Capability", "Capability", "Capability", "Capability", "Capability", "Capability", "Capability",
"Capability", "Capability", "Capability", "Capability", "Capability", "Capability", "Capability",
"Challenge", "Challenge", "Challenge", "Challenge",
"Contracting Management Topics", "Contracting Management Topics", "Contracting Management Topics",
"Contracting Management Topics", "Contracting Management Topics", "Contracting Management Topics",
"Contracting Management Topics",
"Cooperation Site Supervision", "Cooperation Site Supervision", "Cooperation Site Supervision",
"Cooperation Site Supervision", "Cooperation Site Supervision", "Cooperation Site Supervision",
"Cooperation Site Supervision",
"Core Beliefs", "Core Beliefs",
"Expense",
"Safety", "Safety", "Safety", "Safety", "Safety", "Safety",
"Excellence", "Excellence", "Excellence"
),
Question = c(
"Do you believe that the following attributes define XYZ? Dedicated",
"Do you believe that the following attributes define XYZ? Credible",
"Do you believe that the following attributes define XYZ? Creative",
"Do you believe that the following attributes define XYZ? Sincere",
"Do you believe that the following attributes define XYZ? Ambitious",
"Do you believe that the following attributes define XYZ? Qualified",
"Do you believe that the following attributes define XYZ? Honest",
"Extensive Building knowledge",
"The capacity to rapidly share data on your projects",
"The skill to coordinate your projects",
"Skilled and talented personnel",
"A variety of building services that can be adjusted to fit your demands",
"Do you believe that the following attributes define XYZ? Dedicated",
"Do you believe that the following attributes define XYZ? Credible",
"Please evaluate your contentment regarding the following: Compared to competitors, how pleased are you with XYZ?",
"Kindly respond to the following inquiries: How inclined are you to use our solutions again?",
"Kindly respond to the following inquiries: Would you refer XYZ to associates?",
"Please evaluate your contentment regarding the following: Compared to competitors, how pleased are you with XYZ?",
"How pleased are you with the pace at which XYZ handled your concerns and grievances?",
"The final resolution of your concerns and grievances",
"Please evaluate your contentment regarding the following: Billing",
"Please evaluate your contentment regarding the following: Requests",
"Please evaluate your contentment regarding the following: Adherence to your needs",
"Please evaluate your contentment regarding the following: Prompt reaction to your needs",
"Please evaluate your contentment regarding the following: Compliance with the plan",
"How pleased are you with XYZ executives’ accessibility?",
"How pleased are you with XYZ’s efficiency in field monitoring?",
"How pleased are you with XYZ’s capacity to accomplish tasks as requested?",
"How pleased are you with team expertise and comprehension of building techniques?",
"How pleased are you with issue resolution (Provide corresponding examples)?",
"How pleased are you with cooperation with client teams?",
"How pleased are you with XYZ executives’ accessibility?",
"XYZ is devoted to offering exceptional quality service; how pleased are you with our outputs?",
"How pleased are you with XYZ policies concerning Security, Protection, and Well-being?",
"Please evaluate your contentment regarding the following: The entire expenditure of the undertaking (Original proposal & requests)",
"How pleased are you with XYZ policies concerning Security, Protection, and Well-being?",
"XYZ Security Framework on the Project",
"XYZ’s accountability and dedication to Security on the Project",
"Proficiency of XYZ Project Security Team",
"Clarity, efficiency, and openness of reports linked to incident assessments",
"Dialogue between XYZ project executives and yours concerning Security",
"XYZ’s accountability and dedication to Excellence on the project",
"XYZ’s Excellence Administration Framework execution on the project",
"Productivity, proficiency, and openness concerning remedial steps on quality aspects"
)
) %>%
mutate(
response_1 = sample(1:5, n(), replace = TRUE),
response_2 = sample(1:5, n(), replace = TRUE),
response_3 = sample(1:5, n(), replace = TRUE),
response_4 = sample(1:5, n(), replace = TRUE),
response_5 = sample(1:5, n(), replace = TRUE),
response_6 = sample(1:5, n(), replace = TRUE)
)
# Pivot longer
DF_long <- DF %>%
pivot_longer(
cols = starts_with("response_"),
names_to = "response_id",
values_to = "response"
)%>%
select(-response_id)
# View the result
print(DF_long)
likert_levels = c("Very \n Dissatisfied",
"Dissatisfied",
"Neutral",
"Satisfied",
"Very \n Satisfied")
df = DF_long%>%
dplyr::mutate(across(response, ~ case_when(
. == 1 ~ likert_levels[1],
. == 2 ~ likert_levels[2],
. == 3 ~ likert_levels[3],
. == 4 ~ likert_levels[4],
. == 5 ~ likert_levels[5]
)))%>%
dplyr::mutate(across(response, ~ factor(.x, levels = likert_levels)))%>%
dplyr::mutate(Theme = factor(Theme))
The issue are the missings in the transformed data created by gglikert
(most likely due to the wide format of the input data), i.e. even if there are no answers or responses the questions are not missing in the facet panels and hence will not be dropped using "free_y"
.
One option to fix that would be to drop the missing answers in your custom data_fun
(which you missed to provide in your question and which I therefore added from one of your previous questions on related topics).
Additionaly I would suggest to switch to e.g. ggforce::facet_col
which allows for variable panel widths. Still though, there are a lot of overlapping labels.
library(ggstats)
library(tidyverse)
data_fun <- function(.data) {
.data |>
# Drop the missing `.answer`s
filter(!is.na(.answer)) |>
mutate(
.question = interaction(Theme, .question),
.question = reorder(
.question,
ave(as.numeric(.answer), .question, FUN = \(x) {
sum(x %in% 4:5) / length(x[!is.na(x)])
}),
decreasing = TRUE
)
)
}
v1 <- gglikert(df, -Theme,
add_totals = TRUE,
facet_rows = vars(Theme),
totals_color = "black",
data_fun = data_fun
) +
labs(y = NULL) +
theme(
text = element_text(size = 10),
strip.text = element_text(color = "black", face = "bold")
) +
theme(strip.text.y = element_text(angle = 0)) +
ggforce::facet_col(
facets = vars(Theme),
# labeller = labeller(Theme = label_wrap_gen(width = 10)),
scales = "free_y",
space = "free"
) +
scale_y_discrete(
position = "left",
labels = function(x) str_wrap(sub(".*?\\.", "", x), width = 50)
) +
guides(fill = guide_legend(nrow = 1))
# scale_fill_manual(
# #values = custom_colors,
# guide = guide_legend(nrow = 1)
# )
v1