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)
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:
Adding a geom_point
layer would allow you to plot your field sites.