首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对图中的变量进行重新排序

对图中的变量进行重新排序
EN

Stack Overflow用户
提问于 2021-06-25 00:48:01
回答 1查看 16关注 0票数 1

有没有人知道一种简单的方法来将图片中的变量重新排序为我想要的顺序?我想要Q1作为最高标杆,Q4作为最低标杆。

代码语言:javascript
复制
  select(c("Unit_number",
           contains('Q'))) %>%
  filter(Unit_number == 6) %>%
  pivot_longer(names_to = "question",
               values_to = "answer",
               cols = -Unit_number) %>%
  group_by(question) %>%
  summarise( 
    n=n(),
    mean=mean(answer),
    sd=sd(answer)
  )


ggplot(Mydata_tidy, aes(x=question, y=mean)) +
  geom_bar(stat ="identity") +
  geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd)) +
  coord_flip()```

Here's the data frame ↓
structure(list(question = c("Q1", "Q2", "Q3", "Q4"), n = c(8L, 
8L, 8L, 8L), mean = c(5.5, 4.375, 4.75, 5.25), sd = c(1.0690449676497, 
1.30247018062932, 1.16496474502144, 1.03509833901353)), row.names = c(NA, 
4L), class = c("tbl_df", "tbl", "data.frame"))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-25 00:54:29

我们可以将列转换为factor,并将levels按该自定义顺序排列

代码语言:javascript
复制
library(dplyr)
library(ggplot2)
Mydata_tidy %>% 
   mutate(question = factor(question, levels = paste0("Q", 4:1))) %>% 
   ggplot(aes(x=question, y=mean)) + geom_bar(stat ="identity") +
   geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd)) +
   coord_flip()

-output

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68119791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档