Easy racing bar charts in R with ddplot

Victoria D. Doty

Racing bar charts can be a beneficial way to visualize comparative data around time. There are a quantity of methods to build them in R, such as combining ggplot with gganimate or using echarts4r. But the simplest way I’ve identified so significantly is with the ddplot bundle.

Welcome to ddplot

ddplot isn’t on CRAN, so you are going to will need to put in it from GitHub with a bundle like devtools or controllers. 

controllers::put in_github("feddelegrand7/ddplot", develop_vignettes = True)

ddplot is an R wrapper for the D3 JavaScript library, and it does more than racing bars. For case in point, here is code tailored from the bundle internet site for a static bar chart making use of the mpg data established from ggplot2.

library(ggplot2) #loaded for the mpg data established only
mpg %>% group_by(manufacturer) %>%
summarise(indicate_cty = indicate(cty)) %>%
x = "manufacturer",
y = "indicate_cty",
fill = "blue",
stroke = "black",
title = "Normal City Miles for each Gallon by manufacturer",
kind = "descending"
Static bar chart showing average city mpg per manufacturer from the ggplot mpg data set Sharon Machlis, IDG

Static bar chart made with ddplot.

There we have a flawlessly pleasant chart. But animating the bars tends to make points intriguing!

Racing bar chart demo in R

For data in this demo, I’ll download raw daily vaccination info by US state from the Our Earth in Info GitHub repository.

all_data <- read.csv("https://github.com/owid/covid-19-data/raw/master/public/data/vaccinations/us_state_vaccinations.csv")

This data body has fourteen columns and more than 15,000 rows – much too lots of for an animated bar chart. So next, I’ll filter the data for at the time a week (every single Wednesday) instead of daily, starting in March, for six states. (That need to be more workable than attempting to observe 50 states’ animated bars.)

graph_data <- all_data %>%
Date = as.Date(day),
day_of_week = weekdays(Date),
PctFullyVaxed = round(folks_fully_vaccinated_for each_hundred, 1)
) %>%
filter(day_of_week == "Wednesday", day >= "2021-03-01",
spot %in% c("Vermont", "Alaska", "New York",
"California", "Massachusetts", "West Virginia")) %>%
rename(Point out = spot) %>%
pick(Date, Point out, PctFullyVaxed)

If you are following alongside, experience cost-free to pick your individual six states.

ddplot has a barChartRace() operate that is really uncomplicated to use. The key barChartRace() arguments contain data body, x column, y column, time column, color group if you want to alter the default (as significantly as I know, you can only pick out from 10 offered D3 categorical palettes), and how to kind the charts. The syntax:

data, # data body
x, # identify of x axis column as string
y, # identify of y axis column as string
time, #identify of time variable column as solid
colorCategory, # 1 of 10 offered D3 named categorical color palettes
kind # 'none', 'ascending', 'descending'

There are more methods to customise your racing bars, such as body and changeover pace, font size, titles, panel and qualifications colors, and axis ticks:

# in addition to key arguments above
frameDur, # length each body need to display in milliseconds
transitionDur, # length of body changeover in milliseconds
xFontSize and yFontSize,
xtitle and ytitle,
title, # plot title
panelcol, #graph panel qualifications color
bgcol, #qualifications color of the body around the graph
font, # font family members
timeLabelOpts # alternatives contain size, prefix and suffix

Below is how uncomplicated it is to deliver a default racing bar chart:

graph_data %>%
x = "PctFullyVaxed",
y = "Point out",
time = "Date",
title = "% Totally Vaccinated by Point out"

Only x, y, and time columns are demanded I also extra a title. 

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

Display shot of the standard racing bar chart animation. Observe the online video above to see the racing bars in action.

Under is code for a more custom made edition with an axis title, a distinctive label font size and color palette, and a for a longer time time each body displays.

graph_data %>%
x = "PctFullyVaxed",
y = "Point out",
time = "Date",
xtitle = "% Totally Vaxxed",
title = "Comparing Point out Vaccination Costs",
frameDur = 750,
colorCategory = "Dark2",
panelcol = "white",
bgcol = "#DCDCDC", # a light-weight gray
xgridlinecol = "#EBEBEBFF",
timeLabelOpts = listing(size = sixteen)
Horizontal bar chart with 6 states' vaccination data. Sharon Machlis, IDG

Screenshot of a ddplot racing bar chart with custom made colors. Observe the online video above to see the racing bars in action.

Test out the barChartRace() assistance file with


if you want to see further methods to customise a racing bar chart.

For more R suggestions, head to the InfoWorld Do A lot more With R webpage.

Copyright © 2021 IDG Communications, Inc.

Next Post

6 great new Java features you don’t want to miss

Java quietly underwent just one of the biggest alterations in its enhancement in 2018 with the adoption of a new release cadence. This daring new prepare resulted in Java developers acquiring a new element release each and every 6 months. This is wonderful for trying to keep Java clean and […]

Subscribe US Now