简体   繁体   中英

R: Remove island in a tmap map

I am trying of remove islands using tmap.

Frequently I use lims to remove islands in ggplot:

library(tmap)
library(ggplot2)
library(raster)

chile  <- getData("GADM",country="Chile",level=1)

ggplot()+
    geom_polygon(data=chile, aes(x=long, y=lat, group = group),
         color = "#666666") +
         coord_equal() + 
         lims(x = c( -77.2,-60), y = c(-60, -15))

ggplot Chile map

But tmap dont allow use lims and island disturb the map from continent:

tm_shape(chile) +
tm_borders()   

tmap Chile with islands

I use facet with tmap because it give better results to me but islands its a problem to me:

    chile2= chile[c(2,4:5,7,8,12:16),]
    chile2$group=1
    chile3= chile[c(1,3,6,9:11),]
    chile3$group=2
    map_split=rbind(chile2, chile3)

    tm_shape(map_split) +
    tm_borders() +
    tm_facets(by = "group")

tmap Chile facet

You can see how islands dont allow get a good facet map.

Do you know how remove islands in a tmap map?

Thanks

You can remove the areas you do not want from the dataset.

library(raster)
chile  <- getData("GADM",country="Chile",level=1)
xchile <- crop(chile, extent(-85,-59, -61, -14))

It takes a while to run as chile is a rather complex dataset because it has so many islands

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM