如何调整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
相关产品推荐
相关产品推荐

