0

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)
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
GregRousell
  • 997
  • 2
  • 13
  • 23
  • It's going to be difficult to show you this without your shapefiles, but try to follow along with http://stackoverflow.com/questions/29624895/how-to-add-a-hole-to-a-polygon-within-a-spatialpolygonsdataframe to cut a hole out for the area in question then use something like `gUnaryUnion` to dissolve the polys to be shaded. – hrbrmstr Aug 28 '15 at 18:51

1 Answers1

0

I have figured out a way by adding colour codes to the data file from this question: R - stuck with plot() - Colouring shapefile polygons based upon a slot value

schoolBound@data$filter <- NA
schoolBound@@data$filter[bound@data$Code == SCH] <- 1
schoolBound@@data$filter[bound@data$Code != SCH] <- 0

schoolBound@data$COLOUR <- "#FFFFFF"
schoolBound@@data$COLOUR[(as.numeric(as.character(bound@data$filter)) %% 10) == 0] <- "white"
schoolBound@@data$COLOUR[(as.numeric(as.character(bound@data$filter)) %% 10) == 1] <- NA

## Add layers to plot
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(schoolBound, add = TRUE, col = schoolBound@data$COLOUR)
Community
  • 1
  • 1
GregRousell
  • 997
  • 2
  • 13
  • 23