0

The following is my code for plotting a stacked bar plot,

netflix %>% 
  filter(release_year %in% c(2000:2020)) %>%
  transform(release_year = as.character(release_year)) %>% 
  ggplot(aes(x = release_year, fill = type)) +
  geom_bar(position = "dodge") +
  coord_flip()

this is the image of the code and the already plotted graph

plotted graph

please how can I reorder the above-plotted graph according to the number of movies released each year?

The data is sourced from kaggle https://www.kaggle.com/shivamb/netflix-shows

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Please take a look [here](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – YBS Feb 27 '21 at 16:01
  • Does this answer your question? [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – YBS Feb 27 '21 at 16:02

1 Answers1

0

So is this what you want? Sort the year by the release count in that year?


release_order_by_quantity <- netflix %>% 
  filter(release_year %in% c(2000:2020)) %>%
  group_by(release_year) %>%
  summarize(count = n(), .groups = "drop") %>%
  arrange(desc(count))

year_levels_ordered <- unique(release_order_by_quantity[["release_year"]])

netflix %>% 
  filter(release_year %in% c(2000:2020)) %>%
  mutate(release_year_factor = factor(release_year, levels = year_levels_ordered)) %>%
  ggplot(aes(x = release_year_factor, fill = type)) +
  geom_bar(position = "dodge") +
  coord_flip()

enter image description here

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26