You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中调整李克特量表数据绘图的选项顺序?

解决李克特图选项顺序问题

问题背景

用R绘制调查数据的李克特图时,选项默认按字母顺序排列,希望改成指定顺序:"Strongly Disagree"、"Disagree"、"Neither Disagree nor Agree"、"Agree"、"Strongly Agree"。尝试用levels = c(...)重新设置数据框列的因子顺序后,数据框变成单列且无数据,原绘图代码如下:

attitudes <- sub_survey %>% 
  select(i_sub_no_perm_job,                  
         i_sub_flexibility,                  
         i_sub_help_local_school,            
         amnt_sub_wrk_incrsing,              
         i_wrk_max_avlbl_hrs,                
         sub_wrk_not_attractive_low_pay) %>% 
  rename("I provide substitution work because I can't find a permanent job" = i_sub_no_perm_job,                
         "I provide substitution work because I enjoy the flexibility" = i_sub_flexibility,             
         "I provide substitution work because I want to help out my local school" = i_sub_help_local_school,                    
         "I believe the amount of substitution work available is increasing in my area" = amnt_sub_wrk_incrsing,                    
         "I work the maximum number of substitution hours I am available for" = i_wrk_max_avlbl_hrs,                    
         "Substitution work is not an attractive option for me as the pay/conditions are poor" = sub_wrk_not_attractive_low_pay) %>% 
  na.omit()

attitudes[] <- lapply(attitudes, factor)

plot(likert(attitudes),legend.position = "right")

前4行数据的dput输出:

structure(list(`I provide substitution work because I can't find a 
permanent job` = structure(c(1L, 
4L, 3L, 4L), levels = c("Agree", "Disagree", "Neither Disagree nor 
Agree", 
"Strongly Agree", "Strongly Disagree"), class = "factor"), `I provide 
substitution work because I enjoy the flexibility` = structure(c(1L, 
2L, 5L, 1L), levels = c("Agree", "Disagree", "Neither Disagree nor 
Agree", 
"Strongly Agree", "Strongly Disagree"), class = "factor"), `I provide 
substitution work because I want to help out my local school` = 
structure(c(3L, 
1L, 1L, 2L), levels = c("Agree", "Disagree", "Neither Disagree nor 
Agree", 
"Strongly Agree", "Strongly Disagree"), class = "factor"), `I believe 
the amount of substitution work available is increasing in my area` = 
structure(c(3L, 
2L, 4L, 1L), levels = c("Agree", "Disagree", "Neither Disagree nor 
Agree", 
"Strongly Agree", "Strongly Disagree"), class = "factor"), `I work the 
maximum number of substitution hours I am available for` = 
structure(c(3L, 
3L, 1L, 1L), levels = c("Agree", "Disagree", "Neither Disagree nor 
Agree", 
"Strongly Agree", "Strongly Disagree"), class = "factor"), 
`Substitution 
work is not an attractive option for me as the pay/conditions are poor` 
= structure(c(1L, 
3L, 2L, 1L), levels = c("Agree", "Disagree", "Neither Disagree nor 
Agree", 
"Strongly Agree", "Strongly Disagree"), class = "factor")), row.names = 
c(NA, 
-4L), na.action = structure(293:298, names = c("293", "294", 
"295", "296", "297", "298"), class = "omit"), class = c("tbl_df", 
"tbl", "data.frame"))

错误原因

你之前修改因子层级时操作不当,大概率是lapply用法有误,没有明确传入目标levels参数,导致数据框结构被破坏。

解决方案

正确的做法是在将列转换为因子时,直接指定目标顺序作为levels参数值,确保每个列的因子层级都按要求设置。以下两种方法都能解决问题:

方法1:用dplyr::across批量处理

# 定义统一的李克特选项顺序
likert_order <- c("Strongly Disagree", "Disagree", "Neither Disagree nor Agree", "Agree", "Strongly Agree")

attitudes <- sub_survey %>% 
  select(i_sub_no_perm_job,                  
         i_sub_flexibility,                  
         i_sub_help_local_school,            
         amnt_sub_wrk_incrsing,              
         i_wrk_max_avlbl_hrs,                
         sub_wrk_not_attractive_low_pay) %>% 
  rename("I provide substitution work because I can't find a permanent job" = i_sub_no_perm_job,                
         "I provide substitution work because I enjoy the flexibility" = i_sub_flexibility,             
         "I provide substitution work because I want to help out my local school" = i_sub_help_local_school,                    
         "I believe the amount of substitution work available is increasing in my area" = amnt_sub_wrk_incrsing,                    
         "I work the maximum number of substitution hours I am available for" = i_wrk_max_avlbl_hrs,                    
         "Substitution work is not an attractive option for me as the pay/conditions are poor" = sub_wrk_not_attractive_low_pay) %>% 
  na.omit() %>%
  # 批量将所有列转换为指定顺序的有序因子
  mutate(across(everything(), ~factor(., levels = likert_order, ordered = TRUE)))

# 绘制李克特图
plot(likert(attitudes), legend.position = "right")

方法2:修正lapply用法

如果你更习惯用lapply,可以这样修改:

likert_order <- c("Strongly Disagree", "Disagree", "Neither Disagree nor Agree", "Agree", "Strongly Agree")

attitudes <- sub_survey %>% 
  select(i_sub_no_perm_job,                  
         i_sub_flexibility,                  
         i_sub_help_local_school,            
         amnt_sub_wrk_incrsing,              
         i_wrk_max_avlbl_hrs,                
         sub_wrk_not_attractive_low_pay) %>% 
  rename("I provide substitution work because I can't find a permanent job" = i_sub_no_perm_job,                
         "I provide substitution work because I enjoy the flexibility" = i_sub_flexibility,             
         "I provide substitution work because I want to help out my local school" = i_sub_help_local_school,                    
         "I believe the amount of substitution work available is increasing in my area" = amnt_sub_wrk_incrsing,                    
         "I work the maximum number of substitution hours I am available for" = i_wrk_max_avlbl_hrs,                    
         "Substitution work is not an attractive option for me as the pay/conditions are poor" = sub_wrk_not_attractive_low_pay) %>% 
  na.omit()

# 用lapply给每个列设置指定的因子顺序
attitudes[] <- lapply(attitudes, function(x) factor(x, levels = likert_order, ordered = TRUE))

plot(likert(attitudes), legend.position = "right")

说明

  • 统一定义likert_order变量,避免重复输入,也方便后续调整顺序。
  • 添加ordered = TRUE明确这是有序因子,符合李克特量表的逻辑,也能确保绘图时顺序完全匹配预期。
  • 两种方法都能保留数据框的原有结构,不会出现单列空数据的问题。

内容的提问来源于stack exchange,提问作者Shout

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 16:34:55