# Sorting barplot in order! KINDLY HELP

``````fifa19_1 <- fifa19 %>%
select(Club,Wage) %>%
group_by(Club) %>%
summarise(n=sum(Wage)) %>%
arrange(-n) %>%
head(10)

ggplot(data = fifa19_1,
aes(x = Club, y = n)) +
geom_bar(stat = "identity") +
theme(legend.position = "none") +
labs(x = "Name of Club",
y = "Total Wages",
title = "Top 10 Clubs with Most Expensive Squads") +
coord_flip()
``````

I have the above code. I’m trying to ensure that the barplot is sorted in order but I’m getting an error. Also, I need to add background color to the bar plot. Kindly help!

Capture|690x422

Please use triple back ticks ````` to format a code block. See the post below on how to format code block using triple back ticks.

Thank you. I have edited the code and the readability is now better.

Hi @walteratito. To reorder the bars, you’ll probably want to use the `reorder()` function within the `aes()` call. You can read more about the function here. The syntax will look something like this:

``````ggplot(data = fifa19_1,
aes(x = reorder(Club, n), y = n)) +
geom_bar(stat = "identity") +
theme(legend.position = "none") +
labs(x = "Name of Club",
y = "Total Wages",
title = "Top 10 Clubs with Most Expensive Squads") +
coord_flip()
``````

This basically means "reorder the x-axis `Club` based on the value of `n`". If you would like the bars to be ordered from highest to lowest, add a negative (`-`) sign in front of `n` like this:

``````aes(x = reorder(Club, -n), y = n))
``````

This blog post is useful for this subject.

To change the background color, use the `panel.background` function argument within the `theme()` call. It will look something like this in your plot:

``````theme(panel.background = element_rect(fill = "gray"), legend.position = "none")
``````

Check out the documentation here.

I hope this helps! If you continue to have problems rendering your plot, please post the dataset as you have it setup here so that I can reproduce the issue here. Thanks!

Wow! Thank you very much @casey. This is very helpful.

1 Like