H.Stevens
H.Stevens

Reputation: 399

ggplot2 bar-chart order by values of one group

I am trying to order a barplot in ggplot. I am trying to do one of these side-by-side bar plots. I have data for 2 groups and I would like to sort the plot by the values of one group. At the moment, my plot looks like this:

Example barplot

I would like to achieve this:

Expected plot

Where the plot is ordered by the decreasing values of group 1 (here blue). How do I do that? I tried fct_reorder on the Category within the groups, arrange, sort, order, managed to order it outside of ggplot but it didn't come out that way... any help would be appreciated!

EDIT: According to the suggestions I tried the following:

data %>% 
  ggplot(aes(x = fct_rev(fct_reorder(labels, value)), y = value, fill 
  = variable)) +
   geom_bar(stat="identity", width=.5, position = "dodge")  +
   theme(axis.text.x = element_text(angle = 90, hjust = 1))

and I got:

Result Plot

Which is not really what I want. I want to sort by selected (the red bars) in descending order. Selected and new are obviously one variable, so in one column. If I have to separate them thats fine, I just dont know how.

Data:

structure(list(labels = c("3", "16", 
                      "20", "15", "2", 
                      "11", "19", 
                      "8", "1", 
                      "9", "12", "14", 
                      "22", "6", "5", 
                      "18", "17", "23", 
                      "21", "13", "7", 
                      "10", "4", "3", "16", 
                      "20", "15", "2", 
                      "11", "19", 
                      "8", "1", 
                      "9", "12", "14", 
                      "22", "6", "5", 
                      "18", "17", "23", 
                      "21", "13", "7", 
                      "10", "4"), variable = structure(c(1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                          1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                          2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("selected", "new"), class = "factor"), value = c(14.6323031713383, 15.9559892686597, 
                                                                                                                                                                                                  17.8080362595634, 17.9067064016601, 24.0164577112395, 24.8811598509513, 
                                                                                                                                                                                                  26.6357978795688, 26.8956804786175, 29.9814126083425, 32.0991304536091, 
                                                                                                                                                                                                  32.1102536829839, 35.4868462306579, 38.1006034580602, 51.1456293356122, 
                                                                                                                                                                                                  51.3589926258885, 52.1663444535308, 54.5568962268846, 55.046508165816, 
                                                                                                                                                                                                  57.3540750283887, 59.4724704424851, 66.1242468388916, 76.0316429487087, 
                                                                                                                                                                                                  81.2328164785417, 0.852076511173202, 14.5657895883356, 12.879854304722, 
                                                                                                                                                                                                  5.64377267586918, 7.1645313211506, 22.9837800173366, 25.5540168398152, 
                                                                                                                                                                                                  22.2012270424052, 23.9767694081375, 23.8383007367455, 29.8528524241582, 
                                                                                                                                                                                                  35.157323503853, 3.57512478022716, 2.79829414578611, 25.7839377086262, 
                                                                                                                                                                                                  1.61935008218228, 36.4318154009141, 41.9502761903778, 43.6568981148706, 
                                                                                                                                                                                                  50.8270620551394, 25.1245202778254, 17.0173820081843, 32.5450448621647
                                                                                                                                                             )), row.names = c(NA, -46L), class = "data.frame")

Upvotes: 4

Views: 7236

Answers (1)

Peter
Peter

Reputation: 12739

Process the data outside the call to ggplot to determine the factor order for the grouping variable you are interested in (labels). Then apply this factor to the labels variable in ggplot.

library(ggplot2)
library(dplyr)

sel_order <- 
  data %>% 
  filter(variable == "selected") %>% 
  arrange(desc(value)) %>% 
  mutate(labels = factor(labels))


data %>% 
  mutate(labels = factor(labels, levels = sel_order$labels, ordered = TRUE)) %>% 
  ggplot(aes(x = labels, y = value, fill = variable), group = labels) +
  geom_bar(stat="identity", width=.5, position = "dodge")  +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Created on 2020-05-26 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions