Skip to content

Instantly share code, notes, and snippets.

@epijim
Last active January 2, 2016 22:59
Show Gist options
  • Save epijim/8373953 to your computer and use it in GitHub Desktop.
Save epijim/8373953 to your computer and use it in GitHub Desktop.
GoogleVis from R
# SETUP #########################################################################
#The following packages need to be installed
require(googleVis)
setwd("~/Dropbox/Personal/fun with code/GoogleVis")
# Load data #########################################################################
#All crime data is from http://www.cambs.police.uk/LocalPolicing/CrimeData.aspx
#Line graph data
data <- read.csv("CamCrime2013.csv")
sapply(data, mode)
head(data)
#Tree plot data
data2 <- read.csv("CrimeTree.csv")
sapply(data2, mode)
head(data2)
# Line graph #########################################################################
Line = gvisLineChart(data = data, xvar = "Row.names", yvar = c("House.burglary", "Other.burglary","Drugs","Robbery","Shoplifting","Vehicle.Crime"),
options = list(title = "Crimes in Cambridge City ward during 2013",
vAxis="{title:'Number of crimes'}",
hAxis="{title:'Month and year'}",
curveType="function",
width=600, height=600,
legend='top'))
plot(Line)
#Print html file
print(Line, 'chart', file='lineforwordpress.html')
# Tree map #########################################################################
#Left mouse-click to drill down, right mouse-click to move up a hierarchy
Tree <- gvisTreeMap(data2, idvar="Crime", parentvar="Type",
sizevar="Val", colorvar="Val",
options=list(fontSize=12, width=600, height=400,showScale=TRUE))
plot(Tree)
#Print html file
print(Tree, 'chart', file='treeforwordpress.html')
# Merge the two #########################################################################
MergedPlots <- gvisMerge(Line,Tree,horizontal=FALSE)
plot(MergedPlots)
#Print html file
print(MergedPlots, 'chart', file='MergedforWordpress.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment