Sampling Stations Figure for Manuscript
Author: Alexis Provencal
Resources
- Drawing beautiful maps programmatically with R, sf and ggplot2 — Part 1: Basics
ggspatialNorth arrow styles- Hawaii Statewide GIS Data website
- Save a ggplot
- bbox finder
Objective
The objective is to reproduce the figure below. The figure is a
combination of a base map, a locator map, labels, and a scale bar and
north arrow. We can reproduce these items using sf,
ggplot2, and ggspatial.
Citation: Wiegner TN, Mead LH, Molloy SL (2012) A Comparison of Water Quality Between Low- and High-Flow River Conditions in a Tropical Estuary, Hilo Bay, Hawaii. Estuar Coasts 36:319-333
Notes
When preparing figures for export, size matters. To render a plot in
the size you are planning on exporting it in, put
fig.height=3 in the chunk options. 3 is the height of the
rendered figure in inches. What are chunk options?
```{r, chunkOptions}
Install packages
# install.packages("sf")
library(sf)
# install.packages("ggplot2")
library(ggplot2)
# install.packages("ggspatial")
library(ggspatial)Importing data
Data was obtained from the Hawaii Statewide GIS Data website. The layers downloaded were “Coastlines” and “Streams”. The original CRS was found in the metadata file of each respective layer. The files uploaded to the working directory were the .shp and .shx files of each respective layer.
library(sf)
# Read in files
streams_raw_shp <- sf::st_read(dsn = "streams_dar.shp", stringsAsFactors = FALSE)## Reading layer `streams_dar' from data source
## `/Users/alexisprovencal/Desktop/survey_area_figrure/streams_dar.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 10785 features and 0 fields
## Geometry type: LINESTRING
## Dimension: XY
## Bounding box: xmin: 375192.1 ymin: 2095488 xmax: 925123.5 ymax: 2458275
## CRS: NA
original_crs <- sf::st_crs("+proj=utm +zone=4 +datum=NAD83 +units=m +no_defs")
sf::st_crs(streams_raw_shp) <- original_crs
streams <- sf::st_transform(streams_raw_shp, crs = 4326)
# plot(st_geometry(streams), axes = TRUE) # check
coastline_raw_shp <- sf::st_read(dsn = "coast_n83.shp", stringsAsFactors = FALSE)## Reading layer `coast_n83' from data source
## `/Users/alexisprovencal/Desktop/survey_area_figrure/coast_n83.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 13 features and 0 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 371124.5 ymin: 2094231 xmax: 940278.9 ymax: 2458612
## CRS: NA
original_crs <- sf::st_crs("+proj=utm +zone=4 +datum=NAD83 +units=m +no_defs")
sf::st_crs(coastline_raw_shp) <- original_crs
coastlines <- sf::st_transform(coastline_raw_shp, crs = 4326)
# plot(st_geometry(coastlines), axes = TRUE, lwd = 3) # checkPlotting Hilo Bay with
ggplot2
Make base map
library(ggplot2)
library(ggspatial)
# Map coastline layer and stream layer overlay
base_map_0 <- ggplot() +
ggplot2::geom_sf(data=coastlines, fill = "gray80", color = "black", lwd = 0.5) +
ggplot2::geom_sf(data=streams, color = "black", lwd = 0.25) +
ggplot2::coord_sf(datum=st_crs(streams))
# Zoom in to Hilo Bay, Hawaii
base_map_1 <- base_map_0 +
scale_x_continuous(limits = c(-155.1025, -155.04),
breaks = c(-155.09)) +
scale_y_continuous(limits = c(19.707, 19.760),
breaks = c(19.75))
# Add map labels
base_map_2 <- base_map_1 +
geom_text(aes(x = -155.075, y = 19.75, label = "Pacific Ocean"),
size = 5, color = "black",
fontface = "italic", family = "times") +
geom_text(aes(x = -155.065, y = 19.718, label = "Wailoa River"),
size = 3, color = "black",
fontface = "bold", family = "times") +
geom_text(aes(x = -155.097256, y = 19.728130, label = "Wailuku River"),
size = 3, color = "black",
fontface = "bold", family = "times")
# # Add station labels
# base_map_2_a <- base_map_2 +
# geom_text(data = station_locations,
# aes(x = Long, y = Lat, label = Station),
# size = 2.7, color = "black",
# family = "times")
# Add scale bar and north arrow
base_map_3 <- base_map_2 +
ggspatial::annotation_scale(
style = "ticks",
location = "br",
text_family = "times",
text_cex = 1
) +
ggspatial::annotation_north_arrow(
which_north = "true",
location = "tl",
pad_x = unit(1.1, "in"),
pad_y = unit(0.1, "in"),
height = unit(0.25, "in"),
width = unit(0.15, "in"),
style = ggspatial::north_arrow_orienteering(
line_width = 0.8,
line_col = "black",
fill = c("white", "black"),
text_col = "black",
text_family = "times",
text_face = NULL,
text_size = 5,
text_angle = 0)
)
# Theme base map
base_map_4 <- base_map_3 +
theme(panel.border = element_rect(color = "black", fill = NA, size = 1),
text = element_text(family = "times"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(angle = 90, vjust = 0.5, hjust = 0.5, size = 12,
color = "black", family = "times"),
axis.text.x = element_text(size = 12, color = "black", family = "times"),
panel.background = element_rect(fill = "white"))
base_map <- base_map_4
base_mapMake locator map
# Define the bounding box coordinates
hawaii_island_xlim <- c(-156.368408, -154.335938)
hawaii_island_ylim <- c(18.583776, 20.344627)
# Specify the box parameters
box_xlim <- c(-155.13, -154.975891)
box_ylim <- c(19.678798, 19.810638)
# Create an st_bbox object
big_island <- st_bbox(c(
xmin = hawaii_island_xlim[1],
xmax = hawaii_island_xlim[2],
ymin = hawaii_island_ylim[1],
ymax = hawaii_island_ylim[2]),
crs = 4326)
# Zoom in to Hawaii Island
locator_map_base <- st_crop(coastlines, big_island)
# Create the base locator map
locator_map_0 <- ggplot() +
geom_sf(data = locator_map_base, fill = "gray90", color = "black")
# Add box to resemble zoomed in map area
locator_map_1 <- locator_map_0 +
geom_rect(
aes(
xmin = box_xlim[1],
xmax = box_xlim[2],
ymin = box_ylim[1],
ymax = box_ylim[2]
),
color = "black", fill = NA, size = 0.25)
# Label
locator_map_2 <- locator_map_1 +
geom_text(aes(x = -155.55, y = 19.55,
label = "Island of\nHawai'i"),
size = 3, color = "black", family = "times")
# Theme
locator_map_3 <- locator_map_2 +
theme_void()+
theme(panel.border = element_rect(color = "black", fill = NA, size = 1),
panel.background = element_rect(fill = "white"))
locator_map <- locator_map_3
locator_mapInset the locator map in the base map
# Inset the locator map into the base map
final_map <- base_map + annotation_custom(grob = ggplotGrob(locator_map),
xmin = -155.052, xmax = -155.0375,
ymin = 19.74325, ymax = 19.764875)
final_mapSaving the plot
library(ggplot2)
ggplot2::ggsave(filename = "survey_area.png",
plot = final_map,
device = "png",
dpi = 320,
path = NULL,
height = 4,
units = "in")## Saving 10 x 4 in image
How to overlay shapefiles in Base R
# Plot coastlines with axes
plot(st_geometry(coastlines), axes = TRUE, lwd = 1, col = "gray",
xlim = c(-155.1, -155.04),
ylim = c(19.72, 19.760))
# Overlay streams on the existing plot
plot(st_geometry(streams), add = TRUE,
xlim = c(-155.1, -155.04),
ylim = c(19.72, 19.760))