Get startedGet started for free

Tuning your charts

1. Tuning your bar and point charts

Now that we've covered what point-data is and when a bar or point chart is appropriate, let's look at a few ways you can clean up the default plots you get with ggplot to be more efficient and attractive.

2. A busy bar chart

What if you have some data that fits the use case for a bar chart, but the class names are just too long to fit? Here, for example, we are making a bar plot of the number of cases of measles in the region EMR for the year 2015.

3. A busy bar chart

We have a problem. The axis labels are completely overlapping each-other. We could rotate the labels, but then we'd be forcing our viewer to crane their neck to just read our plot. Something not in the best interest of making your visualization accessible.

4. Flipping the bar

This is a good scenario for flipping the bar chart on its side, giving the class labels plenty of room. You can try and swap the x and y axis, but ggplot stubbornly won't let you. ... Luckily, there's an easy way around ggplot's refusal to do this. Simply adding the function coord_flip to the plot object.

5. Flipping the bar

Now we have a much more legible plot with plenty of room for the class labels! This plot we've just made could also do with an ordering of the axes, but one step at a time!

6. Excess grid

Another easy way to clean up your bar charts is to get rid of the excess gridlines running on the categorical axis. The grid lines on the numeric axis are excellent for providing a reference for comparing far away points, but since the bar itself already guides the user's eye to the value, the extra lines only serve to clutter the plot. In points charts, they are only needed when they intersect the points.

7. Default grid

To illustrate the grid problem let's look at an example. Here's a plot we made in the last lesson looking at total cases by disease in India for 1980. We can see the vertical grid lines, while not too intrusive, don't help us read the plot in any way and thus can be removed.

8. Removing vertical grid

To remove these unnecessary grid lines we can reach into the powerful theme function and set panel-grid-major-x to element_blank().

9. Removing vertical grid

Now the plot is cleaner and we've sacrificed no precision. Anytime some element of a chart is not serving a purpose to help the reader, it can, and often should, be removed.

10. Lighter background for point charts

With point charts the lack of ink drawn to the screen can clash with the standard gray background of ggplot; with the points getting lost in the low contrast environment. A quick fix for this is to use theme_minimal, a built-in theme option in ggplot2. This lightens up the background and makes the grid lines darker thus making the chart a bit easier to read. You can take this a step further as well and gently bump up the point size to a value like 2 in order to make them pop a little more. There's plenty of room!

11. Lighter background for point charts

We can see the chart has gone from a bit heavy and dark to light and easy to read. Another way of solving the contrast issue would be to change the color of the points to something that pops more on the grey background, but there is something nice about a simple black and white chart.

12. Let's try it out!

These tunings are subtle but can help make your visualizations just a bit more polished and efficient. Let's try them out!