- 3 Minutes Wednesdays
- Posts
- 3MW (Lollipops! A worthy alternative to bar charts.)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/1c96d84b-82f9-444e-9b5c-5ea3762572f4/fix-scale-labels__1_.png)
Lollipop charts are a worthy alternative to bar charts. For example, have a look at this bar chart.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/1e5a03b2-db8b-4a28-96a9-8261fb35eb6a/image.png)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/0c48959c-a536-46ef-9e75-5d0f90678c79/image.png)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/e2fab2fc-ab06-46ff-9bfd-a7ccc8b00c05/image.png)
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:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/b4bb78a2-9825-4709-837d-c2a6adf615ad/image.png)
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:
Each rectangle is horizontal.
Any rectangle's left border is located at
x = 0
and the right border is located atx = 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
have the same
y
valueare located at
x = 0
andx = n
, respectively.
Here's how that looks by using geom_segment()
.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ae6d1c47-09d8-491a-bad8-74e9e995308c/image.png)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/24dd7216-628d-47a6-9c51-698ff157f8f0/image.png)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/c841e580-1ea9-4df4-9ec1-26df849d69b5/image.png)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/e5ccaefd-81ab-40e6-8b83-f1833fccb962/image.png)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/0022c7a3-2bb8-4a1f-a7e8-5ff5a5f88c2c/image.png)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/2805e539-45df-4224-9fb1-ecf1dbf81ba9/image.png)
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