0

Although my query shows me values in descending order, ggplot then displays them alphabetically instead of ascending order.

enter image description here

Known solutions to this problem haven't seem to work. They suggest using Reorder or factor for values, which didn't work in this case

This is my code:

boxoffice %>%
  group_by(studio) %>%
  summarise(movies_made = n()) %>%
  arrange(desc(movies_made)) %>%
  top_n(10) %>%
  arrange(desc(movies_made)) %>%
  ggplot(aes(x = studio, y = movies_made, fill = studio, label = as.character(movies_made))) +
  geom_bar(stat = 'identity') +
  geom_label(label.size = 1, size = 5, color = "white") +
  theme(legend.position = "none") +
  ylab("Movies Made") +
  xlab("Studio")
Datatician
  • 15
  • 7

2 Answers2

1

for those wanting a more complete example, here's where I got:

library(dplyr)
library(ggplot2)

# get some dummy data
boxoffice = boxoffice::boxoffice(dates=as.Date("2017-1-1"))

df <- (
  boxoffice %>%
  group_by(distributor) %>%
  summarise(movies_made = n()) %>%
  mutate(studio=reorder(distributor, -movies_made)) %>%
  top_n(10))

ggplot(df, aes(x=distributor, y=movies_made)) + geom_col()
Sam Mason
  • 15,216
  • 1
  • 41
  • 60
0

You'll need to convert boxoffice$studio to an ordered factor. ggplot will then respect the order of rows in the data set, rather than alphabetizing. Your dplyr chain will look like this:

boxoffice %>%
  group_by(studio) %>%
  summarise(movies_made = n()) %>%
  arrange(desc(movies_made)) %>%
  ungroup() %>% # ungroup
  mutate(studio = factor(studio, studio, ordered = T)) %>% # convert variable
  top_n(10) %>%
  arrange(desc(movies_made)) %>%
  ggplot(aes(x = studio, y... (rest of plotting code)
jdobres
  • 11,339
  • 1
  • 17
  • 37
  • You basically nailed it. I'm getting what I want. Right now it's in descending order, but if I wanted to make it ascending, how would I go about it? I got rid of desc(), and it changed the order of data frame, but the plot stayed the same – Datatician Sep 07 '18 at 21:56
  • Your code has `desc` twice, actually: right before the new `ungroup` line and then again right after `top_n`. Try removing both. – jdobres Sep 07 '18 at 21:58
  • You're a wizard! – Datatician Sep 07 '18 at 21:58