Reputation: 480
Does anyone know an easy way to reorder the variables in the picture to whatever order I want? I'd like to have Q1 as the highest bar and Q4 as the lowest bar.
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"))
Upvotes: 1
Views: 176
Reputation: 886938
We can convert the column to factor
with levels
arranged in that custom order
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
Upvotes: 1