Skip to content

Instantly share code, notes, and snippets.

@O1sims
Created July 4, 2018 09:31
Show Gist options
  • Save O1sims/61afad8f6629c572b72b0d8e650dac47 to your computer and use it in GitHub Desktop.
Save O1sims/61afad8f6629c572b72b0d8e650dac47 to your computer and use it in GitHub Desktop.
Load up NI shapefile
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