1

I am trying to add reorder to get the data set to arrange from largest to smallest but having issues.

ggplot(Manager_Graph_Data, aes(x = reorder(Manager_Graph_Data$`Completion Rate`), y = Manager_Graph_Data$Manager)) +
  geom_bar(stat="identity", 
           position="identity", 
           fill="#0077b5")

structure(list(Manager = c("Bob Beno", "Dylan Tracy", "Ignacia Lemley", 
"Jaimee Cogdill", "Jeneva Engman", "Julianne Holdren", "Lakia Farrington", 
"Lester Braden", "Soon Mooneyham"), Complete = c(5, 5, 1, 4, 
0, 0, 3, 2, 5), Incomplete = c(6, 6, 7, 2, 3, 4, 5, 2, 3), Total = c(11, 
11, 8, 6, 3, 4, 8, 4, 8), `Completion Rate` = c(0.454545454545455, 
0.454545454545455, 0.125, 0.666666666666667, 0, 0, 0.375, 0.5, 
0.625)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-9L))

Any help will be much appreciated

Kat
  • 15,669
  • 3
  • 18
  • 51
Koolakuf_DR
  • 467
  • 4
  • 16
  • [Don't use `$` inside `aes`](https://stackoverflow.com/q/32543340/5325862). You're also calling `reorder` in such a way that it doesn't do anything to the categorical column (manager)—you're just calling it on the column you want to order by – camille Feb 08 '22 at 23:34

1 Answers1

1

It looks like you're trying to order by the completion rate. When you have a bar or column chart, ggplot will order by the factor (or character) field. So to change the order, set the factor levels. There are a variety of ways to do this. Here is one way:

library(tidyverse)

# order by rate decreasing 
MgrRate <- Manager_Graph_Data %>% 
  arrange(`Completion Rate`, decreasing = T) %>% 
  mutate(Manager = ordered(Manager, levels = .$Manager))

ggplot(MgrRate, 
       aes(x = `Completion Rate`,
           y = Manager)) +
  geom_bar(stat="identity", 
           position="identity", 
           fill="#0077b5")

enter image description here

In case you were not aware, if you want to set an x and y, try using geom_col() to simplify things.

# alternatively (creating the same plot)
ggplot(MgrRate, 
       aes(x = `Completion Rate`,
           y = Manager)) +
  geom_col(fill="#0077b5")

If you actually wanted to order by the manager, here's an example of how to do that. (This is by the first name of the manager.)

# order by manager's first name
Mgr <- Manager_Graph_Data %>% 
  arrange(desc(Manager)) %>% 
  mutate(Manager = ordered(Manager, levels = .$Manager))

ggplot(Mgr, 
       aes(x = `Completion Rate`,
           y = Manager)) +
  geom_col(fill="#0077b5")

enter image description here

Just so you are aware, when you flip the axes (but the factor on y, instead of x) you have to reverse the order.

Kat
  • 15,669
  • 3
  • 18
  • 51