Plotting UK Maps with ggplot2 v0.0.4
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Because of delays with my scholarship payment, if this post is useful to you I kindly ask a minimal donation on Buy Me a Coffee that shall be used to continue my Open Source efforts. If you need an R package or Shiny dashboard for your team, you can email me or inquiry on Fiverr. The full explanation is here: A Personal Message from an Open Source Contributor
You can send me questions for the blog using this form.
Installation
You can install the development version of ukmaps like so:
remotes::install_github("pachadotdev/ukmaps")
Examples
Yes/No map of London administrative areas:
library(ukmaps) library(dplyr) library(ggplot2) d <- boundaries %>% mutate( region_name = if_else(is.na(region_name), "Notr Available", region_name), is_london = if_else(region_name == "London", "Yes", "No") ) pal <- c("#165976", "#d04e66") ggplot(d) + geom_sf(aes(fill = is_london, geometry = geometry), color = "white", linewidth = 0) + scale_fill_manual(values = pal, name = "Is this London?") + labs(title = "Map of England with Administrative Boundaries") + theme_minimal(base_size = 13)
Which part of London is Barnet?
d <- boundaries %>% filter(region_name == "London") %>% mutate(is_barnet = if_else(lad_name == "Barnet", "Yes", "No")) pal <- c("#165976", "#d04e66") ggplot(d) + geom_sf(aes(fill = is_barnet, geometry = geometry), color = "white") + scale_fill_manual(values = pal, name = "Is this Barnet?") + labs(title = "Which part of London is Barnet?") + theme_minimal(base_size = 13)
Which part of London is Golders Green?
d <- boundaries %>% filter(region_name == "London") %>% mutate( is_golders_green = if_else(ward_name == "Golders Green", "Yes", "No") ) pal <- c("#165976", "#d04e66") ggplot(d) + geom_sf(aes(fill = is_golders_green, geometry = geometry), color = "white") + scale_fill_manual(values = pal, name = "Is this Golders Green?") + labs(title = "Which part of London is Golders Green?") + theme_minimal(base_size = 13)
The following maps use functions that aggregate the dataset to keep the package size small.
Country level map of the UK:
pal <- c("#165976", "#365158", "#d04e66", "#ffd613") # country() aggregates the map to country level ggplot(country()) + geom_sf(aes(fill = country_name, geometry = geometry), color = "white") + scale_fill_manual(values = pal, name = "Country") + labs(title = "Map of England with Country Boundaries") + theme_minimal(base_size = 13)
How many R’s in each county name?
# number of R's in county names d <- counties() %>% mutate(n = stringr::str_count(county_name, "[rR]")) # region() aggregates the map to country level ggplot(d) + geom_sf(aes(fill = n, geometry = geometry), color = "white") + scale_fill_gradient(low = "#165976", high = "#d04e66", name = "R's", breaks = seq(0, max(d$n), by = 1)) + labs(title = "How many R's in each county name?") + theme_minimal(base_size = 13)
How many R’s in each LAD name? Local Authority Districts (LAD) (Local Government District (LGD) in Northern Ireland)
d <- lads() %>% mutate(n = stringr::str_count(lad_name, "[rR]")) ggplot(d) + geom_sf(aes(fill = n, geometry = geometry), color = "white") + scale_fill_gradient(low = "#165976", high = "#d04e66", name = "R's", breaks = seq(0, max(d$n), by = 1)) + labs(title = "How many R's in each LAD name?") + theme_minimal(base_size = 13)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.