We can find a (Google) map on almost any website and blog these days. And we are so lucky that companies like Google took the initiative to create good maps and their associated interfaces like Google Earth. But I think the best contribution is the ability to overlay results of analysis on the Google maps. Several API's have been developed to do this kinds of works. For my goal I found an API named "Google Chart Tools, formerly known as Google Visualisation
API" and a R package that gives you the ability to overlay your results on the Google maps. I found it to be usefull to overlays some of the results on beautiful maps from Google (finding a good map that you can overlay your results could give you a headache!). Below I summarize the things that are needed to overlay your point based results:
1- Read your data into R as a data frame.
2- Create filed or column called "LatLong" in that data frame that binds the latitudes and longitudes as a string character in this way: "lat:long".
Tip: If your data is projected you need to transform your coordinates and remember the X is longitude!.
3- Use the function gvisMap() that only needs 3 arguments (name of data frame, "latlong", "a column name in your data frame") to create your HTML output.
4- Use plot() command to fire up the browser and show the over lay. (Example below:)
//-->
-->
1- Read your data into R as a data frame.
2- Create filed or column called "LatLong" in that data frame that binds the latitudes and longitudes as a string character in this way: "lat:long".
Tip: If your data is projected you need to transform your coordinates and remember the X is longitude!.
3- Use the function gvisMap() that only needs 3 arguments (name of data frame, "latlong", "a column name in your data frame") to create your HTML output.
4- Use plot() command to fire up the browser and show the over lay. (Example below:)
Data: Map • Chart ID: GeoMapIDd5479cd4481
R version 2.15.1 (2012-06-22) • googleVis-0.3.1 • Google Terms of Use • Data Policy
R version 2.15.1 (2012-06-22) • googleVis-0.3.1 • Google Terms of Use • Data Policy
Here is the code for the above example (I took it from the package help page):
library(googleVis) input<- read.csv("data.csv") select<- input[which(input$Subgroup=="Total 5-14"),] select<- input[which(input$Subgroup=="Total 5-14 yr"),] Map<- data.frame(select$Country.or.Area, select$Value) names(Map)<- c("Country", "Percentage") Geo=gvisGeoMap(Map, locationvar="Country", numvar="Percentage", options=list(height=350, dataMode='regions')) print(Geo, file="Andrew.html") #copy and paste the content of this html file (between the \body tags) into your blogger post or other websites. plot(Geo)
In the next post I will discuss the downside of the GoogleVis for me!.
No comments:
Post a Comment