More

    Bringing order into bar chart chaos

    Ordering the bars in a single and faceted ggplot2 bar chart

    Image by Author

    We all like order (or at least most of us). It helps us organize our thoughts and gives us a sense of peace. The same principle applies to data visualizations. We like them neat and ordered as it looks more aesthetically pleasing and facilitates comparisons. The bar chart is a visualization in which this is most obvious. Most of you are probably familiar with reordering bars when working with a single chart. However, I bet most of you would have problems doing it on a faceted chart. In the following text, I will show how to solve this issue for both scenarios using the information on the Gross Domestic Product (GDP) from the World Data Bank.

    Ordering a single bar chart

    Let’s filter only the top seven world economies in a span of four decades:

    Now, we can use a bar chart to compare their GDPs:

    It is a bit hard to conpare countries due to the country labels not being ordered by their GDPs. We can solve this by using the reorder function to order the countries descendingly by their 2020 GDPs.

    Great! We have obtained a usable plot. However, this is a chart only for the year 2020. The next section will show how to order the bars when simultaneously displaying multiple bar charts for different years.

    Multiple charts — faceting the bar chart

    In order to simultaneously illustrate the GDP comparison of countries at different times, we can use the facet_wrap function:

    We can observe the same problem as before — the bars are not ordered. Let’s try using the same recipe to solve this issue as well:

    The problem persists! You might think that this might just be an artifact of this facet layout, as using the same horizontal axis labels across the whole column automatically forces the same bar ordering. If that was the case, the problem could be remedied by allowing each facet to have its own set of horizontal axis labels. We can do this by setting the scales argument of the facet_wrap function to “free_x”.

    This didn’t solve the problem either because the ordering is again done globally. The next section will show how to bypass this issue using the tidytext package.

    Ordering the facets

    As its name suggests, the tidytext package is created to facilitate the conversion of text to a tidy format. Here, we will focus on the reorder_within function which allows us to reorder bars within a facet. Besides swapping the reorder function with reorder_within, we also have to add a call to the scale_x_reordered function from the same package in order to make this work.

    That’s it! We can see that the US has managed to stay in 1st place throughout the last four decades. However, China has the fastest growth rate, and seems to be closing in. Japan and Germany have managed to retain their positions behind the leading two, but other major European economies, such as France and the United Kingdom, seem to be lagging behind, making way for India.

    Conclusions

    This post shows how to order single and faceted bar charts. The final ordered faceted chart enabled us to more easily make comparisons between the world’s most significant economies. So, the next time you want to make comparisons between faceted bar charts, you should know what to do. For any questions or remarks, please feel free to drop a comment.

    P.S. For those of you who couldn’t stop thinking about those unordered brick columns, here’s a scratch to your itch 🙂

    Image by Author

    Bringing order into bar chart chaos Republished from Source https://towardsdatascience.com/bringing-order-into-bar-chart-chaos-f271ab91a6ee?source=rss----7f60cf5620c9---4 via https://towardsdatascience.com/feed

    Recent Articles

    spot_img

    Related Stories

    Stay on op - Ge the daily news in your inbox