首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot2地图-创建掩码填充一个不包括单个国家的框

ggplot2地图-创建掩码填充一个不包括单个国家的框
EN

Stack Overflow用户
提问于 2017-05-26 20:41:06
回答 1查看 1.4K关注 0票数 2

是否有可能在of图中有一个图层作为ggmap层的掩码?这里他们在ggmap上添加了一个国家多边形。

我正在寻找的是,国家将是一个“洞”的一层(阿尔法)覆盖一切,但国家。在某种程度上,上面的例子正好相反。来自该答案的代码(添加透明度并更新为使用geom_cartogram)。

代码语言:javascript
复制
library(mapdata)
library(ggmap)
library(ggplot2)
library(ggalt)

# Get Peru map
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite") 

# This is the layer I wish to put over the top
coast_map <- fortify(map("worldHires", fill = TRUE, plot = FALSE)) 

# Subset data for Peru
peru.coast <- subset(coast_map, region == "Peru")

# Draw a graphic
ggmap(Peru) +
  geom_cartogram(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
           fill="white", color="grey", alpha=.1) +
  xlim(-86, -68) +
  ylim(-20, 0) + 
  labs(x = "Longitude", y = "Latitude") +
  coord_map() +
  theme_classic()

在ggplot2中,除了多边形之外,还有什么方法可以填充所有的东西吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-26 22:52:30

在ggplot2中,除了多边形之外,还有什么方法可以填充所有的东西吗?

这种方法可能有点不正统,但无论如何:

代码语言:javascript
复制
library(mapdata)
library(ggmap)
library(ggplot2)
library(raster)
ggmap_rast <- function(map){
  map_bbox <- attr(map, 'bb') 
  .extent <- extent(as.numeric(map_bbox[c(2,4,1,3)]))
  my_map <- raster(.extent, nrow= nrow(map), ncol = ncol(map))
  rgb_cols <- setNames(as.data.frame(t(col2rgb(map))), c('red','green','blue'))
  red <- my_map
  values(red) <- rgb_cols[['red']]
  green <- my_map
  values(green) <- rgb_cols[['green']]
  blue <- my_map
  values(blue) <- rgb_cols[['blue']]
  stack(red,green,blue)
}
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite") 
data(wrld_simpl, package = "maptools")
polygonMask <- subset(wrld_simpl, NAME=="Peru")
peru <- ggmap_rast(Peru)
peru_masked <- mask(peru, polygonMask, inverse=T)
peru_masked_df <- data.frame(rasterToPoints(peru_masked))
ggplot(peru_masked_df) + 
  geom_point(aes(x=x, y=y, col=rgb(layer.1/255, layer.2/255, layer.3/255))) + 
  scale_color_identity() + 
  coord_quickmap()

通过的问题/答案。

我要找的是透明填充层的环境和alpha=1的秘鲁

如果第一次想到这很容易。然而,后来我看到并记得geom_polygon不太喜欢有洞的多边形。幸运的是,来自包geom_polypathggpolypath做了。然而,它将抛出一个“grid.Call.graphics中的错误(L_path,x$x,x$y,index,switch(x$规则,缠绕=1L.)”ggmap的默认面板扩展出现错误。

所以你可以

代码语言:javascript
复制
library(mapdata)
library(ggmap)
library(ggplot2)
library(raster)
library(ggpolypath) ## plot polygons with holes
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite") 
data(wrld_simpl, package = "maptools")
polygonMask <- subset(wrld_simpl, NAME=="Peru")
bb <- unlist(attr(Peru, "bb"))
coords <- cbind(
  bb[c(2,2,4,4)],
  bb[c(1,3,3,1)])
sp <- SpatialPolygons(
  list(Polygons(list(Polygon(coords)), "id")), 
  proj4string = CRS(proj4string(polygonMask)))
sp_diff <- erase(sp, polygonMask)
sp_diff_df <- fortify(sp_diff)  

ggmap(Peru,extent="normal") +
  geom_polypath(
    aes(long,lat,group=group),
    sp_diff_df, 
    fill="white",
    alpha=.7
  )

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44209859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档