R is a very powerful and free (and fun) software package that allows you to do, pretty much anything you could ever want. Someone told me that there’s even code that allows you to order pizza (spoiler alert: you actually cannot order pizza using R 🙁 ). But if you’re not hungry, the statistical capabilities are astounding. Some people hate code; their brains shut down and they get sick when they look at it, subsequently falling to the floor restricted to the fetal position. I used to be that guy, but have since stood up, gained composure, sat back down, and developed a passion for statistical programming. I hope to teach the R language with some more intuition in order to keep the faint-of-heart vertical and well.
Alright so for the start in this series, I’m going to lay the foundation for a Baltimore, MD real estate analysis and demonstrate some extremely valuable spatial and statistical functions of R. So without too much blabbing, let’s jump in…
For those of you completely new to R, its interface allows you to download different packages which perform different functions. People use R for so many different data-related reasons, and the inclusion of all or most of the packages would be HUGE, so each one, housed in various servers located around the world, can be downloaded simply. For the first-time use of each package, you’ll need to install it. They will then be on your machine and you will simply load them for each future use.
For the initial map creation, we need to install the following (click Packages->Install Package(s)and holding Ctrl allows you to select multiple ones at a time):
Since these are now installed to our machine, we simply load these packages each session we use them. Loading just ggmap and RgoogleMaps will automatically load the others we just downloaded. With each session, open a script and once you’ve written out your code, highlight it and right-click “Run Line or Selection,” or just press Ctrl -> R. A quick note: unlike other programming languages like SAS and SQL, R is case sensitive.
To load run:
We will specify the object of the map center as CenterOfMap. Anything to the left of “<-" in R is the title and anything to the right are the specified contents of the object. Now for the map we're using, the shape of Baltimore behaves pretty well, so we can just type within the geocode() command “Baltimore, MD” ( R is smart and that’s all it takes).
CenterOfMap <- geocode("Baltimore, MD")
Not all areas are as symmetrically well behaved as Baltimore, and for other cases, my preferred method of centrally displaying an area’s entirety begins with entering the lat/long coordinates of your preferred center. For this, I go to Google Maps, find the area I wish to map, right click on my desired center and click “What’s here?” and taking the lat/long coordinates which are then populated in the search bar above. For Baltimore, I’m going to click just north of the harbor.
The code would then look like this:
CenterOfMap <- geocode(" 39.299768,-76.614929")
Now that we told R where the center of our map will be, lets make a map! So remember, left of the “<-" will be our name. I'd say naming the map 'BaltimoreMap' will do.
Baltimore <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat),zoom = 12, maptype = "terrain", source = "google") BaltimoreMap <- ggmap(Baltimore) BaltimoreMap
Alright, to explain what just happened, getmap() is the command to construct the map perimeters and lay down its foundation. I’m going to retype the code with what will hopefully explain it more intuitively.
get_map(c(lon=‘The longitude coordinate of the CenterOfMap object we created. The dollar sign shows what follows is part of what is before it, for example ExcelSpreadsheet$ColumnA, lat=‘The latitude coordinate of the CenterOfMap object we created.’, zoom = ‘The zoom level of map display. Play around with this and see how it changes moving from say,5 to 25′, maptype = ‘We assigned “terrain” but there are others to suit your tastes and preferences. Will show more later.’, source = ‘We assigned “google” but there are other agents who provide types of mapping data’)
And the grand unveiling of the first map…
Now that is one good lookin’ map. Just a few lines of code, too.
I’ll show you some other ways to manipulate it. I like to set the map to black & white often times so the contrast (or lack thereof) of the values later plotted are more defined. I prefer the Easter bunny/night club/glow-in-the-dark type spectrums, and so, I usually plot on the following:
Baltimore <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat),zoom = 12, maptype = "toner", source = "stamen") BaltimoreMap <- ggmap(Baltimore) BaltimoreMap
We just set the night sky for the meteor shower. Notice that all we did was change maptype from “terrain” to “toner,” and source from “google” to “stamen.”
A few other examples:
Baltimore <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat),zoom = 12,source = "osm") BaltimoreMap <- ggmap(Baltimore) BaltimoreMap
This map looks great but it’s pretty busy – probably not the best to use if you will be plotting a colorful array of values later.
Here’s a fairly standard looking one, similar to Google terrain we covered above.
Baltimore <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat),zoom=12) BaltimoreMap <- ggmap(Baltimore, extent="normal") BaltimoreMap
And one for the hipsters…
Baltimore <- get_map(c(lon=CenterOfMap$lon, lat=CenterOfMap$lat),zoom = 12, maptype = "watercolor",source = "stamen") BaltimoreMap <- ggmap(Baltimore) BaltimoreMap
George Washington and the cartographers of yesteryear would be doing cartwheels if they could see this now. The upcoming installments in this series will cover:
1) Implementing Shapefiles and GIS Data
2) Plotting Statistics and other Relationship Variables on the Maps
3) Analyzing Real Estate Data and Patterns of Residential Crime and Housing Prices
Thanks for reading this!If you have any problems with coding or questions whatsoever, please shoot me an email (pbidanset[@]gmail.com) or leave a comment below and I’ll get back to you as my schedule permits (should be quickly). Cheers.
All works on this site (spatioanalytics.com) are subject to copyright (all rights reserved) by Paul Bidanset, 2013-2014. All that is published is my own and does not represent my employers or affiliated institutions.