Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
After generating a few interactive charts with googleVis, I realized that it’s a great way to visualize numeric data, especially multi-dimentional data. Days ago, my colleague sent me a picture taken by Curiosity from Mars. He was crazy about it and claimed that this is the real science human should do. To be honest, I’m not as a big fan as he is. And I always believe that human won’t go anywhere unless we fix our problems here on earth. But still, I was glad to see NASA’s Curiosity successfully landing on Mars.
Thus, I decided to look into the NASA’s yearly budget and their progresses in Mars exploration. Both the data is available on wikipedia (NASA budget and Exploration of Mars). For this time, I directly read these data into R using readHTMLTable(), as Einar commented. Thereafter, raw data was modified to satify googleVis requirement. And finally, the chart was generated using gvisAnnotatedTimeLine(), with Date, budget (nominal and 2007 constant) , events in mars exploring history, etc. I even added three future programs, MAVEN, Insight and Red Dragon, in the future time line. The following is the snapshot; click here for the interactive chart. Hope you enjoy it!
Note: the blue line is the budget amounts in nominal million dollars; the red line is those in 2007 constant million dollars. And the original code is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | library(XML) # prepare the budget data url <- "http://en.wikipedia.org/wiki/Budget_of_NASA" budget <- readHTMLTable(url) # retrieve data table from url budget <- rbind(budget[[5]], budget[[6]]) colnames(budget)[2:4] <- c("Nominal", "Percentage in Federal Budget", "2007 Constant") budget <- as.matrix(budget) mt <- grepl("^\\d", budget[, "Year"]) # remove lines that don't start with digits (year) budget <- budget[mt, ] budget <- gsub("\\s*[[\\(].*[]\\)]\\s*", "", budget) # remove '()' or '[]' comments budget <- budget[1:(dim(budget)[1]-3), ] # remove 2013-2015 est. budget$Date <- as.Date(paste(budget$Year, "-1-1", sep = "")) budget$Nominal <- as.numeric(sub(",", "", budget$Nominal)) budget[, "2007 Constant"] <- as.numeric(sub(",", "", budget[, "2007 Constant"])) budget.nominal <- budget[, c("Date", "Nominal")] budget.nominal$id <- "Nominal" colnames(budget.nominal)[2] <- "Budget" budget.constant <- budget[, c("Date", "2007 Constant")] budget.constant$id <- "Constant 2007" colnames(budget.constant)[2] <- "Budget" dat <- rbind(budget.nominal, budget.constant) dat <- rbind(budget.nominal, budget.constant) dat$Type <- NA dat$Annot <- NA # budget data is ready # prepare the events data url.mars <- "http://en.wikipedia.org/wiki/Exploration_of_Mars" mars <- readHTMLTable(url.mars)[[9]] mt <- grepl('[Ss]uccess', mars$Result) # remove failed events mars <- mars[mt, -4] # remove the terminated date colnames(mars) <- c('mission', 'launched', 'arrived', 'type', 'result') mars.nasa <- mars[c(1:4, 9, 10, 12:14, 16, 17, 19:22), 1:4] # extract NASA events time.launch <- mars.nasa[, c(1,2,4)] colnames(time.launch) <- c('id', 'Date', 'Type') time.launch$Annot <- paste(time.launch$id, " launched on ", time.launch$time, sep = "") time.arrive <- mars.nasa[, c(1,3,4)] colnames(time.arrive) <- c('id', 'Date', 'Type') time.arrive$Annot <- paste(time.arrive$id, " arrived on ", time.arrive$time, sep = "") time.mars <- rbind(time.launch, time.arrive) for(i in 1:12) { time.mars$Date <- gsub(month.name[i], i, time.mars$Date) } time.mars$Date <- as.Date(time.mars$Date, '%d %m %Y') dat.mars <- time.mars[, 2:4] dat.mars$id <- "2007 Constant" dat.mars$Budget <- NA # events data is ready # generate the chart dat <- rbind(dat, dat.mars) dat[141:143, 'id'] <- 'Nominal' dat[141:143, 'Budget'] <- 17711 dat[141:143, 'Date'] <- as.Date(paste(c(2013, 2016, 2018), "-1-1", sep = "")) dat[141:143, 'Type'] <- c('Orbiter', rep('Lander', 2)) dat[141, 'Annot'] <- 'MAVEN will investigate Mars\' atmosphere history' dat[142, 'Annot'] <- 'Insight will compete with two other concepts in the Discovery program' dat[143, 'Annot'] <- 'Red Dragon will look for biosignatures' library(googleVis) dat.timeline <- gvisAnnotatedTimeLine(dat, datevar = 'Date', numvar = 'Budget', idvar = 'id', titlevar = 'Type', annotationvar = 'Annot', options = list(displayAnnotations = T, displayExactValues = T, allValuesSuffix = ' million dollars', dateFormat = 'yyyy', thickness = 2, width = 1000, height = 400)) plot(dat.timeline) |
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.