I have the following code, that generates a plot:
library(tidyverse)
plastics <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-26/plastics.csv')
plastics %>%
group_by(year, country) %>%
summarize(hdpe = mean(hdpe)) %>%
ungroup() %>%
group_by(year) %>%
top_n(10, hdpe) %>%
ungroup() %>%
mutate(country = fct_reorder(country, hdpe)) %>%
ggplot(aes(country, hdpe)) + geom_col() + coord_flip() + facet_wrap(~year, scales = "free_y")
However, when I try to select the median of the hdpe, I don't get the 20 rows I would expect.
library(tidyverse)
plastics <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-26/plastics.csv')
plastics %>%
group_by(year, country) %>%
summarize(hdpe = median(hdpe)) %>%
ungroup() %>%
group_by(year) %>%
top_n(10, hdpe) %>%
ungroup() %>%
mutate(country = fct_reorder(country, hdpe)) %>%
ggplot(aes(country, hdpe)) + geom_col() + coord_flip() + facet_wrap(~year, scales = "free_y")
Could somebody explain what's going on here?


