I have a netCDF file containing bathymetry data that I am trying to combine on the same plot with shapefiles that are in WGS84. When I plot the netCDF data it doesn't have any coordinate system, how can I set a coordinate system for the netCDF data so I can plot all my spatial files together on the same plot?
library(ncdf4)
library(raster)
library(rgdal)
library(ggplot2)
# READ IN AND PARSE BATHYMETRY DATA
# from https://www.ngdc.noaa.gov/thredds/catalog/regional/catalog.htmldataset=regionalDatasetScan
#/albemarle_sound_s010_30m.nc
nc_data <- nc_open('albemarle_sound_s010_30m.nc')
# Get Data from netCDF File
x_coord <- ncvar_get(nc_data, "x")
y_coord <- ncvar_get(nc_data, "y")
Band1 <- ncvar_get(nc_data, "Band1")
# Close connection
nc_close('albemarle_sound_s010_30m.nc')
# Melt data into dataframe for plotting in ggplot2
Band1_df = melt(Band1)
# Plot Data
ggplot() +
geom_raster(aes(x = X1, y = X2, fill = value), data = Band1_df) +
scale_fill_continuous(na.value = "transparent") +
ylab("") +
xlab("") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.7),
panel.background = element_rect(fill = "black"),
plot.title = element_text(color = "black", size = 16, hjust = 0.5))