如果您经常使用shapefile,geom_path和geom_polygon会提供您需要的一切。在最近的版本中,ggplot直接处理空间对象,因此不需要使用强化和合并(可能是在代码中花费更多时间的步骤)。这是一个使用的例子 来自IBGE的巴西联邦单位的形状文件 作为基本地图:
shapeUFs <- readOGR('.', 'BRUFE250GC_SIR') shapeHid <- readOGR('.', 'PrincipaisRiosDoBrasil') ggplot(shapeUFs, aes(long, lat, group = group)) + geom_polygon(fill = 'gray90', color = 'black') + geom_path(data = shapeHid, color = 'steelblue2') + coord_map() + theme_void()
性能将受到形状大小(由特征数量和细节级别决定)的影响,而不是您在ggplot中使用的几何形状。您可以使用rgeos :: gSimplify来减少空间多边形/线对象中的顶点数。您还可以直接在地图上绘制点:
# Simplifying the geometry of the federative units shapeUFs.s <- rgeos::gSimplify(shapeUFs, .05, TRUE) # Storing map in an object riversMap <- ggplot(shapeUFs.s, aes(long, lat)) + geom_polygon(aes(group = group), fill = 'gray90', color = 'black') + geom_path(data = shapeHid, aes(group = group), color = 'steelblue2') + coord_map() + theme_void() # Sampling 20 cities in Brazil brMunics <- read.csv('https://raw.githubusercontent.com/kelvins/Municipios-Brasileiros/master/Municipios_Brasileiros.csv') Munics <- brMunics[sample(nrow(brMunics), 20), ] # Plotting points over the map riversMap + geom_point(data = Munics, aes(Longitude, Latitude), color = 'red') # If your data already have the coordinates named 'lat' and 'long', # you can skip aes(Longitude, Latitude): names(Munics)[6:7] <- c('lat','long') riversMap + geom_point(data = Munics, color = 'red')
我会做以下事情:
library(sf) library(ggplot2) world_map <- map_data('world') sdf <- read_sf("PrincipaisRiosDoBrasil.shp") myMap3 <- ggplot() + geom_map(data = world_map, map = world_map, aes(map_id = region), color = 'black', fill = NA, linetype=2) + geom_sf(data = sdf)+ theme(panel.border = element_rect(fill = NA, colour = "black"))+ theme(axis.title=element_blank())+ scale_y_continuous(limits=c(-15,6),expand=c(0,0))+ scale_x_continuous(limits=c(-76,-55),expand=c(0,0)) myMap3
您需要将ggplot2更新为3.0.0 geom_sf 。
geom_sf