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

如何为R语言sjPlot包的plot_likert函数设置李克特图自定义配色

R语言sjPlot包plot_likert函数自定义配色实现方案

你可以直接通过plot_likert()的colors参数传入和李克特选项顺序对应的自定义配色向量实现需求,以下是可直接运行的完整代码:

library(tidyverse)
library(sjPlot)

# 构造示例数据集
df1 <-
  data.frame(
  matrix(
    data = sample(x = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"), size = 500, replace = TRUE),
    ncol = 5
    )
  ) %>% 
  mutate_all(., ~ ordered(., levels = c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")))

# 绘制自定义配色的李克特量表图
plot_likert(
   items       = df1,
   cat.neutral = 3,
   # 按因子水平顺序传入自定义配色,顺序必须和你定义的选项顺序完全一致
   colors = c(
     "Strongly Disagree" = "#d73027", # 深红色
     "Disagree" = "#fc8d59", # 浅红色
     "Neutral" = "#999999", # 灰色
     "Agree" = "#91cf60", # 浅绿色
     "Strongly Agree" = "#1a9850" # 深绿色
   )
)

参数说明

  • 传入colors参数的向量顺序必须和你给因子定义的水平顺序完全对应,否则会出现配色错位的问题
  • 你也可以替换为R内置的颜色名使用,比如"darkred"、"lightcoral"、"gray"、"lightgreen"、"darkgreen",效果和十六进制色值一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 10:57:02