在ggplot2中的多层shapefile映射上显示多边形内的多边形

时间:2018-05-25 03:43:18

标签: r ggplot2 shapefile

我想使用多层彼此叠加的shapefile。我有三个深度水平的测深图(即200米,1000米和2000米)。在多边形中有一些“洞”(*不是白色多边形),你可以在我在QGIS中生成的地图中看到(即在200米范围内有2000米深度的多边形):

Bathymetry map created by QGIS

这是我在ggplot2中使用相同的shapefile制作的: Bathymetry map created by ggplot2

ggplot地图上未显示多边形内的多边形。我怎样才能解决这个问题呢?

提前致谢。

用于地图的R脚本是:

Thailand <- readShapePoly("Thailand.shp")
Thailand2 <- fortify(Thailand)
Bthy_200m <- readShapePoly("ne_10m_bathymetry_K_200.shp")
Bthy_1000m <- readShapePoly("ne_10m_bathymetry_all/ne_10m_bathymetry_J_1000.shp")
Bthy_2000m <- readShapePoly("ne_10m_bathymetry_all/ne_10m_bathymetry_I_2000.shp")

Bthy_200m_crop <- crop(Bthy_200m, extent(84.11236, 108.4594, -4.046979, 24.09534))
Bthy_1000m_crop <- crop(Bthy_1000m, extent(84.11236, 108.4594, -4.046979, 24.09534))
Bthy_2000m_crop <- crop(Bthy_2000m, extent(84.11236, 108.4594, -4.046979, 24.09534))

Bthy_200m_crop2<- fortify(Bthy_200m_crop)
Bthy_1000m_crop2<- fortify(Bthy_1000m_crop)
Bthy_2000m_crop2<- fortify(Bthy_2000m_crop)

ggplot()+geom_polygon(data = Bthy_200m_crop2, aes(long, lat, group = group), fill="#52958b", na.rm =TRUE)+geom_polygon(data = Bthy_1000m_crop2, aes(long, lat, group = group), fill="#128277", na.rm =TRUE)+geom_polygon(data = Bthy_2000m_crop2, aes(long, lat, group = group), fill="#004D47", na.rm =TRUE)+geom_polygon(data = est_contour, aes(long, lat, group = group), fill="#99CCCC")+  geom_path(data = est_contour, aes(long, lat, group = group), color = "black")+theme_bw()+theme(panel.background = element_rect(fill = "#5EA8A7"))+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

形状文件可在此处找到:http://www.naturalearthdata.com/downloads/10m-physical-vectors/10m-bathymetry/

1 个答案:

答案 0 :(得分:1)

所以我下载了一些提到的文件。要查看多边形内的多边形,我建议使用geom_sf。这里有一些代码以及输出图片,以确保它是您需要的。这是使用geom_sf生成它的代码。注意,我没有使用泰国,因为我找不到shapefile,也没有使用河口shapefile,但你应该能够将这个代码应用到情节

library(sf)
library(tidyverse)
library(maptools)
library(rgdal)
library(raster)

Bthy_200m <- read_sf("ne_10m_bathymetry_K_200.shp")
Bthy_1000m <- read_sf("ne_10m_bathymetry_J_1000.shp")
Bthy_2000m <- read_sf("ne_10m_bathymetry_I_2000.shp")


Bthy_200m_crop <- st_intersection(Bthy_200m,st_set_crs(st_as_sf(as(extent(84.11236, 108.4594, -4.046979, 24.09534),"SpatialPolygons")), st_crs(Bthy_200m)))
Bthy_1000m_crop <- st_intersection(Bthy_1000m, st_set_crs(st_as_sf(as(extent(84.11236, 108.4594, -4.046979, 24.09534),"SpatialPolygons")), st_crs(Bthy_1000m)))
Bthy_2000m_crop <- st_intersection(Bthy_2000m, st_set_crs(st_as_sf(as(extent(84.11236, 108.4594, -4.046979, 24.09534),"SpatialPolygons")), st_crs(Bthy_2000m)))


Bthy_200m_crop2<- fortify(Bthy_200m_crop)
Bthy_1000m_crop2<- fortify(Bthy_1000m_crop)
Bthy_2000m_crop2<- fortify(Bthy_2000m_crop)
ggplot()+
  geom_sf(data = Bthy_200m_crop2,fill = "#52958b",na.rm = TRUE)+
  geom_sf(data = Bthy_1000m_crop2, fill = "#128277",na.rm = TRUE)+
  geom_sf(data = Bthy_2000m_crop2, fill="#004D47", na.rm =TRUE)+
  coord_sf(crs = st_crs(Bthy_1000m_crop2), datum = NA)+
  theme_bw()+
  theme(panel.background = element_rect(fill = "#5EA8A7"))+ 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

enter image description here