R语言ggplot绘图非数值参数报错、y美学缺失及因子重命名问题求助
R ggplot2 条形图问题解决方案
问题1:绘图代码报错修复
错误原因:
- 语法括号不匹配:
geom_bar()函数的参数写完后缺少右括号,导致后续的labs()被错误识别为geom_bar()的入参,触发「非数值参数传入二元运算符」报错 - y轴映射写法兼容性问题:旧版
..count..写法在部分ggplot2版本中会被误判为缺失映射,建议替换为官方推荐的after_stat()规范写法
修复后代码:
ggplot(religion) + geom_bar(aes(x = comfort_practice_religion_umprompted, fill = comfort_practice_religion_umprompted, y = after_stat(count / sum(count)))) + labs(x = "舒适度", y = "占比", title = "被主动询问宗教信仰时的舒适程度") + theme_classic() + theme(axis.ticks.y = element_blank(), axis.ticks.x = element_blank())
问题2:「对象未找到」报错修复
错误原因:
- 未提前加载原始数据到R环境:你直接调用
rename()操作不存在的religion对象,导致第一步生成religion2失败 - 代码执行顺序错误:后续所有依赖
religion2的代码都会因为前置步骤失败,报对象不存在错误
修复步骤:先将你提供的样本向量转为数据框,再执行后续处理:
# 加载样本数据,构造religion数据框 raw_vec <- c("Extremely comfortable", "Very comfortable", "Extremely comfortable", "Very comfortable", "Not so comfortable", "Somewhat comfortably", "Extremely comfortable", "Somewhat comfortably", "Not so comfortable", "Very comfortable", "Somewhat comfortably", "Not at all comfortable", "Extremely comfortable", "Not at all comfortable", "Very comfortable", "Extremely comfortable", "Very comfortable", "Extremely comfortable", "Very comfortable", "Very comfortable", "Somewhat comfortably", "Extremely comfortable", "Not so comfortable", "Extremely comfortable", "Somewhat comfortably", "Somewhat comfortably", "Somewhat comfortably", "Not so comfortable", "Very comfortable", "Not at all comfortable", "Very comfortable", "Not so comfortable", "Very comfortable", "Extremely comfortable", "Not so comfortable", "Somewhat comfortably", "Somewhat comfortably", "Not so comfortable", "Very comfortable", "Not so comfortable", "Very comfortable", "Extremely comfortable", "Very comfortable", "Extremely comfortable", "Extremely comfortable", "Not at all comfortable", "Not at all comfortable", "Somewhat comfortably", "Not so comfortable", "Somewhat comfortably", "Extremely comfortable", "Not at all comfortable", "Somewhat comfortably", "Very comfortable", "Extremely comfortable", "Somewhat comfortably", "Somewhat comfortably", "Somewhat comfortably", "Very comfortable", "Extremely comfortable", "Extremely comfortable", "Extremely comfortable", "Not so comfortable", "Very comfortable", "Very comfortable", "Very comfortable", "Not so comfortable", "Very comfortable", "Not so comfortable", "Not at all comfortable", "Somewhat comfortably", "Not at all comfortable", "Extremely comfortable", "Not so comfortable", "Not so comfortable", "Extremely comfortable", "Somewhat comfortably", "Extremely comfortable", "Very comfortable", "Somewhat comfortably", "Not at all comfortable", "Somewhat comfortably", "Not at all comfortable", "", "Not at all comfortable", "Extremely comfortable", "Somewhat comfortably", "Very comfortable", "Not so comfortable", "Somewhat comfortably", "Very comfortable", "Somewhat comfortably", "Extremely comfortable", "Extremely comfortable", "Somewhat comfortably", "Extremely comfortable", "Not so comfortable", "Very comfortable", "Somewhat comfortably", "Not so comfortable", "Very comfortable", "Not so comfortable", "Extremely comfortable", "Not so comfortable", "Extremely comfortable", "Very comfortable", "Extremely comfortable") religion <- data.frame( How.comfortable.would.you.be.seeing.someone.who.practices.a.different.religion.from.you..Ask.you.about.your.religion..unprompted..in.conversation = raw_vec ) # 加载依赖包后执行重命名即可 library(ggplot2) library(dplyr) library(forcats) religion2 <- rename(religion, comfort_practice_religion_umprompted = How.comfortable.would.you.be.seeing.someone.who.practices.a.different.religion.from.you..Ask.you.about.your.religion..unprompted..in.conversation)
问题3:空值无法重命名为「No response」修复
错误原因:操作顺序错误,你在两次mutate中都操作原始的religion2,第一次重编码的结果没有被保存,第二次重排顺序时还是用的带空值的原始数据,导致重命名不生效。
修复代码:
religion <- religion2 %>% # 先将空字符串重编码为No response mutate(comfort_practice_religion_umprompted = fct_recode(comfort_practice_religion_umprompted, "No response" = "")) %>% # 再调整因子排序 mutate(comfort_practice_religion_umprompted = fct_relevel(comfort_practice_religion_umprompted, "Extremely comfortable","Very comfortable", "Somewhat comfortably", "Not so comfortable", "Not at all comfortable", "No response"))
内容的提问来源于stack exchange,提问作者cbear23
相关产品推荐
相关产品推荐

