Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coord_flip() changes ordering of bars within groups in grouped bar plot

Tags:

r

ggplot2

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")

enter image description here

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()

enter image description here

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()
like image 713
Ivan Bacher Avatar asked Mar 23 '18 11:03

Ivan Bacher


People also ask

What is a grouped bar plot?

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.

How do I reorder a bar plot?

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().

How to change the Order of bars in bar chart in R?

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.

What are grouped bar charts?

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.

How to change the Order of bars in a ggplot?

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.

How to change the Order of the bars manually in Excel?

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:


Video Answer


1 Answers

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:

enter image description here

like image 52
Jack Brookes Avatar answered Sep 30 '22 11:09

Jack Brookes