Libraries used:
library(sp)
library(sf)
library(ggplot2)
library(ggmap)
Created a dataframe called "coordinate.data" with longitude & latitude as column names, & weather station locations as row names.
longitude <- c(-73.964482,-73.953678,-73.893522,-73.815856,-74.148499)
latitude <- c(40.767544,40.631762,40.872481,40.734335,40.604014)
coordinate.data <- data.frame(longitude,latitude)
rownames(coordinate.data) <- c("MANH","BKLN","BRON","QUEE","STAT")
I then retreived shapefile data of NJ counties and NYC boroughs, & deleted all unnecessary columns so only the geometry field was left in both shapefiles. The NYC Boroughs shapefile data was downloaded from NYC Open Data, & the NJ county boundaries was downloaded from NJGIN Open Data.
nj.shp <- st_read("~/Downloads/NJ/NJ_Counties.shp")
nj <- nj.shp[,-(1:21)]
nyc.shp <- st_read("~/Downloads/NY/NYC_Boroughs.shp")
nyc <- nyc.shp[,-(1:4)]
I formatted both shapefiles to have the same projection (ESPG code 3857) and combined them into a shapefile dataframe with 26 observations (counties/boroughs) in one variable (geometry).
same.projection <- CRS("+init=EPSG:3857")
nj.data <- st_transform(nj,same.projection)
new.projection <- CRS("+init=EPSG:3857")
nyc.data <- st_transform(nyc,new.projection)
combined.data <- rbind(nj.data,nyc.data)
I am now attempting to plot the combined shapefile ("combined.data") on a map, in addition to the weather station locations ("coordinate.data"). When I attempt this, it runs inevitably & R shuts down. If I remove geom_sf(...), it plots the stations & formats everything correctly, so I assume the issue is with this line of code.
mesonet.map <-ggplot() +
ggtitle("NY Mesonet Site Locations") +
xlab("Longitude") +
ylab("Latitude") +
geom_point(data=coordinate.data,aes(x=longitude,y=latitude))+
geom_text(aes(x=longitude,y=latitude,label=rownames(coordinate.data)),size=3.25,nudge_y=0.02)+
geom_sf(data=combined.data,fill='darkgreen') +
mesonet.map + theme(
panel.background=element_rect(fill="lightblue",color="lightblue",size=0.5,linetype="solid"),
panel.grid.major=element_line(size=0.5,linetype='solid',color="white"),
panel.grid.minor=element_line(size=0.25,linetype='solid',color="white")
)