updated chapters

This commit is contained in:
Jeremy Kidwell 2023-10-12 14:16:02 +01:00
parent f8735b6edf
commit 3a657d6b2f
8 changed files with 217 additions and 269 deletions

View file

@ -9,7 +9,7 @@ Let's start by importing some data into R. Because R is what is called an object
In the example below, we're going to read in data from a comma separated value file ("csv") which has rows of information on separate lines in a text file with each column separated by a comma. This is one of the standard plain text file formats. R has a function you can use to import this efficiently called "read.csv". Each line of code in R usually starts with the object, and then follows with instructions on what we're going to put inside it, where that comes from, and how to format it:
```{r}
```{r, results = 'hide'}
#| include: true
#| label: fig-polar
setwd("/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion")
@ -92,8 +92,8 @@ barplot(height=df$value, names=df$key)
### GGPlot
```{r}
ggplot(uk_census_2021_religion_wmids, aes(x = key, y = value)) + # <1>
geom_bar(stat = "identity") # <1>
ggplot(uk_census_2021_religion_wmids, aes(x = key, y = value)) + # <1>
geom_bar(stat = "identity") # <1>
ggplot(uk_census_2021_religion_wmids, aes(x= reorder(key,-value),value)) + geom_bar(stat ="identity") # <2>
```
@ -215,7 +215,6 @@ If you want to draw some data from the nomis platform yourself in R, have a look
```{r}
# Get table of Census 2011 religion data from nomis
# Note: for reproducible code used to generate the dataset used in the book, see the cookbook here:
z <- readRDS(file = (here("example_data", "z.rds")))
# Filter down to simplified dataset with England / Wales and percentages without totals

View file

@ -10,12 +10,13 @@ We've decided to open up access to our data and I'm highlighting it in this book
# Loading in some data
```{r}
```{r, results = 'hide'}
# R Setup -----------------------------------------------------------------
setwd("/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion")
library(here)
library(tidyverse)
library(haven) # used for importing SPSS .sav files
library(here) |> suppressPackageStartupMessages()
library(tidyverse) |> suppressPackageStartupMessages()
# used for importing SPSS .sav files
library(haven) |> suppressPackageStartupMessages()
here::i_am("chapter_2.qmd")
climate_experience_data <- read_sav(here("example_data", "climate_experience_data.sav"))
```

View file

@ -21,7 +21,7 @@ Now that you have a sense of some of the basic aspects of geospatial data, let's
A good starting point is to aquire some adminstrative data. This is a way of referring to political boundaries, whether country borders or those of a local authority or some other administrative unit. For our purposes, we're going to import several different types of administrative boundary which will be used at different points in our visualisations below. It's worth noting that the data we use here was prepared to support the 2011 census, and make use of the shapefile format.
```{R}
```{r, results = 'hide'}
library(sf) |> suppressPackageStartupMessages()
library(here) |> suppressPackageStartupMessages()
library(tidyverse)
@ -36,46 +36,43 @@ if (file.exists(here("data", "infuse_uk_2011_clipped.shp")) == FALSE) {
download.file("https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_uk_2011_clipped.zip", destfile = "data/infuse_uk_2011_clipped.zip")
unzip("data/infuse_uk_2011_clipped.zip", exdir = "data")
}
uk_countries <- st_read(here("data", "infuse_uk_2011_clipped.shp"))
uk_countries <- st_read(here("data", "infuse_uk_2011_clipped.shp"), quiet = TRUE)
# Download administrative boundaries for whole UK at regions level
if (file.exists(here("data", "infuse_rgn_2011_clipped.shp")) == FALSE) {
download.file("https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_rgn_2011_clipped.zip", destfile = "data/infuse_rgn_2011_clipped.zip")
unzip("data/infuse_rgn_2011_clipped.zip", exdir = "data")
}
uk_rgn <- st_read(here("data", "infuse_rgn_2011_clipped.shp"))
uk_rgn <- st_read(here("data", "infuse_rgn_2011_clipped.shp"), quiet = TRUE)
# Download administrative boundaries for whole UK at local authority level
if (file.exists(here("data", "infuse_dist_lyr_2011_clipped.shp")) == FALSE) {
download.file("https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_dist_lyr_2011_clipped.zip", destfile = "data/infuse_dist_lyr_2011_clipped.zip")
unzip("data/infuse_dist_lyr_2011_clipped.zip", exdir = "data")
}
local_authorities <- st_read(here("data", "infuse_dist_lyr_2011_clipped.shp"))
local_authorities <- st_read(here("data", "infuse_dist_lyr_2011_clipped.shp"), quiet = TRUE)
# Download building outlines for whole UK
if (file.exists(here("data", "infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg")) == FALSE) {
download.file("https://zenodo.org/record/6395804/files/infuse_dist_lyr_2011_simplified_100m_buildings_overlay_simplified.gpkg", destfile = here("data", "infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg"))}
local_authorities_buildings_clip <- st_read(here("data", "infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg"))
local_authorities_buildings_clip <- st_read(here("data", "infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg"), quiet = TRUE)
```
Before we move on, let's plot a simple map and have a look at one of our administrative layers. We can use ggplot with a new type of shape `geom_sf()` to plot the contents of a geospatial data file with polygons which is loaded as a `simplefeature` in R.
```{r}
ggplot(uk_countries) +
geom_sf()
library(bench) |> suppressPackageStartupMessages()
bench_time(ggplot(uk_countries) + geom_sf())
```
# Load in Ordnance Survey OpenMap Points Data
```{r}
# Note: for more advanced reproducible scripts which demonstrate how these data surces have been
# obtained, see the companion cookbook here: https://github.com/kidwellj/hacking_religion_cookbook/blob/main/ordnance_survey.R
os_openmap_pow <- st_read(here("data", "os_openmap_pow.gpkg"))
ggplot(os_openmap_pow) +
geom_sf()
os_openmap_pow <- st_read(here("data", "os_openmap_pow.gpkg"), quiet = TRUE)
bench_time(ggplot(os_openmap_pow) + geom_sf())
```
It's worth noting that the way that you load geospatial data in R has changed quite dramatically since 2020 with the introduction of the simplefeature class in R. Much of the documentation you will come across "out there" will make reference to a set of functions which are now deprecated.
@ -87,6 +84,7 @@ Let's use that data we've just loaded to make our first map:
# using temporary palette here so that 0s are white
library(tmap) |> suppressPackageStartupMessages()
# palette <- c(white, "#a8ddb5", "#43a2ca")
map1 <- tm_shape(local_authorities) +
# tm_fill(col = "surveys_count", , palette = palette, title = "Concentration of survey respondents") +
tm_borders(alpha=.5, lwd=0.1) +
@ -116,14 +114,15 @@ map1
tmap_save(map1, here("figures", "map.png"), width=1920, height=1080, asp=0)
```
```{r}
```{r, results = 'hide'}
# subsetting ordnance survey openmap data for measuring clusters and proximity
os_openmap_important_buildings <- st_read(here("data", "os_openmap_important_buildings.gpkg"))
os_openmap_important_buildings <- st_read(here("data", "os_openmap_important_buildings.gpkg"), quiet = TRUE)
# add pubs, check_cashing, pawnbrokers, SSSI
## subsets
```
1. Count the number of churches in Local Authorities
```{r}