library(tidyverse)
library(sf)
library(sp)
library(spdep)
library(rgdal) # Bindings for the Geospatial Data Abstraction Library
library(rgeos) # Interface to Geometry Engine - Open Source
library(tmap)
library(tmaptools)
library(spgwr)
library(grid)
library(gridExtra)
library(leaflet)
options(prompt="R> ", digits=4, scipen=999)
<- read.csv("practicaldata.csv") Census.Data
glimpse(Census.Data)
## Rows: 749
## Columns: 5
## $ OA <chr> "E00004120", "E00004121", "E00004122", "E00004123", "E00…
## $ White_British <dbl> 42.36, 47.20, 40.68, 49.66, 51.14, 41.42, 48.54, 48.68, …
## $ Low_Occupancy <dbl> 6.2937, 5.9322, 2.9126, 0.9259, 2.0000, 3.9326, 5.5556, …
## $ Unemployed <dbl> 1.8939, 2.6882, 1.2121, 2.8037, 3.8168, 3.8462, 4.5455, …
## $ Qualification <dbl> 73.63, 69.90, 67.58, 60.78, 65.99, 74.21, 62.45, 60.35, …
Data comprise the files: Camden_oa11.dbf Camden_oa11.prj Camden_oa11.shp Camden_oa11.shx
<- readOGR(".", "Camden_oa11") Output.Areas
## Warning in OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS
## = dumpSRS, : Discarded datum Ordnance_Survey_of_Great_Britain_1936 in Proj4
## definition: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000
## +y_0=-100000 +ellps=airy +units=m +no_defs
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/admin/Desktop/Linh Data Studio/Spatial Data/Spatial Modelling", layer: "Camden_oa11"
## with 749 features
## It has 1 fields
# plots the shapefile
plot(Output.Areas)
# joins data to the shapefile
<- merge(Output.Areas, Census.Data, by.x="OA11CD", by.y="OA") OA.Census
# sets the coordinate system to the British National Grid
proj4string(OA.Census) <- CRS("+init=EPSG:27700")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj
## = prefer_proj): Discarded datum Ordnance_Survey_of_Great_Britain_1936 in Proj4
## definition
## Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
## https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html
# create a map with a legend quickly you can use the qtm() function
qtm(OA.Census, fill = "Qualification") # Qualification: a variable of data
Creating maps in tmap involves you binding together several functions that comprise different aspects of the graphic. For instance:
polygon + polygon’s symbology + borders + layout
tm_shape(OA.Census) +
tm_fill("Qualification")
library(RColorBrewer)
display.brewer.all()
tm_shape(OA.Census) + tm_fill("Qualification", palette = "-Greens")
# (-Green <-- invert the order of the colour ramp)
tm_shape(OA.Census) + tm_fill("Qualification", palette = "Greens")
# (-Green <-- invert the order of the colour ramp)
“style =” followed by one of the options below.
equal - divides the range of the variable into n parts. pretty - chooses a number of breaks to fit a sequence of equally spaced ‘round’ values. So they keys for these intervals are always tidy and memorable. quantile - equal number of cases in each group jenks - looks for natural breaks in the data Cat - if the variable is categorical (i.e. not continuous data)
# changing the intervals
tm_shape(OA.Census) + tm_fill("Qualification", style = "quantile", palette = "Reds")
tm_shape(OA.Census) + tm_fill("Qualification", style = "pretty", palette = "Reds")
tm_shape(OA.Census) + tm_fill("Qualification", style = "pretty", n = 7, palette = "Reds")
tm_shape(OA.Census) + tm_fill("Qualification", style = "quantile", n = 5, palette = "Reds", legend.hist = TRUE)
## Adding borders
tm_shape(OA.Census) + tm_fill("Qualification", palette = "Reds") + tm_borders(alpha = .4)
tm_shape(OA.Census) + tm_fill("Qualification", palette = "Reds") + tm_borders(alpha = .4) +
tm_compass()
# Add layout, get rid of frame
tm_shape(OA.Census) + tm_fill("Qualification", palette = "Reds", style = "quantile", title = "% with a Qualification") +
tm_borders(alpha=.4) +
tm_compass() +
tm_layout(title = "Camden, London", legend.text.size = 1.1, legend.title.size = 1.4, legend.position = c("right", "top"), frame = FALSE)