如何为负面表述的Likert问题翻转配色方案?
Likert量表正负向问题配色翻转实现方案
一、HH包实现方案:拆分绘图拼接
HH包的likert()函数默认对所有问题使用统一配色,无法直接给单个问题单独设置配色顺序。要实现需求,最直接的方式是拆分数据分别绘图,再拼接在一起:
- 加载依赖包
library(HH) library(gridExtra)
- 拆分并处理数据
把原数据拆分为正向问题、负向问题两个子集,同时反转负向问题的Likert选项列顺序(让积极选项对应原配色的粉色):
# 拆分正向/负向问题数据 pos_data <- section_1[section_1$question == "I know when I have understood a new concept or idea", ] neg_data <- section_1[section_1$question == "After I finish a test, I can't tell whether I have done well or not until I get the results", ] # 反转负向问题的选项列顺序(从"Not true at all"到"Very true") neg_data <- neg_data[, c("question", "pre_post", "Not true at all", "Not really true", "A little bit true", "Mostly true", "Very true")]
- 分别绘制图表
给两个子集用相同的配色,负向问题因列顺序反转,粉色会自动对应积极方向("Not true at all"):
# 正向问题图表 fig_pos <- likert(pre_post~.| question, data = pos_data, as.percent = TRUE, ReferenceZero = 3, ylab = "", xlim = c(-100, 100), main = "", col = c("#E6007E","#F380BF","#DDDFE4","#80CFF1","#009FE3"), strip = strip.custom(factor.levels = "I know when I have understood \na new concept or idea"), par.strip.text = list(cex=0.6, lines=5)) # 负向问题图表 fig_neg <- likert(pre_post~.| question, data = neg_data, as.percent = TRUE, ReferenceZero = 3, ylab = "", xlim = c(-100, 100), main = list("Knowledge of Cognition", x=unit(.55, "npc")), col = c("#E6007E","#F380BF","#DDDFE4","#80CFF1","#009FE3"), strip = strip.custom(factor.levels = "After I finish a test, I \ncan't tell whether I have \ndone well or not until I get \nthe results"), par.strip.text = list(cex=0.6, lines=5))
- 拼接两张图表
grid.arrange(fig_pos, fig_neg, ncol=1)
二、ggplot2实现方案(更灵活)
ggplot2的自定义能力更强,适合实现这类个性化需求,步骤如下:
- 加载依赖包
library(tidyverse)
- 整理数据格式
把宽格式数据转成长格式,标记问题类型,并对负向问题反转选项的因子顺序:
section_1_long <- section_1 %>% # 转成长格式 pivot_longer(cols = c("Very true", "Mostly true", "A little bit true", "Not really true", "Not true at all"), names_to = "response", values_to = "count") %>% # 计算百分比(按问题和前后测分组) group_by(question, pre_post) %>% mutate(percent = count / sum(count) * 100) %>% ungroup() %>% # 标记问题是正向/负向 mutate(question_type = case_when( question == "I know when I have understood a new concept or idea" ~ "positive", question == "After I finish a test, I can't tell whether I have done well or not until I get the results" ~ "negative" )) %>% # 对负向问题反转选项的因子顺序,让积极选项对应粉色 mutate(response = ifelse(question_type == "negative", factor(response, levels = rev(c("Very true", "Mostly true", "A little bit true", "Not really true", "Not true at all"))), factor(response, levels = c("Very true", "Mostly true", "A little bit true", "Not really true", "Not true at all"))))
- 绘制自定义Likert图表
ggplot(section_1_long, aes(x = pre_post, y = percent, fill = response)) + geom_col(position = "fill", width = 0.7) + coord_flip() + # 翻转坐标轴,让问题显示在y轴 facet_wrap(~question, ncol = 1, scales = "free_y") + # 按问题分面 scale_fill_manual(values = c("#E6007E","#F380BF","#DDDFE4","#80CFF1","#009FE3")) + # 统一配色 labs(title = "Knowledge of Cognition", y = "Percentage", x = "Pre/Post", fill = "Response") + theme_minimal() + theme(strip.text = element_text(size = 8, lineheight = 1.2), plot.title = element_text(hjust = 0.5))
这样处理后,粉色(#E6007E)在正向问题对应Very true,在负向问题对应Not true at all,统一代表积极方向,符合视觉直观性需求。
内容的提问来源于stack exchange,提问作者Sophie Elizabeth
相关产品推荐
相关产品推荐

