0
votes

I'm used to working with matlab and am now trying to learn how to use the tidyverse in R (and specifically ggplot2), so I'm making a map of all the points off the coast of Nova Scotia where I am collecting data for a project. I know I'm plotting the part starting at "map" wrong, but I don't know how to make a plot with ggmap based on latitude/longitude. I assume the next line, "loc_map", then doesn't work because the "map" isn't made within the tidyverse, but I don't know how to fix this!

lat <- loc$Lat
long <- loc$Long
locs <- data.frame(long,lat)
data("coastlineWorldFine")
map <- plot(coastlineWorldFine, col='grey', clong= mean(long), 
            clat=mean(lat), span=400, projection = "+proj=merc",
            main="Sample Sites")
loc_map <- map + geom_point(data=locs, aes(x=long, y=lat), size = 20)
2
You cannot use base plot with ggplot2 geom_s. You're mixing two very different plot composition and rendering engines together and that's just not going to work.hrbrmstr

2 Answers

2
votes

Here's a start point that you can add your geom_point layer to. First, I load the libraries, which are numerous. marmap and oce are required for bathymetry and coastline data, respectively. RColorBrewer is used for the colour palette for the bathymetry, while dplyr is needed for mutate. magrittr provides the compound assignment pipe operator (%<>%), tibble is used when I restructure the bathymetry data, and ggthemes provides theme_tufte.

# Load libraries
library(ggplot2)
library(marmap)
library(oce)
library(RColorBrewer)
library(dplyr)
library(magrittr)
library(tibble)
library(ggthemes)

Here, I get the bathymetry data, restructure it, and bin it into depth intervals.

# Get bathymetry data
bathy <- getNOAA.bathy(lon1 = -68, lon2 = -56, 
                       lat1 = 41, lat2 = 49, 
                       resolution = 1, keep = TRUE)
bathy <- as.tibble(fortify.bathy(bathy))
bathy %<>% mutate(depth_bins = cut(z, breaks = c(Inf, 0, -200, -500, -1000, 
                                                 -1500, -2000, -2500, -3000, -Inf)))  

Next, I get the coastline data and put it into a data frame.

# Get coast line data
data(coastlineWorldFine, package = "ocedata")
coast <- as.data.frame(coastlineWorldFine@data)

Finally, I plot it.

# Plot figure
p <- ggplot()
p <- p + geom_raster(data = bathy, aes(x = x, y = y, fill = depth_bins), interpolate = TRUE, alpha = 0.75)
p <- p + geom_polygon(data = coast, aes(x = longitude, y = latitude))
p <- p + coord_cartesian(ylim = c(42, 47), xlim = c(-67, -57))
p <- p + theme_tufte()
p <- p + theme(axis.text = element_blank(),
               axis.title = element_blank(),
               axis.line = element_blank(),
               axis.ticks = element_blank(),
               legend.position = "right",
               plot.title = element_text(size = 24),
               legend.title = element_text(size = 20),
               legend.text = element_text(size = 18))
p <- p + scale_fill_manual(values = rev(c("white", brewer.pal(8, "Blues"))), guide = "none")
print(p)

This gives the following:

enter image description here

Adding a geom_point layer would allow you to plot your field sites.

1
votes

You didn't give us much to work with, but here goes.

library(sf)
library(lwgeom) # needed only for st_sample
library(tidyverse)

Let's get an Admin01-level Canada shapefile, yank out Nova Scotia and simplify the polygons a bit

# Get a Canada Admin01 shapefile
canada <- st_as_sf(raster::getData("GADM", country = "CAN", level = 1))
# just get Nova Scotia
ns <- filter(canada, NAME_1 == "Nova Scotia")
# simplify the polygons a bit (tweak `0.01` as you need)
ns <- st_simplify(ns, preserveTopology = TRUE, 0.01)

Now, we'll generate some point data since you didn't provide any. These will not all be on the coastline:

set.seed(2018-11-23)
some_random_points <- as_data_frame(st_coordinates(st_sample(ns, 20)))

some_random_points
## # A tibble: 18 x 2
##        X     Y
##    <dbl> <dbl>
##  1 -63.4  44.7
##  2 -63.9  45.1
##  3 -64.2  44.7
##  4 -60.8  46.8
##  5 -65.0  44.3
##  6 -63.8  45.4
##  7 -62.7  45.3
##  8 -66.1  44.3
##  9 -64.8  44.1
## 10 -64.5  44.8
## 11 -63.8  44.5
## 12 -64.7  44.8
## 13 -63.1  44.9
## 14 -65.5  43.9
## 15 -64.6  44.4
## 16 -60.4  45.9
## 17 -63.9  44.6
## 18 -62.4  45.6

Now, do some modern gg_cartography:

ggplot() +
  geom_sf(data = ns, fill = "gray90", color = "#2b2b2b", size=0.125) +
  geom_point(data = some_random_points, aes(X, Y)) +
  theme_bw()

enter image description here