Difference between revisions of "Mapping on R"

From edegan.com
Jump to navigation Jump to search
imported>Kunal
imported>Kunal
Line 5: Line 5:
 
===Mapping with Leaflet===
 
===Mapping with Leaflet===
  
 +
[[Image:leaflet.jpg|400px|thumb|right|Interactive map on Leaflet]]
 +
[[Image:marker.jpg|400px|thumb|right|Interactive map with Markers]]
  
 
Leaflet is one of the most popular open-source JavaScript libraries for interactive maps.  
 
Leaflet is one of the most popular open-source JavaScript libraries for interactive maps.  

Revision as of 16:12, 20 April 2016

Interactive Maps:

Mapping with Leaflet

Interactive map on Leaflet
Interactive map with Markers

Leaflet is one of the most popular open-source JavaScript libraries for interactive maps. This R package makes it easy to integrate and control Leaflet maps in R.

Introduction: https://rstudio.github.io/leaflet/ Markers: https://rstudio.github.io/leaflet/markers.html Github: https://github.com/rstudio/leaflet

Features

  • Interactive panning/zooming
  • Compose maps using arbitrary combinations of:
  *Map tiles
  *Markers
  *Polygons
  *Lines
  *Popups
  *GeoJSON
  • Create maps right from the R console or RStudio
  • Embed maps in knitr/R Markdown documents and Shiny apps
  • Easily render Spatial objects from the sp package, or data frames with latitude/longitude columns
  • Use map bounds and mouse events to drive Shiny logic



Other R Packages

Helpful links: http://www.r-bloggers.com/interactive-maps-for-the-web-in-r/ To find HEX codes for RGB colors: http://www.javascripter.net/faq/rgbtohex.htm

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

map <- plotGoogleMaps(polygons.plot,zoom=4,fitBounds=F,filename="Map_GoogleMaps.html",layerName="Economic Data")

#To add this to an existing HTML page

map <- plotGoogleMaps(polygons.plot,zoom=2,fitBounds=F,filename="Map_GoogleMaps_small.html",layerName="Economic Data",map="GoogleMap",mapCanvas="Map",map.width="800px",map.height="600px",control.width="200px",control.height="600px")

Quick Guide to Leaflet Alternatives

  • Make sure you have downloaded the RgoogleMaps package.
    • code: library(Rgooglemaps)
  • 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")
  • Transform addresses into geocode via http://www.geocodezip.com/v3_example_geo2.asp?addr1=United%20States%20of%20America&geocode=1
  • Plot map, adjust variables
    • 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"

Example Code: Chicago MSA w/ color by Industry

library(RgoogleMaps) library(ggplot2) library(colorspace) library(RColorBrewer)

merged.data<- merge(Chicago.Adresses.for.geocode.v2, Chicago.Addresses.w.Industries, by="Name")

newmap <- GetMap(center = c(41.92,-87.78), zoom = 11,GRAY = TRUE, destfile = "chicago_test.png")

col.list <- c("#EB6D2E","#EB6D2E", "#EB6D2E", "#00B2A9", "#516B66", "#00B2A9") palette(col.list)

PlotOnStaticMap(newmap, lat=c(Chicago.Adresses.for.geocode.v2$Latitude),lon=c(Chicago.Adresses.for.geocode.v2$Longitude), pch=20, col=c(Chicago.Addresses.w.Industries$Industry))