Thirteen lines of code

[This article was first published on Milano R net, 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.

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 their blog: Milano R net.

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)