I'm trying to make a bar plot showing expenses and revenues for 7 countries, and I want to order them by their expenses. The problem is that ggplot orders it by the revenue, and I don't know how to tell ggplot which of the variables it should use when I pivoted the data frame. How can I do this?
I first tried using reorder(), which arranged the bars after revenue, then I tried using arrange(), both before and after pivoting, which didn't work. I've also tried to manually impute the order, which did arrange them in the order I wanted, but it messed up the spacing between the bars, somehow.
Any help would be highly appreciated!
#Replication data:
df_country <- data.frame(country = c('Denmark', 'Finland', 'Japan', 'Norway', 'Switzerland', 'Germany', 'USA'),
revenues = c(283631, 238985, 133487, 354506, 221348, 235079, 181078),
expenses = c(261849, 243311, 144859, 314360, 212768, 227343, 218995))
#My attempt, which orders by revenue:
df_country %>%
pivot_longer(cols = c('revenues', 'expenses'),
names_to = 'vars',
values_to = 'values') %>%
ggplot(aes(x = reorder(country, values), y = values, fill = vars)) +
geom_bar(stat = 'identity', width = 0.7, position = position_dodge(width = 0.9)) +
scale_y_continuous(breaks = seq(0, 3.5e+5, 5e+4)) +
scale_fill_manual(values = c('#ff9214', '#003f5c')) +
labs(x = NULL) +
theme(
axis.text.x = element_text(hjust = -0.25,
margin = margin(t = -2, unit = 'mm')),
axis.ticks = element_blank(),
panel.background = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = '#909090'),
text = element_text(family = 'serif')
)
You can reorder country
in the original data frame first, then plot:
df_country %>%
mutate(country=reorder(country, -expenses)) %>%
pivot_longer(cols = c('revenues', 'expenses'),
names_to = 'vars',
values_to = 'values') %>%
ggplot(aes(x = country, y = values, fill = vars)) +
...
theme(
axis.text.x = element_text(margin = margin(t = -2, unit = 'mm')),
...