Thirteen lines of code

November 21, 2013
By

(This article was first published on Milano R net, and kindly contributed to R-bloggers)

Thanks to R-bloggers, I discovered that googleVis 0.4.7 with RStudio integration is available on CRAN.

This is great news, but wasn't this that catched my eye. At the end of the post a beautiful map shows a terrible event of the last days: the devastating typhoon track of Haiyan that hit Southeast Asia in November.

plot

It was amazing for me that this plot was created with only 13 lines of code, the following ones:

?View Code RSPLUS
library(XML)
library(googleVis)
url <- "http://www.gdacs.org/Cyclones/report.aspx?eventid=41058&episodeid=28&eventtype=TC"
dat <- readHTMLTable(readLines(url), which = 5)
dat$latlon <- dat[, 8]
levels(dat$latlon) <- sapply(strsplit(levels(dat[, 8]), ",\n        "), function(x) paste(x[2], x[1], sep = ":"))
dat$Category <- factor(dat$Category, levels = levels(dat$Category)[c(6, 7, 1:5)], ordered = TRUE)
dat$cat <- as.numeric(dat$Category)
dat$Gust_kmh <- dat[, 6]
levels(dat$Gust_kmh) <- sapply(strsplit(levels(dat[, 6]), "km"), function(x) gsub(" ", "", x[1]))
dat$Gust_kmh <- as.numeric(as.character(dat$Gust_kmh))
M <- gvisGeoChart(dat, "latlon", sizevar = "cat", colorvar = "Gust_kmh", options = list(region = "035", backgroundColor = "lightblue", datalessRegionColor = "grey"))
plot(M)

To be honest, some more break lines will increase code readibility as reported below, but it is wonderful how this plot can be drawn with so few lines of code: two lines about options (loading libraries), nine lines to import data and only one line to produce the plot (and another one to view it!).

Sometimes, also nice things are easy to be obtained.

?View Code RSPLUS
### Loading packages
library(XML)
library(googleVis)
 
### Data import and preparation
url <- "http://www.gdacs.org/Cyclones/report.aspx?eventid=41058&episodeid=28&eventtype=TC"
dat <- readHTMLTable(readLines(url), which = 5)
dat$latlon <- dat[, 8]
levels(dat$latlon) <- sapply(
  strsplit(levels(dat[, 8]), ",\n        "),
 function(x) paste(x[2], x[1], sep = ":")
)
dat$Category <- factor(dat$Category,
  levels = levels(dat$Category)[c(6, 7, 1:5)], 
  ordered = TRUE
)
dat$cat <- as.numeric(dat$Category)
dat$Gust_kmh <- dat[, 6]
levels(dat$Gust_kmh) <- sapply(
  strsplit(levels(dat[, 6]), "km"),
  function(x) gsub(" ", "", x[1])
)
dat$Gust_kmh <- as.numeric(as.character(dat$Gust_kmh))
 
### Plot
M <- gvisGeoChart(
  dat, "latlon",
  sizevar = "cat", colorvar = "Gust_kmh",
  options = list(
    region = "035", 
    backgroundColor = "lightblue",
   datalessRegionColor = "grey"
  )
)
plot(M)

To leave a comment for the author, please follow the link and comment on his blog: Milano R net.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.