2017-05-26 4 views
2

ggplotにggmapレイヤーのマスクとして機能するレイヤーを持たせることはできますか? Here ggmapの上に国のポリゴンを追加しました。map with 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

を行うことができます – Nancy

+0

''? '' ''サブセット(coast_map、地域!= "ペルー")を使用してperu.coast変数を置き換えることができるためのおかげで提案はありますが、あなたはworldHiresのすべてのデータを取りますが、これは海をカバーしません。回避策は、すべての隣人と海洋を選択することですnシェイプファイルが、複雑になる可能性があります。私は本当に補完または逆のアプローチを探しています。 – 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

経由thisthis、およびthis質問/回答。私が探しています何


まず、これは簡単であると思った場合= 1

アルファと透明の塗りつぶし 層とペルーとその周辺です。しかし、その後、私は見て、geom_polygonは非常に穴のあるポリゴンが好きではないことを思い出しました。幸いにも、geom_polypathパッケージggpolypathからします。しかし、ggmapのデフォルトパネル拡張では、 "grid.Call.graphics(L_path、x $ x、x $ y、index、switch(x $ rule、winding = 1L ..)"エラーが発生します。

ですから、

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

関連する問題