Haven't used RStudio in a while, so I am quite rusty.
I want to create a bar chart showing the countries shipping the most freight weight in ascending order.
I have made this simple script that does the job:
df_new %>%
filter(!is.na(Freight_weight)) %>%
filter(!is.na(origin_name)) %>%
select(origin_name, Freight_weight) %>%
ggplot(aes(x = reorder(origin_name, Freight_weight, FUN = sum), y = Freight_weight)) +
geom_col() +
labs(x = "") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
However, when I try to do more with it, like adding a top_10 clause to only get the countries with the highest shipments, it doesn't work since it takes the 10 highest individual shipments and not per country.
Instead, I have tried something like this:
df_new %>%
group_by(origin_name) %>%
summarise(n = sum(Freight_weight, na.rm = TRUE)) %>%
ungroup() %>%
mutate(share = n /sum(n) %>% factor() %>% fct_reorder(share)) %>%
ggplot(aes(x = origin_name, y = n)) +
geom_col() +
labs(x = "") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
But here, I can't get the share function to work. What am I doing wrong?
Greatly appreciate your input - if I get this down I should be able to do most of the concurrent analyses!