根据最差的likert水平对gglikert进行排序,并显示分组变量的前n个水平

问题描述 投票:0回答:1

我在 R 中有一个名为 df 的数据框,其中有一个分组变量用作 gglikert 中的分面,以及 3 列问题 val1、val2、val3:

# Load necessary libraries
library(tibble)
library(tidyverse)
library(ggplot2)
library(ggpubr)
library(ggstats)

# Define categories and Likert levels
var_levels <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q")

likert_levels <- c(
  "Strongly disagree",
  "Disagree",
  "Neither agree nor disagree",
  "Agree",
  "Strongly agree"
)

# Set seed for reproducibility
set.seed(42)

# Create the dataframe with three Likert response columns
df <- tibble(
  var = sample(var_levels, 50, replace = TRUE),  # Random values from A to Q
  val1 = sample(likert_levels, 50, replace = TRUE),  # Random values from Likert levels
  val2 = sample(likert_levels, 50, replace = TRUE),  # Random values from Likert levels
  val3 = sample(likert_levels, 50, replace = TRUE)   # Random values from Likert levels
)

# View the first few rows of the dataframe
print(df)

我想像here一样对数据集进行gglikert,但这只是针对一个问题val。现在我有3个问题。问题是我想根据强烈不同意和不同意对它们进行排序,并显示最差的 10 个。

如果我尽我所能:

df%>%
  mutate(id=row_number()) %>%
  pivot_longer(-c(id, var), names_to="group") %>%
  pivot_wider(names_from=var) %>%
  gglikert(c(unique(df$var)),
         facet_rows=vars(group))

但这并没有进行我想要的排序,也没有显示最差的 10 个等级的 var 因子。 我怎样才能在 R 中实现这一点?

r dataframe ggplot2
1个回答
0
投票

您可以在下面找到可能的解决方案。我对 gglikert 不太熟悉,所以我决定制作三个 gglikert 图并将它们与

patchwork
合并,而不是摆弄它的分面选项。 我使用了您之前获得的解决方案中的一些代码进行排序。

require(pacman)
pacman::p_load(char = c(
  "tibble",
  "tidyverse",
  "ggplot2",
  "ggstats","ggpubr","patchwork"
))

# Define categories and Likert levels
var_levels <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q")

likert_levels <- c(
  "Strongly disagree",
  "Disagree",
  "Neither agree nor disagree",
  "Agree",
  "Strongly agree"
)

# Set seed for reproducibility
set.seed(42)

# Create the dataframe with three Likert response columns
df <- tibble(
  var = sample(var_levels, 50, replace = TRUE),  # Random values from A to Q
  val1 = sample(likert_levels, 50, replace = TRUE),  # Random values from Likert levels
  val2 = sample(likert_levels, 50, replace = TRUE),  # Random values from Likert levels
  val3 = sample(likert_levels, 50, replace = TRUE)   # Random values from Likert levels
)

# View the first few rows of the dataframe
print(df)

## this is the transformation you already prepared
sorted_clean <- df |> 
  mutate(id = row_number())%>%
  mutate(
    val1 = factor(val1, likert_levels),
    val2 = factor(val2, likert_levels),
    val3 = factor(val3, likert_levels)
  ) |>
  pivot_longer(-c(id, var), names_to = "group") 

## now we count by responded value and count the disagree values
keep_combinations <- sorted_clean |> 
  count(var, group, value) |> 
  group_by(var, group) |> 
  summarise(disagree_count = sum(n[value %in% c("Strongly disagree", "Disagree")])) |> 
  group_by(group) |> 
  ## now we only keep the ones with the max count of disagree count
  slice_max(order_by = disagree_count, n = 10)

## here we filter the original dataset to these top categories filtered in keep_combinations
grouped_df <- sorted_clean |> 
  left_join(keep_combinations) |> 
  filter(!is.na(disagree_count)) %>% 
  pivot_wider(names_from=var) |> 
  ## now we gorup split by group to make three groups to avoid facetting throigh gglikert for more flexibility in the sorting
  group_split(group)

grouped_levels <- keep_combinations |> 
  group_split(group)

## plot 
map2(grouped_df, grouped_levels, ~gglikert(.x,.y$var, sort = "ascending")+
       aes(y = reorder(
         factor(.question, levels = unique(.y$var)),
         ave(
           as.numeric(.answer), .question,
           FUN = \(x) {
             sum(x %in% 1:2) / length(x[!is.na(x)])
           }
         )
       ))+
       labs(title = unique(.y$group))) |> 
  ## make a patchwork plot out of the 
  reduce(`/`)+ 
  plot_layout(guides = "collect")& 
  theme(legend.position = "bottom")


© www.soinside.com 2019 - 2024. All rights reserved.