Here are files and code to make a simple map (credit Andrew Thompson NOAA)
You can go here to download the latest gshhg files. If you use the gshhg-bin-2.3.5.zip file or files after 2.3.4 you will have to change this in the code.
Mapping
Scott Gabara
10/2/2016
R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#setwd("/Users/yourname/folderwithfiles")
setwd("/Users/scottgabara/Documents/R/R workshop/mapping/")
rm(list=ls())
ls()
## character(0)
### You will need R v3.2.3 or higher to get this stuff to work
### install and load packages necessary for this code
#install.packages("httr")
#install.packages("ncdf4")
#install.packages("sp")
#### the above packages need to be installed but not loaded for xtractomatic to run
#install.packages("devtools")
devtools::install_github("rmendels/xtractomatic")
## Skipping install of 'xtractomatic' from a github remote, the SHA1 (073c61b6) has not changed since last install.
## Use `force = TRUE` to force installation
### We will use these packages in the examples
#install.packages("ggplot2")
#install.packages("maptools")
#install.packages("reshape2")
#install.packages("PBSmapping")
#install.packages("sp")
#install.packages("mapproj")
#install.packages("plyr")
#install.packages("data.table")
#install.packages("rgeos")
#install.packages("ggmap")
#install.packages("rgdal")
#install.packages("spatstat")
#install.packages("vegan")
### load packages
library(xtractomatic)
library(ggplot2)
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
library(reshape2)
library(PBSmapping)
##
## -----------------------------------------------------------
## PBS Mapping 2.69.76 -- Copyright (C) 2003-2016 Fisheries and Oceans Canada
##
## PBS Mapping comes with ABSOLUTELY NO WARRANTY;
## for details see the file COPYING.
## This is free software, and you are welcome to redistribute
## it under certain conditions, as outlined in the above file.
##
## A complete user guide 'PBSmapping-UG.pdf' is located at
## /Library/Frameworks/R.framework/Versions/3.3/Resources/library/PBSmapping/doc/PBSmapping-UG.pdf
##
## Packaged on 2015-04-23
## Pacific Biological Station, Nanaimo
##
## All available PBS packages can be found at
## http://code.google.com/p/pbs-software/
##
## To see demos, type '.PBSfigs()'.
## -----------------------------------------------------------
library(sp)
library(mapproj)
## Loading required package: maps
library(plyr)
##
## Attaching package: 'plyr'
## The following object is masked from 'package:maps':
##
## ozone
library(data.table)
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:reshape2':
##
## dcast, melt
library(rgeos)
## rgeos version: 0.3-20, (SVN revision 535)
## GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921
## Linking to sp version: 1.2-3
## Polygon checking: TRUE
library(ggmap)
library(rgdal)
## rgdal: version: 1.1-10, (SVN revision 622)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
## Linking to sp version: 1.2-3
library(spatstat)
## Loading required package: nlme
## Loading required package: rpart
##
## spatstat 1.46-1 (nickname: 'Spoiler Alert')
## For an introduction to spatstat, type 'beginner'
##
## Note: spatstat version 1.46-1 is out of date by more than 12 weeks; a newer version should be available.
##
## Attaching package: 'spatstat'
## The following object is masked from 'package:data.table':
##
## shift
library(vegan)
## Loading required package: permute
## Loading required package: lattice
##
## Attaching package: 'lattice'
## The following object is masked from 'package:spatstat':
##
## panel.histogram
## This is vegan 2.4-1
######## PART 1 ##########################################################################################################################################################################
############ Get background maps ###################################################################################
### download map data at http://www.soest.hawaii.edu/wessel/gshhs/ select the binary files and save them in your folder####
##### Our goal here is to plot shoreline maps that will be used in subseqent parts of this presentation #############
## importGSHHS is a function from PBSmapping
#### xlim and ylim define the boundaries of the map you want; longitude has to be in units between 0 and 360. For negative values you have to add 360
########
borders <- importGSHHS("/Users/scottgabara/Documents/R/R workshop/mapping/gshhg-bin-2.3.4/wdb_borders_f.b" , xlim= c(-121+360,-117+360), ylim = c(32,35) , maxLevel=1)
## importGSHHS status:
## --> Pass 1: complete: 1152 bounding boxes within limits.
## --> Pass 2: complete.
## --> Clipping...
rivers <- importGSHHS("/Users/scottgabara/Documents/R/R workshop/mapping/gshhg-bin-2.3.4/wdb_rivers_f.b" , xlim= c(-121+360,-117+360), ylim = c(32,35) , maxLevel=1)
## importGSHHS status:
## --> Pass 1: complete: 14 bounding boxes within limits.
## --> Pass 2: complete.
## --> Clipping...
## importGSHHS: input xlim was (239, 243) and the longitude range of the extracted data is (241.014167, 243).
poly1 <- importGSHHS("/Users/scottgabara/Documents/R/R workshop/mapping/gshhg-bin-2.3.4/gshhs_f.b" , xlim= c(-121+360,-117+360), ylim = c(32,35) , maxLevel=1)
## importGSHHS status:
## --> Pass 1: complete: 21 bounding boxes within limits.
## --> Pass 2: complete.
## --> Clipping...
## importGSHHS: input xlim was (239, 243) and the longitude range of the extracted data is (239.325778, 243).
#fortify turns the maps files into a data frame. It's from ggplot2
shore <- fortify(poly1)
border <- fortify(borders)
river <- fortify(rivers)
head(shore)
## PID SID POS X Y
## 1 1 0 1 243.0000 35.00000
## 2 1 0 2 239.3568 35.00000
## 3 1 0 3 239.3568 35.00000
## 4 1 0 4 239.3557 34.99589
## 5 1 0 5 239.3558 34.99389
## 6 1 0 6 239.3556 34.99331
#PID lats and longs for polygons, firt coord should match with last coordinate
#must connect polygon
## Set boundaries of the map
xlim <- c(-121,-117)
ylim <- c(32,35)
### Plot the background map
# projection - many different projections
p1 <- ggplot()
p1 +
coord_map(projection="mercator",xlim=xlim, ylim=ylim) + ## tells it a map is coming; keeps x and y values on same scale; xlim and ylim define the boundaries of the plot
geom_polygon(aes(x=X, y=Y, group=PID), data=shore, colour='black', fill='ivory') + ## adds the map polygons
geom_path(aes(x=X, y=Y, group=PID), data=river, colour='blue') + ## adds the river lines
geom_path(aes(x=X, y=Y, group=PID), data=border, colour='brown') + ## adds the border lines
theme_bw() + ## makes the backgraound black and white; default grey
labs(list(title="", x="longitude",y="latitude")) + ## labels
scale_x_continuous(breaks = seq(-121,-117,1)) + ## sets the scale for x axis
scale_y_continuous(breaks = seq(32,35,1)) + ## sets the scale for y axis
theme(
panel.grid.major = element_blank(), ## gets rid of grids
panel.grid.minor = element_blank(), ## get rid of grids
axis.text.x=element_text(size=12, angle=90), ## sets size and angle of x axis text
axis.text.y=element_text(size=12, angle=0), ## sets size and angle of y axis text
axis.title.x=element_text(size=15, angle=0), ## sets size and angle of x axis title
axis.title.y=element_text(size=15, angle=90) ## sets size and angle of y axis title
)
ggsave(filename = "map test southern CA bight.png",width=13.3,height=9.8)