library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(rgdal) # for readOGR
## Loading required package: sp
## Please note that rgdal will be retired during 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-2, (SVN revision 1183)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.5.3, released 2022/10/21
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/gdal
## GDAL does not use iconv for recoding strings.
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 9.1.0, September 1st, 2022, [PJ_VERSION: 910]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-1
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(rnaturalearth) # for coastline data
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
Here, we’re plotting a simple map of the outline of continents.
## Reduce extent of map in latitude
latmin <- -60
latmax <- 80
lonmin <- -180
lonmax <- 180
## define domain object
domain <- c(lonmin, lonmax, latmin, latmax)
## read 110 m resolution coastline from NaturalEarth data (is a shapefile)
coast <- rnaturalearth::ne_coastline(scale = 110, returnclass = "sf")
# download and load the ocean layer and save it in a temporary destination
dir_ne <- tempdir()
layer_ocean <- rnaturalearth::ne_download(
scale = 110,
type = "ocean",
category = "physical",
returnclass = "sf",
destdir = dir_ne
)
layer_ocean <- rnaturalearth::ne_load(
scale = 110,
type = "ocean",
category = "physical",
returnclass = "sf",
destdir = dir_ne
)
# download global coastline data from naturalearth
countries <- rnaturalearth::ne_countries(scale = 110, returnclass = "sf")
##---------------------------------------------
## Create ggplot object
##---------------------------------------------
ggplot() +
# plot ocean
geom_sf(data = layer_ocean,
color = NA,
fill = "white") +
# plot coastline
geom_sf(data = coast,
colour = 'black',
size = 0.1) +
# set extent in longitude and latitude
coord_sf(xlim = c(lonmin, lonmax),
ylim = c(latmin, latmax),
expand = FALSE # to draw map strictly bounded by the specified extent
) +
# some layout modifications
xlab('') +
ylab('') +
theme_bw() +
theme(axis.ticks.y.right = element_line(),
axis.ticks.x.top = element_line(),
panel.grid = element_blank(),
panel.background = element_rect(fill = "grey70"),
plot.background = element_rect(fill = "white")
)
The function plot_map_simpl()
from the {rbeni} package implements
the steps of plotting a map described above. We can generate the map
which is just a simple outline of the continents, then add points, given
their longitude and latitude in degrees.
rbeni::plot_map_simpl() +
geom_point(
data = data.frame(x = 7.451123, y = 46.947456), # Bern
aes(x, y),
color = "red"
)