Created
July 4, 2018 09:31
-
-
Save O1sims/61afad8f6629c572b72b0d8e650dac47 to your computer and use it in GitHub Desktop.
Load up NI shapefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
library(ggplot2) | |
library(rgdal) | |
# Input `mapType` can either be "parliamentary" or "county" | |
getNIPopulationMap <- function(mapType = "parliamentary") { | |
normaliseVector <- function(x) { | |
normalisation <- (x-min(x))/(max(x)-min(x)) | |
return(normalisation * 100) | |
} | |
mapDataLocation <- paste0("NI-", mapType, "-boundaries") | |
# Load NI shapefile into R | |
NIShapefile <- rgdal::readOGR( | |
dsn = getwd() %>% paste0( | |
"/data/ODNI/maps/", mapType, "/", mapDataLocation, ".shp"), | |
layer = mapDataLocation) | |
# use the NAME_2 field to create data frame | |
NIMap <- ggplot2::fortify( | |
model = NIShapefile) | |
# Create population data | |
NIMap$count <- normaliseVector(rexp( | |
n = NIMap %>% nrow(), | |
rate = 1)) | |
# Plot NI map | |
mapPlot <- ggplot( | |
data = NIMap, | |
aes(x = long, y = lat, | |
group = group, fill = count)) + | |
geom_polygon( | |
colour = "white", | |
size = 0.5, | |
aes(group = group)) + | |
scale_fill_gradient( | |
low = "firebrick1", | |
high = "steelblue") + | |
ggtitle( | |
"Projected population change, Northern Ireland, 2016-2041") + | |
theme( | |
axis.text = element_blank(), | |
axis.ticks = element_blank(), | |
panel.border = element_blank(), | |
panel.background = element_blank(), | |
panel.grid.major = element_blank(), | |
panel.grid.minor = element_blank(), | |
plot.title = element_text( | |
family = "Arial"), | |
legend.position = "none") | |
# Save map | |
ggsave(filename = getwd() %>% | |
paste0('/figures/raw/populationMap.png'), | |
plot = mapPlot) | |
return(mapPlot) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment