I have created a group bar plot (img 1) and used coord_flip()
to move the
groups bars to the y axis. I noticed that coord_flip()
also reorders the way each bar is presented in the groups.
E.g. In img 1 bars flow from Group A-D. However, in img 2 the bars flow from Group D-A. How can I keep the same ordering of the bars within each group using coord_flip()
?
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) +
geom_bar(stat="identity", position="dodge")
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) +
geom_bar(stat="identity", position="dodge") +
scale_x_discrete(limits = as.character(16:1)) +
coord_flip()
Working example (subset of data -- questions 8-11)
dput() output:
structure(list(group = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), correct_per = c(90.4761904761905, 100, 100, 87.5, 83.3333333333333, 90.9090909090909, 84.6153846153846, 87.5, 80.9523809523809, 88.6363636363636, 100, 70.8333333333333, 63.4146341463415, 76.7441860465116, 76.9230769230769, 62.5), nr_correct = c(38L, 44L, 26L, 21L, 35L, 40L, 22L, 21L, 34L, 39L, 26L, 17L, 26L, 33L, 20L, 15L), nr_incorrect = c(4L, 0L, 0L, 3L, 7L, 4L, 4L, 3L, 8L, 5L, 0L, 7L, 15L, 10L, 6L, 9L), length = c(42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 41L, 43L, 26L, 24L), qid = c("8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11")), .Names = c("group", "correct_per", "nr_correct", "nr_incorrect", "length", "qid"), row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame"))
save to file
all_Q <- dget(filename)
ggplot(all_Q, aes(x=qid, y=correct_per, fill=group)) +
geom_bar(stat="identity", position="dodge") +
scale_x_discrete(limits = as.character(11:8)) +
coord_flip()
grouped bar charts are Bar charts in which multiple sets of data items are compared, with a single color used to denote a specific series across all sets. As with basic Bar charts, both vertical and horizontal versions of grouped bar charts are available.
Another way to re-order barplots is to use the base R function reorder(). It kind of works the same way and we reorder the factors based on population size. Reordering bars in barplot using base R function reorder() also results in the same barplot as we ordered by fct_reorder().
In this article, we will discuss how to change the order of bars in bar chart in R programming language. We can change the order of bars by using two plots ggplot and barplot. Firstly create a sample dataset and plot the graph-Manual. Now let’s reorder them accordingly. Reordering in ggplot is done using theme () function.
grouped bar charts are Bar charts in which multiple sets of data items are compared, with a single color used to denote a specific series across all sets. As with basic Bar charts, both vertical and horizontal versions of grouped bar charts are available.
We can change the order of bars by using two plots ggplot and barplot. Firstly create a sample dataset and plot the graph-Manual. Now let’s reorder them accordingly.
If we want to change the order of the bars manually, we need to modify the factor levels of our ordering column. We can do that with the following R syntax:
One "hacky" way to do this is to set the position_dodge
width to be a negative number. .9
is the default, so -.9
would create the same appearance but in reverse.
library(ggplot2)
all_Q <- structure(list(group = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), correct_per = c(90.4761904761905, 100, 100,
87.5, 83.3333333333333, 90.9090909090909, 84.6153846153846, 87.5, 80.9523809523809, 88.6363636363636, 100, 70.8333333333333, 63.4146341463415, 76.7441860465116, 76.9230769230769, 62.5), nr_correct = c(38L, 44L, 26L, 21L, 35L, 40L, 22L, 21L, 34L, 39L, 26L, 17L, 26L, 33L, 20L, 15L), nr_incorrect = c(4L, 0L, 0L, 3L, 7L, 4L, 4L, 3L, 8L, 5L, 0L, 7L, 15L, 10L, 6L, 9L), length = c(42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 42L, 44L, 26L, 24L, 41L, 43L, 26L, 24L), qid = c("8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11")), .Names = c("group", "correct_per", "nr_correct", "nr_incorrect", "length", "qid"), row.names = c(NA,
-16L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(all_Q, aes(x = qid, y = correct_per, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(-.9)) +
scale_x_discrete(limits = as.character(11:8)) +
coord_flip()
Alterantively, you could reorder the levels of the factor, then reverse the legend.
all_Q$group <- factor(all_Q$group, levels = rev(levels(all_Q$group)))
ggplot(all_Q, aes(x = qid, y = correct_per, fill = group)) +
geom_bar(stat = "identity", position = "dodge") +
scale_x_discrete(limits = as.character(11:8)) +
coord_flip() +
guides(fill = guide_legend(reverse = TRUE))
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With