I wonder if there is an easier way to do the manual reordering of bars that I did with the code below, that includes creating new data frame. I'd like to do that without creating another data frame. Is there a way?
A plot with bars in wrong order
ggplot(data1, aes(x=question, y=mean)) +
geom_bar(stat ="identity")+
geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd)) +
coord_flip()
The trick with new data frame
data2 <- data1
data2$question <- factor(data2$question,
levels = c("Q4", "Q3", "Q2", "Q1"))
A plot with bars in right order
ggplot(data2, aes(x=question, y=mean)) +
geom_bar(stat ="identity")+
geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd)) +
coord_flip()
data1
data1 <- structure(list(question = c("Q1", "Q2", "Q3", "Q4"), n = c(10L,
10L, 10L, 10L), mean = c(4.9, 5.1, 4.9, 4.1), sd = c(0.994428926011753,
1.66332999331662, 1.28668393770792, 1.28668393770792)), row.names = c(NA,
4L), class = c("tbl_df", "tbl", "data.frame"))