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

Visualization in R

Visualization with the quantmod package in R

Credit: Screenshot by Sharon Machlis of data visualized by the R quantmod package

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's 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")
library("leaflet")
leaflet() %>% addTiles() %>% setView(-84.3847, 33.7613, zoom = 16) %>%
addMarkers(data = starbucks, lat = ~ Latitude, lng = ~ Longitude, popup = starbucks$Name)

Graph Anthem stock price

library("quantmod")
getSymbols("ANTM")
barChart(ANTM, subset='last 6 months')

Graph Atlanta-area unemployment

getSymbols("ATLA013URN", src="FRED")
names(ATLA013URN) = "rate"
library("dygraphs")
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)
library(ggvis)
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]))
library(corrplot)
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

install.packages("devtools")
devtools::install_github("rstudio/leaflet")
install.packages("quantmod")
install.packages("dygraphs")
install.packages("ggvis")
install.packages("corrplot")

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

To express your thoughts on Computerworld content, visit Computerworld's Facebook page, LinkedIn page and Twitter stream.
Windows 10 annoyances and solutions
Shop Tech Products at Amazon
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.