Five great charts in 5 lines of R code each

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Sharon Machlis is a journalist with Computerworld, and to show other journalists how great R is for data visualization she shows them these five data visualizations, each of which can be created in 5 lines of R code or less.

 

I’ve reproduced Sharon’s code and charts below. I did make a couple of tweaks to the code, though. I added a call to checkpoint(“2016-08-22”) which, if you’ve saved the code to a file, will install all the necessary packages for you. (I also verified that the code runs with package versions as of today’s date, and if you’re trying out this code at a later time it will continue to do so, thanks to checkpoint.) I also modified the data download code to make it work more easily on Windows. Here are the charts and code:

Starbucks

## Plot all Starbucks locations using OpenStreetMap
## Credit: http://www.computerworld.com/article/2893271/business-intelligence/5-data-visualizations-in-5-minutes-each-in-5-lines-or-less-of-r.html
library(checkpoint)
checkpoint("2016-08-22")
file <- "https://opendata.socrata.com/api/views/ddym-zvjk/rows.csv"
starbucks <- read.csv(file)
library(leaflet); library(magrittr)
leaflet() %>% addTiles() %>% setView(-84.3847, 33.7613, zoom = 16) %>%
addMarkers(data = starbucks, lat = ~ Latitude, lng = ~ Longitude, popup = starbucks$Name)
view raw starbucks.R hosted with ❤ by GitHub

Stocks

## Plot last 6 months of ANTM share price
## Credit: http://www.computerworld.com/article/2893271/business-intelligence/5-data-visualizations-in-5-minutes-each-in-5-lines-or-less-of-r.html
library(checkpoint)
checkpoint("2016-08-22")
library(quantmod)
getSymbols("ANTM", auto.assign=TRUE)
barChart(ANTM, subset = 'last 6 months')
view raw stocks.R hosted with ❤ by GitHub

Atlanta

## Plot Atlanta area unemployment
## Credit: http://www.computerworld.com/article/2893271/business-intelligence/5-data-visualizations-in-5-minutes-each-in-5-lines-or-less-of-r.html
library(checkpoint)
checkpoint("2016-08-22")
library(quantmod)
getSymbols("ATLA013URN", src = "FRED")
names(ATLA013URN) = "rate"
library(dygraphs)
dygraph(ATLA013URN, main = "Atlanta area unemployment")
view raw unemployment.R hosted with ❤ by GitHub

Scatter

## Credit: http://www.computerworld.com/article/2893271/business-intelligence/5-data-visualizations-in-5-minutes-each-in-5-lines-or-less-of-r.html
library(checkpoint)
checkpoint("2016-08-22")
## Correlation plot
file <- "https://github.com/smach/NICAR15data/raw/master/testscores.csv"
testdata <- read.csv(file, 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")
view raw correlations.R hosted with ❤ by GitHub

Corrmap

For more on the charts, read Sharon’s Computerworld article by following the link below.

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

 

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)