Creating interactive maps is typically either difficult (e.g. openheatmap) or expensive (e.g. ESRI). However, I came across an interesting R package not to long ago that allows for flexible online visualization of geographic data, googleViz. Below is a simple example using the included R datasets. It displays graduation rates by states, mousing over a state provides its name and given graduation rate. After the map is quick tutorial on how to do it.
Data: states • Chart ID: GeoChartID193417db2630Immpressively, this map was created using only a few lines of code in R:
Let's take a closer look at what's going on:
require(googleVis) require(datasets) states = data.frame(state.name, state.x77) G3 = gvisGeoChart(states, locationvar = "state.name", colorvar = "HS.Grad", options=list(region="US", displayMode="regions", resolution="provinces", width=800, height=600)) plot(G3)
This is where we tell R which package we're using. For our map, we'll need "googleVis" and "datasets". Datasets is a package included in your R installation providing useful datasets for experimenting with functions. GoogleVis is where the magic happens, it is the library of functions Google has designed to let us common analysts use all the cool code Google uses in their visualizations. If you are receiving a warning about googleVis not installed, run the following command:
Install.packages pulls in packages published to CRAN and installs them to your local instance of R. Next, we need some data to work with, this is where the "datasets" package comes to help.
This code creates a table of state names and eight statistics such as population, life expectancy, and graduation rates. The stats are from the 1970s, so I wouldn't use it for any academic purposes, but it's great for providing examples. 'data.frame" is a function that makes ("coerces") data into a dataframe. A dataframe is an R data type for two dimensional data containing potentially different types of data ("heterogeneous"). Now that we have our data, we need to make a map. googleVis provides a function gvisGeoChart() to generate on online map. It only requires one arguement "data", but there are multiple which can also be assigned, use either ?gvisGeoChart in R or page 82 of the following pdf to learn more: http://cran.r-project.org/web/packages/googleVis/googleVis.pdf
states = data.frame(state.name, state.x77)
In this example we assign the output of gvisGeoChart to "G3" using our newly created "states" data table as the datasource. locationvar = "state.name" tells gvisGeoChart to use "state.name" from "states" to associate values with a unit of geography, in this case, US States. colorvar = "HS.Grad" indicates to gvisGeoChart to use High School Graduations rates from the States dataset for coloring the map Finally, the "options" argument can take even more arguments:
G3 = gvisGeoChart(states, locationvar = "state.name", colorvar = "HS.Grad",
options=list(region="US", displayMode="regions", resolution="provinces", width=800, height=600))"list" forces the data into a "list" data type, Rs heterogeneous one dimension data type. displayMode = "regions" indicates not to use entire countries but defined regions, the "resolution arguement specificies what kind of regions we want. resolution = "provinces" tells displayMode = "regions" we want to look at provinces. Because googlevis is gobal, it would be confusing to a non-American audience to use "states" as a subset of countries. "width" and "height" are fairly self explanatory, but they dictate to size of the resulting visulization in pixels. For more information on all options arguments see the googleVis api documentation: https://google-developers.appspot.com/chart/interactive/docs/gallery/geochart Finally, with all arguments populated to our needs, it's time to make the map!
That's it! plot() will open a new window in default web browser with your brand-new map. If you want to embed it into a page on the web, simply copy the source code into an html file or your CMS. Now that you know the basics, check out this presentation from the googleVis package authors Markus Gesmann and Diego de Castillo for far more details and some amazing examples: http://decastillo.github.io/googleVis_Tutorial/