5 data visualizations in 5 minutes: each in 5 lines or less of R

Mapping Starbucks in R
Screenshot by Sharon Machlis of map created with Leaflet and OpenStreetMap

The National Institute for Computer Assisted Reporting conference is an annual nerdfest at the intersection of technology and journalism. It brings together hundreds of tech folks who work in journalism and journalists who like to analyze data, with sessions about everything from Excel to machine learning. One of the conference traditions is "lightning talks," 5-minute spiels about a specific tech topic of interest. Attendees propose ideas and then we all vote on which we want to see; the top 10 get to present.

This year, my pitch on 5 data visualizations in R in 5 minutes, all in 5 lines of code or less was one of the winning proposals. Here is a version of that talk I recorded in advance.

Code for the 5 visualizations is below. If the packages leaflet, quantmod, dygraphs, ggvis and corrplot aren't installed on your system, you'll need to install them first.

Map of Starbucks

download.file("https://opendata.socrata.com/api/views/ddym-zvjk/rows.csv?accessType=DOWNLOAD", "starbucks.csv", method = "curl")
starbucks <- read.csv("starbucks.csv")
leaflet() %>% addTiles() %>% setView(-84.3847, 33.7613, zoom = 16) %>%
addMarkers(data = starbucks, lat = ~ Latitude, lng = ~ Longitude, popup = starbucks$Name)

Graph Anthem stock price

barChart(ANTM, subset='last 6 months')

Graph Atlanta-area unemployment

getSymbols("ATLA013URN", src="FRED")
names(ATLA013URN) = "rate"
dygraph(ATLA013URN, main="Atlanta area unemployment")

Correlations (adds line to download data)

download.file(destfile = "data/testscores.csv", method = "curl", url="https://raw.githubusercontent.com/smach/NICAR15/master/data/testscores.csv")
testdata <- read.csv("data/testscores.csv", stringsAsFactors = FALSE)
ggvis(testdata, ~pctpoor, ~score) %>%
layer_points(size := input_slider(10, 310, label="Point size"), opacity := input_slider(0, 1, label="Point opacity")) %>%
layer_model_predictions(model = "lm", stroke := "red", fill := "red")

# and for a correlation matrix
mycorr <- cor(na.omit(testdata[3:6]))
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(mycorr, method="shade", shade.col=NA, tl.col="black", tl.srt=45, col=col(200), addCoef.col="black")

Latest versions of these packages can be installed with


Just starting out? See my Beginner's Guide to R.

Copyright © 2015 IDG Communications, Inc.

7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon