library(sf) # classes and functions for vector data
library(raster) # classes and functions for raster data
library(spData) # load geographic data
library(spDataLarge) # load larger geographic data
```
# R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunks
We like R Markdown. In this file we step through some very simple mapping functions in R/Rmd.
# Geo-comp
Doing maps and stuff in R.
We use https://geocompr.robinlovelace.net/ a lot...
# Explore 'World' maps
First try using the `sf` [package](https://cran.r-project.org/web/packages/sf/index.html)'s built-in `world` data to make a simple map plot.
It's worth remembering that a map is just a plot with spatial arrangements...
```{r world_maps}
head(world)
# plot variables 3 to 6
plot(world[3:6])
# plot world population
plot(world["pop"])
```
Now re-draw the population map using `ggplot2` and the `geom_sf` geometry... This will make a much [prettier map](https://www.johan-rosa.com/2019/11/13/creating-maps-with-sf-and-ggplot2/) because `ggplot2` is so cool.
# "Havant","New Forest","Hart","Basingstoke and Deane")
# we pre-saved this data in the data folder of the repo for speed
# see getBoundaryData.R for how it works
inf <- here::here("data", "boundaries", "la_solent.shp") # use here to specify the data location
message("Loading LA geometry from ONS Open Geography API")
la_sf_data <- sf::read_sf(inf)
head(la_sf_data)
```
Which areas have we got?
```{r boundaryLAAreas}
table(la_sf_data$lad18nm)
```
## Mapping data
Now we'll map/plot some of the data using the `ggplot2` approach. To do that we need to merge the boundaries and the energy data so that we can `fill` the boundaries with a colour according to one of the variables.
```{r plotLAElec}
# create a variable with the LA code and the same name as in the sf_data
la_elecData$lad18cd <- la_elecData$`LA Code`
# merge them
la_merged_sf <- merge(la_sf_data, la_elecData)
# plot
ggplot2::ggplot(la_merged_sf) +
geom_sf(aes(fill = Total_GWh)) +
scale_fill_continuous(name = "Total GWh", low = "green", high = "red")
```
# MSOAs
These are census areas - smaller than LSOAs so the files are bigger and the maps are denser.
## Loading data
As before we're going to load some polygon boundary data (you need internet access for this) and some energy demand data so we can link them and map them.
```{r loadElecMSOAData}
# electricity consumption data at MSOA level (pre downloaded)
# "Havant","New Forest","Hart","Basingstoke and Deane")
# we pre-saved this data in the data folder of the repo for speed
# see getBoundaryData.R for how it works
inf <- here::here("data", "boundaries", "msoa_solent.shp") # use here to specify the data location
message("Loading MSOA geometry from ONS Open Geography API")
msoa_sf_data <- sf::read_sf(inf)
head(msoa_sf_data)
```
Which areas have we got and how many MSOAs are there in each?
```{r boundaryMSOAAreas}
table(msoa_sf_data$LAD11NM)
```
## Mapping data
Now we'll map/plot some of the data using the `ggplot2` approach. To do that we need to merge the boundaries and the energy data so that we can `fill` the boundaries with a colour according to one of the variables.
```{r plotMSOAElec}
# create a variable with the LA code and the same name as in the sf_data
msoa_elecData$MSOA11CD <- msoa_elecData$`Middle Layer Super Output Area (MSOA) Code`