
有没有人知道一种简单的方法来将图片中的变量重新排序为我想要的顺序?我想要Q1作为最高标杆,Q4作为最低标杆。
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"))发布于 2021-06-25 00:54:29
我们可以将列转换为factor,并将levels按该自定义顺序排列
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

https://stackoverflow.com/questions/68119791
复制相似问题