2017-05-26 21 views
2

是否有可能在ggplot中有一个图层作为ggmap图层的掩码? Here他们在ggmap顶部添加了一个国家多边形。与ggplot2的地图 - 创建一个框,填写一个框,不包括一个国家

enter image description here

我所寻找的是,该国将在一个层(阿尔法)一个“洞”涵盖一切,但该国。以上述示例的反面的方式。该答案的代码(透明度添加并更新为使用geom_cartogram)。

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中的多边形之外的所有内容?

+0

难道你'''子取代你peru.coast变量(coast_map,区域!= “秘鲁”)'''? – Nancy

+0

感谢建议但不是,你会将所有数据都记录在worldHires中,但这不包括海洋。解决方法可能是选择所有邻居和海洋n shapefile,但这很容易变得复杂。我真的在寻找一种补充或反向的方法。 – Lod

+0

我不明白这里的结果应该是什么样子 - 你是否想在中间出现一个洞(即秘鲁被裁掉了)? – lukeA

回答

1

有没有办法填补除ggplot2中的多边形之外的所有内容?

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

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() 

enter image description here

通过thisthisthis问题/解答。


我所寻找的是一个透明填充 层和秘鲁用α= 1

如果首先想到这是很容易的环境。然而,然后我看到并记住geom_polygon不喜欢带孔的多边形。幸运的是,geom_polypath从包ggpolypath呢。但是,它会在grid.Call.graphics(L_path,x $ x,x $ y,index,switch(x $ rule,winding = 1L ...。)错误ggmap默认面板扩展中抛出“错误。

所以,你可以做

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 
) 

enter image description here

+0

非常感谢,我会仔细看看代码和参考。可能我的问题会更加准确,问是否可以将矢量蒙版定义为画布边界的倒数减去多边形。 – Lod

+0

@Lod也许这是因为我在营销工作,但我会问一些像“如何填充所有东西没有多边形在中间”。我不明白诸如“矢量蒙版”或“画布边界反转”之类的东西。只要在心中写下一个孩子,而不是教授。 ;-) – lukeA

相关问题