SpatialPolygonsDataFrame doesn't plot properly when I make the color transparent

孤人 提交于 2019-12-06 09:42:06

First, I don't know the reason of the problem. The fact that it works for regular colours but not for colours with alpha channel, seems strange to me.

Second, here is an example of a work-around using gIntersection of the rgeos package to clip the polygons to the size of interest, i.e. the xlim and ylim. Plotting the clipped map seems to work for alpha colours.

library(rgdal)
library(rgeos)  # for gIntersection
library(raster) # for extent()

# download world map
download.file('http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip', {f <- tempfile()}); 
unzip(f, exdir=tempdir()); 
shp <- readOGR(tempdir(), 'ne_110m_admin_0_countries'); 

# define map limits
xlim = c(0, 90)
ylim = c(40, 60)

# red with alpha
mycol = rgb(1,0,0,0.5)

# plot world map
plot(shp)

# create rectangle from xlim and ylim
CP <- as(extent(c(xlim,ylim)), "SpatialPolygons")
proj4string(CP) <- proj4string(shp)

# add to plot
plot(CP, add=T, col=rgb(1,0,0,0.5))

# set up window for 2 plots, comparing 2 methods
par(mfrow=c(2,1))

# plot map with xlim and ylim: does not work the way we want
plot(shp, xlim=xlim, ylim=ylim, col=mycol, axes=T, las=1)

# clip map to xlim and ylim, then plot: works
out <- gIntersection(shp, CP, byid=TRUE)
plot(out, col=mycol, axes=T, las=1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!