3MW (Lollipops! A worthy alternative to bar charts.)

Guten Tag!

Many greetings from Ulm, Germany. Like every week, let me share with you my newest video tutorial before we get started with this week’s newsletter.

Ever wondered how to make nice labels for the numbers on your axes? If so, I got just the video for your. It shows you how to use scale_*_*() layers to transform your axes. It even shows you how the related {scales} package works. As always, you can find it on YouTube.

Lollipop charts are a worthy alternative to bar charts. For example, have a look at this bar chart.

If you can create a bar chart like this with ggplot, then creating a lollipop chart is simple too. Here's how that chart could look.

I'm not saying that bar charts are obsolete now and you should always use lollipops instead. But I do think that they can be a nice alternative for a fresh, light-weight look. As always, you can find the full code on GitHub.

A bar chart first

To understand how to transform a bar chart into a lollipop chart, let us create a bar chart first. Here's the code that produced the above bar chart.

Notice that this is the same chart that we looked at last week. This not by coincidence. Last week, we learned to tweak axes expansions so that the bar labels are directly adjacent to the bars. This is a good look for a lollipop chart too. Hence, we piggyback off the work we did last week.

Transform to lollipop

To turn our bar chart into a lollipop chart, we need to understand the data that we used in our previous chart. Here it is again:

Recall that we used this tibble in connection with geom_col() and mapped y to the manufacturer and x to the n, i.e. the counts. Thus, geom_col() understands that it needs to draw rectangles with two properties:

  1. Each rectangle is horizontal.

  2. Any rectangle's left border is located at x = 0 and the right border is located at x = n.

Now, to turn this into a lollipop chart, we have to replace the rectangles by lines that mimic these two properties. Therefore, we have to draw line segments whose start and end point

  1. have the same y value

  2. are located at x = 0 and x = n, respectively.

Here's how that looks by using geom_segment().

And to make this into a true lollipop chart, we need to add points to the right ends of the line segments. To do so, add another geom_point() layer.

Simple, right? Finally, to make this a little bit prettier, we can customize our theme a little bit by removing the grid lines for the y axis.

Hope you’ve enjoyed this week’s newsletter. If you want to reach out to me, just reply to this mail or find me on Twitter, uhhh I mean X.

See next week,
Albert 👋

If you like my content, you may also enjoy these:

Reply

or to participate.