The world’s leading publication for data science, AI, and ML professionals.

Customize Your ggplot2 Bar Graph – 5 ways to instantly improve your R data visualizations

How to grab your reader's attention and make professional-looking graphs

Image by Author
Image by Author

Bar graphs are the bread and butter of Data Visualization. They are useful for comparing values of categorical data.

  • In tidyverse, you can construct the bar graph below with only two lines of code.
Image by Author
Image by Author

Despite its simple appearance, this graph tells you everything you need to know about the data. If you’re just doing an exploratory data analysis, its modest appearance may be sufficient. However, if you plan to communicate your data through a report or presentation, it may not be enough to draw your reader’s attention. Customizing your graph’s aesthetics can improve its readability, draw emphasis to interesting trends, and help your visualization look more professional.

Here are 5 quick and easy ways to improve your graphs.

For this tutorial, we’ll be using the same data we used to generate the graph above. It contains the profits for each computer accessory sold in a hypothetical store. You can generate the dataset with this code.

  1. Reorder factors
Image by Author
Image by Author
  • Draw attention to outstanding data points by reordering the bars in descending order.
  • For other datasets, it also may be useful to group the data by geographical location or order them alphabetically. Master fct_reorder by reading its documentation, and you can do these as well.

2. Add labels

Image by Author
Image by Author
  • Edit your title, x-, and y- axes labels to give your reader context and ensure variables are easily understood.

    3. Reposition the tick labels

Image by Author
Image by Author
  • Tick labels get harder to read as they increase in number and length. Overlapping and alignment issues can make them incomprehensible. To solve this problem you can rotate them slightly.

    Image by Author
    Image by Author
  • Or flip your coordinate axes.

    4. Reclassify less significant categories as "others"

Image by Author
Image by Author
  • Emphasize the extremity of top data points by regrouping the lowest ones together. You can do this by creating a new column with mutate(). Here, we used ifelse to reclassify all items with profits less than 3000 USD to others .

    5. Customize your color palette

Image by Author
Image by Author
  • R has a variety of in-built color palettes you can choose from. Consider colorblind-friendly palettes. The package viridis is a good place to start.

    Image by Author
    Image by Author
  • You may want to use your company’s color palette when making visualizations for work. Since profit is continuous data, I used scale_fill_gradient. You can specify the end colors of your gradient with high and low.
  • R also has custom themes you can use to instantly change your graph’s layout and background colors. I used theme_minimal() here to get a nice and clean look.

    Next Steps

A while ago, we introduced theme_minimal, one of R’s many theme presets that you can apply to instantly beautify your graph. Experiment with other default themes here and consider exploring custom themes to apply your own branding.

Improving your visualizations is a task worth investing your time in. In my experience, sharing advanced animated visualizations like the one below helped me build a following on Tech Twitter. Take a deeper dive into the libraries and functions we learned earlier to bring your graphs to the next level.


Related Articles