{"id":130,"date":"2012-03-05T07:25:56","date_gmt":"2012-03-05T07:25:56","guid":{"rendered":"http:\/\/blog.fellstat.com\/?p=130"},"modified":"2012-03-05T07:25:56","modified_gmt":"2012-03-05T07:25:56","slug":"plot-maps-like-a-boss","status":"publish","type":"post","link":"https:\/\/blog.fellstat.com\/?p=130","title":{"rendered":"Plot maps like a boss"},"content":{"rendered":"<p>A new package OpenStreetMap has been released to CRAN this week which is designed to allow you to easily add\u00a0satellite\u00a0imagery, or open street maps to your plots. Raster maps are a great way to add context to your spatial data with a minimum outlay of effort.<\/p>\n<p>The syntax in OpenStreetMap is fairly simple, just give it a bounding box in lat\/long and it will download a high quality raster image ready for plotting<\/p>\n<p><code><br \/>\nlibrary(OpenStreetMap)<br \/>\nlibrary(rgdal)<br \/>\nmap plot(map)<\/code><\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-140\" title=\"My first world map\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot001-300x300.png\" alt=\"\" width=\"600\" height=\"600\" \/><\/a><br \/>\n(click for higher quality image)<\/p>\n<p>The above code downloads multiple map tiles and stitches together, with the level of zoom determined automatically. Unlike RGoogleMaps no files are created or stored on the hard drive. The plot gets is images from the <a title=\"mapnik\" href=\"http:\/\/mapnik.org\/\">mapnik<\/a> server, which can provide street level information. In my opinion, there rendering looks pretty clean as well.<\/p>\n<p>We can also access satellite imagery though <a title=\"Bing\" href=\"http:\/\/www.bing.com\">Bing<\/a>.<\/p>\n<p><code><br \/>\nmap plot(map)<\/code><\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0011.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-136\" title=\"Bing!\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0011.png\" alt=\"\" width=\"600\" height=\"600\" \/><\/a><\/p>\n<p>Now, that is all fine and dandy, but kind of useless unless you are able to combine it with your own data. Open street map, and Bing (and Google maps) use a particular form of the <a title=\"mercator projection\" href=\"http:\/\/en.wikipedia.org\/wiki\/Mercator_projection\">Mercator projection<\/a> which has the properties that angles are preserved, and tiles on multiple zoom levels can be stitched together. You can access this projection with the &#8216;osm&#8217; function.<\/p>\n<p>In terms of combining maps with your data there are two options. The data can be transformed to the osm() projection, or the raster map can be translated to whatever projection the data are in. Here is an example of the first option:<\/p>\n<p><code>library(UScensus2000)<br \/>\ndata(california.tract)<br \/>\nlat lon southwest california.tract plot(southwest)<br \/>\nplot(california.tract,add=TRUE,col=(california.tract@data$med.age&gt;40)+4)<br \/>\n<\/code><\/p>\n<p>Here we take a map from the UScensus2000 package, transform it to mercator coordinates, and make a choropleth. The spTransform function is a great easy way to project your data into different map coordinate systems.<\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0012.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-140\" title=\"Rplot001\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0012.png\" alt=\"\" width=\"600\" height=\"600\" \/><\/a><\/p>\n<p>We may also want to go the other way and transform the image. The openproj function can transform open street maps to different projections. Here is an example combining OpenStreetMap with the maps library by projecting the map into longlat coordinates.<\/p>\n<p><code>map map_longlat plot(map_longlat,raster=TRUE)<br \/>\nmap(\"world\",col=\"red\",add=TRUE)<\/code><\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0013.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-143\" title=\"Rplot001\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0013.png\" alt=\"\" width=\"600\" height=\"600\" \/><\/a><\/p>\n<p>but, we are not just limited to the longlat projection, we can also do weirder ones like the lambert conic conformal.<\/p>\n<p><code>map c(40,179),zoom=2,type='bing')<br \/>\nmap_longlat #Lambert Conic Conformal (takes some time...)<br \/>\nmap_llc \"+proj=lcc +lat_1=33 +lat_2=45 +lat_0=39 +lon_0=-96\")<br \/>\nplot(map_llc,raster=TRUE)<br \/>\n#add choropleth<br \/>\ndata(states)<br \/>\nst_llc plot(st_llc,add=T,col=heat.colors(48,.4)[slot(st_llc,\"data\")[[\"ORDER_ADM\"]]])<br \/>\n<\/code><\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0014.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-145\" title=\"Rplot001\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplot0014.png\" alt=\"\" width=\"600\" height=\"600\" \/><\/a><\/p>\n<p>Now, I have no idea why you would want to use this projection, but it is pretty cool none the less.<\/p>\n<p>One of the best uses for raster maps is in street level data, where it is particularly important to give the reader contextual information. Here is a plot of some locations in the Long Beach harbor, using the LA_places dataset.<\/p>\n<p><code>data(LA_places)<br \/>\nxy map c(33.73290566922855,-118.17521095275879))<br \/>\npng(width = 1000, height = 1000)<br \/>\nplot(map,raster=TRUE)<br \/>\nplot(LA_places,add=TRUE,col=\"red\")<br \/>\ntext(xy[,1],xy[,2],slot(LA_places,\"data\")[,'NAME'],adj=1)<\/code><\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplots.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-150\" title=\"Rplots\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2012\/03\/Rplots.png\" alt=\"\" width=\"800\" height=\"600\" \/><\/a><\/p>\n<p>If you are a Deducer user, the DeducerSpatial package provides a <a title=\"GUI for spatial plotting\" href=\"http:\/\/blog.fellstat.com\/?p=89\">GUI for spatial plotting<\/a> using the OpenStreetMap package.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A new package OpenStreetMap has been released to CRAN this week which is designed to allow you to easily add\u00a0satellite\u00a0imagery, or open street maps to your plots. Raster maps are a great way to add context to your spatial data with a minimum outlay of effort. The syntax in OpenStreetMap is fairly simple, just give [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6,9],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-openstreetmap","category-r","category-spatial"],"_links":{"self":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":0,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}