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

使用plot_likert绘制Likert量表数据时遇赋值错误,请求协助

问题分析与解决

错误原因汇总

  1. 变量名拼写错误:调用plot_likert时误写为at,实际数据框名称是att。
  2. 值标签赋值逻辑错误:循环遍历的是数据框的列值而非列名,且错误地给整个数据框统一赋值标签,而非为每一列单独设置。
  3. 标签与数据取值不匹配:原数据取值范围是1-4,但标签包含了未出现的0,会导致后续统计异常。
  4. 未加载依赖包:plot_likert属于sjPlot包,val_lab和make_labels属于sjmisc包,需提前加载这两个包。

修正后的代码

# 加载所需依赖包
library(sjPlot)
library(sjmisc)

# 构建数据集
att <- structure(list(att1_goodofall = c(3L, 3L, 1L, 3L, 3L, 3L, 3L, 
2L, 3L, 3L), att2_pvtdisease = c(3L, 3L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L), att3_curedisease = c(3L, 1L, 3L, 2L, 2L, 1L, 2L, 
3L, 3L, 3L), att4_timewaste = c(4L, 4L, 2L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L), att5_helpgenerations = c(3L, 3L, 3L, 3L, 2L, 3L, 3L, 
2L, 3L, 3L)), row.names = c(NA, 10L), class = "data.frame")

# 为每一列单独赋值值标签
for(col in names(att)) {                              
    val_lab(att[[col]]) <- make_labels("1 Disagree
                                        2 Neither agree nor disagree
                                        3 Agree
                                        4 Strongly agree")
}

# 绘制Likert量表图
plot_likert(att)

关键修正点说明

  • 循环逻辑调整:通过names(att)遍历列名,用att[[col]]定位具体列,实现为每一列单独设置标签。
  • 标签匹配数据:移除未在数据中出现的0标签,确保标签与实际取值完全对应。
  • 修正变量名:将plot_likert(at)改为plot_likert(att),匹配数据框名称。
  • 明确加载包:提前加载依赖包,保证所有函数可正常调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 02:41:25