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

如何调整ggplot输出的分类顺序,取消默认按字母排序规则?

解决方案

修改Excel数据顺序不生效的原因是,ggplot默认将字符型分类变量按照字母顺序排序展示,你只需要手动将分类列转换为指定顺序的因子类型即可,修改后的完整代码如下:

data_setReading20 <- read_excel("~/Desktop/Survey_Data1_ENGSPA(FIXED).xlsx")

my_data20 <- data_setReading20[, c("par_res20", "par_count20")]
# 新增下行指定分类展示顺序
my_data20$par_res20 <- factor(my_data20$par_res20, levels = c("Agree", "Somewhat Agree", "Somewhat Disagree", "Disagree"))

ggplot(data = my_data20, aes(x = par_res20, y = par_count20, fill = par_res20)) +
  geom_bar(stat = "identity") + 
  geom_text(mapping = aes(label=par_count20), position = position_dodge(width = 0.9), cex = 2.5, vjust=-1) +    
  labs(fill = "Responses",
       x = "Youth Responses",
       y = "Youth Count",
       subtitle = "I want to come OR return to _ in person.",
       caption = "N = 312, youth responses to survey in English and Spanish") +
  scale_fill_manual(values = c("Agree" = "#FCD81F",
                               "Somewhat Agree" = "#F88B3B", 
                               "Somewhat Disagree" = "#F8543B",
                               "Disagree" = "#F8AA3B")) + 
  theme_bw()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 03:48:03