I'm creating maps for individual schools within a district and want to create a shaded area around the school boundary. I'm able to do it manually using QGIS however would like to be able to produce something similar in R. I'm at the point where I can map some demographic variables at the neighbourhood level, add the school location and boundary. There's a loop that will automatically create a new map for each school and insert it into a knitr report.
What I can't figure out is how to create a layer that will shade the area outside of the boundary. This is my map using R:
however would like to make it look more like this:
I've tried the add.masking
function from GISTools however it's based on kernel density of points, not polygons.
My code looks like this (apologies for not being reproducible as they're shape files sitting on a secure drive.
# Load Shape files
proj<- CRS("+init=epsg:32617")
schoolBound <- readShapePoly("J:/GIS/Data/Catchments/ElementaryAreas.shp", proj4string= proj)
schools <- readShapePoints("J:/GIS/Data/Schools/ElementarySchools.shp", proj4string= proj)
roads <- readShapePoints("J:/GIS/Data/Roads/MajorRoads.shp")
da <- readShapePoly("J:/GIS/Data/Dissemination Areas 2011/da2011.shp",
proj4string = proj)
SCH <- "School A"
# Subset shape files for area around school
school1Bound <- schoolBoundR [schoolBoundR$SchoolName == paste(SCH),]
school1BoundBuff <- gBuffer(school1Bound, width = 15) # Adds buffer around geometry
school1Point <- schoolsR [schoolsR$Name == paste(SCH),]
roadsBound <- roadsR[apply(gIntersects(roadsR, school1BoundBuff, byid = TRUE),2,any),]
# Plot map and add layers
plot(school1BoundBuff)
plot(da,
add = TRUE,
col=colours[findInterval(da$sri, brks,all.inside=TRUE)], # Adds colour palette to Social Risk Index
axes=F)
plot(school1Point, add = TRUE, pch = 15, col = "blue")
plot(roadsBound, add = TRUE, col = "gray60")
plot(school1Bound, add = TRUE, lwd = 5)