## Making a Map in R ## Goal: to make a map using high-resolution spatial polygon data from the web ## 1. Load libraries (code below courtesy of Ivan Ramler at SLU) if(!require(maps)) {install.packages("maps", repo = "http://cran.rstudio.com/"); require(maps)} if(!require(RColorBrewer)) {install.packages("RColorBrewer", repo = "http://cran.rstudio.com/"); require(RColorBrewer)} if(!require(classInt)) {install.packages("classInt", repo = "http://cran.rstudio.com/"); require(classInt)} if(!require(mapdata)) {install.packages("mapdata", repo = "http://cran.rstudio.com/"); require(mapdata)} if(!require(maptools)) {install.packages("maptools", repo = "http://cran.rstudio.com/"); require(maptools)} ## 2. Download spatial polygon data from the web (high-res option). load(url("http://biogeo.ucdavis.edu/data/gadm2/R/USA_adm2.RData")) ##remember quotes around URL! ##Note: if you look at the gadm value, you'll see that it is a Large SpatialPolygons DataFrame. Click on the blue arrow next to the name and you will see the variables that make up "gadm". One is NAME_1 which are the states of the US. For this exercise, we want to look at Rhode Island, so that is what we are going to focus on. So, we need to extract this information. To do that, enter the following code: ri <- gadm[gadm$NAME_1 == "Rhode Island",] ##Remember the comma! ## 3. Plot the map. ## To plot the whole state: plot(ri) ## To plot a certain area of the state, use the latitude and longitude to set the limits for the wanted area. plot(ri, xlim = c(-71.6, -71.2), ylim = c(41.3, 41.85)) ##Sometimes it takes a little trial and error to get the limits right. Note that x values are longitude and y values are latitude. ##4. Add points. #a. First read in site data. sites <- read.csv('http://myslu.stlawu.edu/~msch/NoCoR/2015sites.csv', header = T, stringsAsFactors=FALSE) #b. Next, to simply plot all sites with blue markers: points(sites$Lon, sites$Lat, pch=19, col="blue") ?pch #shows different marker shapes #c. This isn't good enough for me, though! I want to add markers that show different site categories (e.g. Beach, Boat Ramp, Marina/Port). plotvar <- sites$Type nclr <- 3 #number of colors plotclr <- brewer.pal(nclr, "Greys") # ?brewer.pal shows the possible color palettes class <- classIntervals(plotvar, nclr) #ignore warning message for the purposes of this exercise colcode <- findColours(class, plotclr) points(sites$Lon, sites$Lat, pch=21, col="black", bg=colcode, cex=1) #col is the color of the outline of the circle; bg is the color of the circle itself #pch refers to marker style, cex refers to marker size #5. Add a legend. legend("bottomright", legend=c("Beach", "Boat Ramp", "Marina/Port"), fill = attr(colcode, "palette"), cex=0.7, bty="n") #6. You can also plot right from Google Maps! There are a lot of ways to do this. Here's one example: if(!require(RgoogleMaps)) {install.packages("RgoogleMaps", repo = "http://cran.rstudio.com/"); require(RgoogleMaps)} lat <- c(41.4, 41.8) lon <- c(-71.5, -71.2) center=c(mean(lat), mean(lon)) zoom <- 10 termap <- GetMap(center=center, zoom=zoom, maptype = "terrain", destfile="RIterrain.png") # This will save the file in the project folder you have designated (if you are using R Studio) ## Sources and Resources #http://milanor.net/blog/?p=594 #http://www.r-bloggers.com/mapping-points #http://www.gadm.org/download