簡體   English   中英

在同一個ggplot plot上繪制Shapefile和Raster

[英]Plotting Shapefile and Raster on the same ggplot plot

我在同一 plot 上繪制柵格測深數據和 shapefile 時遇到問題。 我認為這是一個突出的問題,但我不確定。

當我運行代碼時,我得到一個錯誤:坐標系已經存在。 添加新的坐標系,它將替換現有的坐標系。 錯誤:geom_sf() 必須與 coord_sf() 一起使用

############################################################################################ 
############################
# ATLANTIC COAST RIVER HERRING MIGRATION VIZ
# script to plot States, Topography, Catch 
# author:

############################################################################################

library(ncdf4) 
library(raster) 
library(rgdal) 
library(ggplot2)
library(fields)
library(reshape)
library(RColorBrewer)
library(maps)
library(rgdal)
library(gridExtra)
library(maptools)
library(scales)
library(raster)
library(rgeos)
library(ggplot2)
library(grid)
library(mapplots)
library(dplyr)    
library(mapproj)
library(ggmap)
library(ggspatial)
library(sf)
library(ggrepel)
library(cowplot)
library(rnaturalearth)
library(rnaturalearthdata)
library(rnaturalearthhires)
library(stringr)
library(marmap)
library(oce)
library(terra)
library(rasterVis)

theme(
  text = element_text(family = "Ubuntu Regular", color = "#22211d"),
  axis.line = element_blank(),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks = element_blank(),
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  # panel.grid.minor = element_line(color = "#ebebe5", size = 0.2),
  panel.grid.major = element_line(color = "#ebebe5", size = 0.2),
  panel.grid.minor = element_blank(),
  plot.background = element_rect(fill = "#f5f5f2", color = NA), 
  panel.background = element_rect(fill = "#f5f5f2", color = NA), 
  legend.background = element_rect(fill = "#f5f5f2", color = NA),
  panel.border = element_blank())

wd <- "D:/R/DataViz" # top level working directory
setwd(wd)

##############################################################################################

# STATE BORDERS
# read in shape border shapefile
# data from https://github.com/jasperdebie/VisInfo/blob/master/us-state-capitals.csv
state_borders <- readOGR(paste(wd, "/shapefiles/stateBorders/statesp010g.shp", sep=""))
state_borders <- spTransform(state_borders, CRS("+proj=utm +zone=18 +datum=NAD83 +units=m+no_defs"))
# below code fixes some bad polygons in this shapefile
# simplify the polgons a tad (tweak 0.00001 to your liking)
state_borders <- gSimplify(state_borders, tol = 0.00001)
# this is a well known R / GEOS hack (usually combined with the above) to 
# deal with "bad" polygons
state_borders <- gBuffer(state_borders, byid=TRUE, width=0)
# any bad polys?
sum(gIsValid(state_borders, byid=TRUE)==FALSE)

##############################################################################################

#Albemarle Sound Bathy
# READ IN AND PARSE BATHYMETRY DATA
# from https://www.ngdc.noaa.gov/thredds/catalog/regional/catalog.html?dataset=regionalDatasetScan/albemarle_sound_s010_30m.nc

ABSound <- raster('albemarle_sound_s010_30m.nc')
ABSound <- as.data.frame(ABSound, xy = TRUE)
extents <- extent(ABSound)

#############################################################################################

###Set up for plotting

ymax <- 4051522
extents@ymax <- ymax
ymin <- 3945472
extents@ymin <- ymin
xmax <- 445624.1
extents@xmax <-  xmax
xmin <- 325264.1
extents@xmin <- xmin

state_borders_sf <- st_as_sf(state_borders)
state_borders_sf_crop <- st_crop(state_borders_sf, xmin = xmin, xmax = xmax,
                             ymin = ymin, ymax = ymax)



ggplot() +
  geom_raster(aes(x=x, y=y, fill = GDAL.Band.Number.1), data = ABSound) +
  ylab("") +
  xlab("") +
  geom_sf(data=state_borders_sf_crop, fill="black", color=NA) +
  geom_sf(data=state_borders_sf, size=0.2, fill=NA, color=alpha("white",0.2) ) +
  coord_sf(expand = FALSE, xlim=c(xmin, xmax), ylim=c(ymin, ymax)) +
  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),
        legend.position = "none") +
  coord_equal()

正如評論中所發布的,解決方案是簡單地刪除 ggplot 代碼末尾的 coord_equal() 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM