Closed Captioning Closed captioning available on our YouTube channel

How to create racing bar charts in R

InfoWorld | Sep 23, 2021

It’s easy to create animated bar charts with the ddplot package, letting viewers easily see category rankings over time.

Copyright © 2021 IDG Communications, Inc.

Hi, I’m Sharon Machlis at IDG, here with Episode 64 of Do More With R: Easy Racing Bar Charts with ddplot.
That’s D-D plot, not g-g-plot. Racing bar charts can be a fun way to show comparative data over time. And even if they’re not something you’d use in some corporate or academic reports, they can be a compelling addition in other cases – like social media posts.
You can create racing bar charts with g-g-plot and g-g-animate. Or, I show how to do them in my echarts4r tutorial. But they’re even easier in ddplot. Let’s take a look.
ddplot isn’t on CRAN, so you’ll need to install it from GitHub with a package like devtools or remotes.
The package is an R wrapper for the D3 JavaScript library. It generates images in SVG format, which is useful for Web data visualizations. SVG images are typically small, scalable, and make the underlying data machine readable.
A lot of ddplot visualizations are basic and not too interactive, like this conventional bar chart.
But I do love the racing bars! For data in this demo, I’ll download raw daily vaccination info from the Our World in Data GitHub repository. You can see it’s got 14 columns and more than 15,000 rows – too many for a bar chart. In this code I’m filtering the data for once a week (every Wednesday) instead of daily, starting in March, for just six states. That’ll be more manageable than trying to watch 50 states’ animated bars.
ddplot has a barChartRace() function that’s extremely easy to use. Here are some of its most important arguments: a data frame, x column, y column, time column, color category if you want to change the default (as far as I know, you can only choose from 10 available D3 color palettes), and how to sort. There are more ways to customize your racing bars, and these aren’t even all of them. Things like frame and transition speed, font size, titles, panel and background colors. This is how easy it is to generate a default racing bar chart! Only x, y, and time columns are required; I also added a title.
Here’s a slightly more customized version. I added an axis title, changed some colors and the time label font size, and lengthened how long each frame displays. As I said before, there are other ways to customize a chart; check out the barChartRace help file if you want to see them all.
You can find out more about the package at the ddplot website. And if you don’t want to keep stopping the video to look at the code, here’s a link to the companion InfoWorld article: That’s www infoworld dot com slash article slash 3-6-3-3-4-4-8.
That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R. You can also find the Do More With R playlist on YouTube’s IDG Tech Talk channel where you can subscribe so you never miss an episode. Hope to see you next time. Stay healthy and safe, everyone!
Featured videos from