Simple racing bar charts in R with ddplot

Racing bar charts is usually a helpful strategy to visualize comparative information over time. There are a selection of how to create them in R, together with combining ggplot with gganimate or utilizing echarts4r. However the easiest method I’ve discovered to date is with the ddplot package deal.

Welcome to ddplot

ddplot isn’t on CRAN, so that you’ll want to put in it from GitHub with a package deal like devtools or remotes. 

remotes::install_github("feddelegrand7/ddplot", build_vignettes = TRUE)

ddplot is an R wrapper for the D3 JavaScript library, and it does greater than racing bars. For instance, right here is code tailored from the package deal web site for a static bar chart utilizing the mpg information set from ggplot2.

library(ggplot2) #loaded for the mpg information set solely
mpg %>% group_by(producer) %>%
summarise(mean_cty = imply(cty)) %>%
x = "producer",
y = "mean_cty",
fill = "blue",
stroke = "black",
title = "Common Metropolis Miles per Gallon by producer",
type = "descending"
Static bar chart showing average city mpg per manufacturer from the ggplot mpg data set Sharon Machlis, IDG

Static bar chart created with ddplot.

There now we have a wonderfully good chart. However animating the bars makes issues fascinating!

Racing bar chart demo in R

For information on this demo, I’ll obtain uncooked day by day vaccination data by US state from the Our World in Knowledge GitHub repository.

all_data <- learn.csv("")

This information body has 14 columns and greater than 15,000 rows – too many for an animated bar chart. So subsequent, I’ll filter the info for as soon as per week (each Wednesday) as an alternative of day by day, beginning in March, for six states. (That must be extra manageable than making an attempt to observe 50 states’ animated bars.)

graph_data <- all_data %>%
Date = as.Date(date),
day_of_week = weekdays(Date),
PctFullyVaxed = spherical(people_fully_vaccinated_per_hundred, 1)
) %>%
filter(day_of_week == "Wednesday", date >= "2021-03-01",
location %in% c("Vermont", "Alaska", "New York",
"California", "Massachusetts", "West Virginia")) %>%
rename(State = location) %>%
choose(Date, State, PctFullyVaxed)

When you’re following alongside, be happy to pick out your individual six states.

ddplot has a barChartRace() function that’s extremely easy to use. The key barChartRace() arguments include 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 categorical palettes), and how to sort the charts. The syntax:

data, # data frame
x, # name of x axis column as string
y, # name of y axis column as string
time, #name of time variable column as strong
colorCategory, # 1 of 10 available D3 named categorical color palettes
sort # 'none', 'ascending', 'descending'

There are more ways to customize your racing bars, including frame and transition speed, font size, titles, panel and background colors, and axis ticks:

# in addition to key arguments above
frameDur, # length each frame should show in milliseconds
transitionDur, # length of frame transition in milliseconds
xFontSize and yFontSize,
xtitle and ytitle,
title, # plot title
panelcol, #graph panel background color
bgcol, #background color of the frame around the graph
font, # font family
timeLabelOpts # options include size, prefix and suffix

Here is how easy it is to generate a default racing bar chart:

graph_data %>%
x = "PctFullyVaxed",
y = "State",
time = "Date",
title = "Percent Fully Vaccinated by State"

Only x, y, and time columns are required; I also added a title. 

horizontal bar chart with 6 bars showing state vaccination rates Sharon Machlis, IDG

Screen shot of the basic racing bar chart animation. Watch the video above to see the racing bars in action.

Below is code for a more customized version with an axis title, a different label font size and color palette, and a longer time each frame displays.

graph_data %>%
x = "PctFullyVaxed",
y = "State",
time = "Date",
xtitle = "Percent Fully Vaxxed",
title = "Comparing State Vaccination Rates",
frameDur = 750,
colorCategory = "Dark2",
panelcol = "white",
bgcol = "#DCDCDC", # a light gray
xgridlinecol = "#EBEBEBFF",
timeLabelOpts = list(size = 16)
Horizontal bar chart with 6 states' vaccination data. Sharon Machlis, IDG

Screenshot of a ddplot racing bar chart with customized colors. Watch the video above to see the racing bars in action.

Check out the barChartRace() help file with


if you want to see additional ways to customize a racing bar chart.

For more R tips, head to the InfoWorld Do More With R page.

Copyright © 2021 IDG Communications, Inc.

Source link