Author

Steve Nolan

1 MSOA map from census dara

The idea here is to make an interactive map to gain a greater understanding of the spread of ethnic minorities in the wider region.

2 Preamble

Code
# load libraries
library(ggplot2)
library(dplyr)
library(nomisr)
library(kableExtra)
library(sf)
library(raster)
library(leaflet)
library(geojsonio)
library(readxl)
library(tmap)
library(RColorBrewer)
library(rmapshaper)

3 Census data

We are using the 2021 UK Census data for the breakdown of ethnic population.

Code
#Import census data
census = read.csv('/Users/stevenolan/Library/CloudStorage/OneDrive-LiverpoolJohnMooresUniversity/projects/social_value/nomis/data/census/census_ethnic_sex_lsoa.csv')



# rename columns
census <- census %>% 
  rename(
    "lsoa_code" ="Lower.layer.Super.Output.Areas.Code"  ,
    "lsoa_area"= "Lower.layer.Super.Output.Areas" ,
    "eth_code" = "Ethnic.group..20.categories..Code",
    "eth_group" = "Ethnic.group..20.categories." ,
    "sex_code" = "Sex..2.categories..Code" ,
    "sex" = "Sex..2.categories.",
    "count" = "Observation" 
    )

We need to sum up the males and females for each ethnic group and for each MSOA so that we can calculate percentages.

Code
# Sum male and female by MSOA
census <- census %>%
  group_by(lsoa_code, eth_code) %>%
  mutate(count_lsoa_eth = sum(count)) 

# Sum as male and female in MSOA
census <- census %>%
  group_by(lsoa_code, sex_code) %>%
  mutate(count_lsoa_gender = sum(count)) 

# Sum all people in MSOA
census <- census %>%
  group_by(lsoa_code) %>%
  mutate(count_lsoa_all = sum(count)) 

# Percentage of ethnic group in MSOA
 census <- census %>%
  mutate(per_lsoa = (count_lsoa_eth/count_lsoa_all)*100) 
 
 # Percentage of ethnic group in MSOA by gender
 census <- census %>%
  mutate(per_lsoa_gen = (count/count_lsoa_gender)*100) 

4 Map

Now we need to bring in the shapefile and geoJSON file. Both are optained for the LSOA from the Open Geography Portal..

Code
# import shapefile 
lsoa_map <- st_read("/Users/stevenolan/Library/CloudStorage/OneDrive-LiverpoolJohnMooresUniversity/projects/social_value/nomis/data/shapefiles/Lower_layer_Super_Output_Areas_2021_EW_BFE_V9_3524696818178975005/LSOA_2021_EW_BFE_V9.shp")  
Reading layer `LSOA_2021_EW_BFE_V9' from data source 
  `/Users/stevenolan/Library/CloudStorage/OneDrive-LiverpoolJohnMooresUniversity/projects/social_value/nomis/data/shapefiles/Lower_layer_Super_Output_Areas_2021_EW_BFE_V9_3524696818178975005/LSOA_2021_EW_BFE_V9.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 35672 features and 7 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 82643.12 ymin: 5333.81 xmax: 655989 ymax: 657600.4
Projected CRS: OSGB36 / British National Grid
Code
# Limit census data to Whites only. 
census_white <- census %>%
  filter(eth_code == 13 & sex_code == 1)

# Merge data
merged_data <- merge(lsoa_map, census_white, by.x = "LSOA21CD", by.y = "lsoa_code", all.x = TRUE)

# Keep only Wales and Northwest
merged_data <- na.omit(merged_data)

# Plot heatmap
ggplot() +
  geom_sf(data = merged_data, aes(fill = per_lsoa)) +
  scale_fill_viridis_c(name=" ") +  # Adjust colors as needed
  labs(title = "% Whites by Parliamentary Constituency") +
  theme_minimal()