Difference between revisions of "Mapping on R"

Quick Guide

• Create a base map
• find geocode for center of desired map, adjust zoom as needed
• example code (for map of North America): newmap <- GetMap(center = c(39, -95), zoom = 3, destfile = "NorthAmerica.png")
• example code: PlotOnStaticMap(newmap, lat = c(30.268606, 41.888519), lon = c(-97.740467, -87.63548), pch = 20, col = "red")
• lat=latitude; lon=longitude, pch = point style, color="color"

Interactive Maps:

googleVis - forms a tasteful interactive map that pop up bubbles of information for each component.

Example Code:

data.poly <- as.data.frame(polygons) data.poly <- data.poly[,c(5,12)] names(data.poly) <- c("Country Name","CO2 emissions (metric tons per capita)")

map <- gvisGeoMap(data=data.poly, locationvar = "Country Name", numvar='CO2 emissions (metric tons per capita)',options=list(width='800px',heigth='500px',colors="['0x0000ff', '0xff0000']")) plot(map)

print(map,file="Map.html")

plotGoogleMaps - This is another great package that harness the power of Google’s APIs to create intuitive and fully interactive web maps. The difference between this and the previous package is that here we are going to create interactive maps using the Google Maps API, which is basically the one you use when you look up a place on Google Maps. Again this API uses javascript to create maps and overlays, such as markers and polygons. However, with this package we can use very simple R code and create stunning HTML pages that we can just upload to our websites and share with friends and colleagues.

Example Code:

library(plotGoogleMaps) polygons.plot <- polygons[,c("CO2","GDP.capita","NAME")] polygons.plot <- polygons.plot[polygons.plot\$NAME!="Antarctica",] names(polygons.plot) <- c("CO2 emissions (metric tons per capita)","GDP per capita (current US\$)","Country Name")

\#Full Page Map