updated files
|
@ -1,6 +1,6 @@
|
||||||
# Theology, Religious Studies, and Data Science
|
# Hacking Religion
|
||||||
|
|
||||||
An open textbook introducing data science to religious studies.
|
An open textbook introducing data science to religious studies (or vice versa)
|
||||||
|
|
||||||
You can view a live demo of the book here: [https://kidwellj.github.io/hacking_religion_textbook/intro.html]
|
You can view a live demo of the book here: [https://kidwellj.github.io/hacking_religion_textbook/intro.html]
|
||||||
|
|
||||||
|
@ -25,6 +25,10 @@ Directory structure includes:
|
||||||
3. change to the `hacking_religion` subdirectory and run `quarto preview`
|
3. change to the `hacking_religion` subdirectory and run `quarto preview`
|
||||||
4. alternatively you can render a copy of the book using `quarto render`
|
4. alternatively you can render a copy of the book using `quarto render`
|
||||||
|
|
||||||
|
# Cookbook
|
||||||
|
|
||||||
|
There is a companion repository which contains recipes which will replicate the example data used in the book. This can be found here: [https://github.com/kidwellj/hacking_religion_cookbook]
|
||||||
|
|
||||||
# Copyright
|
# Copyright
|
||||||
|
|
||||||
Content here, unless otherwise indicated are copyright by Jeremy H. Kidwell. Please re-use them as they are covered by Creative Commons Attribution 4.0 International Licence (CC BY 4.0).
|
Content here, unless otherwise indicated are copyright by Jeremy H. Kidwell. Please re-use them as they are covered by Creative Commons Attribution 4.0 International Licence (CC BY 4.0).
|
||||||
|
|
|
@ -252,37 +252,31 @@ div.csl-indent {
|
||||||
<p>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:</p>
|
<p>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:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">setwd</span>(<span class="st">"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion"</span>)</span>
|
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">setwd</span>(<span class="st">"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion"</span>)</span>
|
||||||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(here) <span class="co"># much better way to manage working paths in R across multiple instances</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(here) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
<div class="cell-output cell-output-stderr">
|
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook</code></pre>
|
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>here<span class="sc">::</span><span class="fu">i_am</span>(<span class="st">"chapter_1.qmd"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
|
||||||
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
||||||
<div class="cell-output cell-output-stderr">
|
|
||||||
<pre><code>-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
|
|
||||||
v dplyr 1.1.3 v readr 2.1.4
|
|
||||||
v forcats 1.0.0 v stringr 1.5.0
|
|
||||||
v ggplot2 3.4.3 v tibble 3.2.1
|
|
||||||
v lubridate 1.9.3 v tidyr 1.3.0
|
|
||||||
v purrr 1.0.2 </code></pre>
|
|
||||||
</div>
|
|
||||||
<div class="cell-output cell-output-stderr">
|
|
||||||
<pre><code>-- Conflicts ------------------------------------------ tidyverse_conflicts() --
|
|
||||||
x dplyr::filter() masks stats::filter()
|
|
||||||
x dplyr::lag() masks stats::lag()
|
|
||||||
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors</code></pre>
|
|
||||||
</div>
|
|
||||||
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>here<span class="sc">::</span><span class="fu">i_am</span>(<span class="st">"chapter_1.qmd"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
||||||
<div class="cell-output cell-output-stderr">
|
<div class="cell-output cell-output-stderr">
|
||||||
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion</code></pre>
|
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion</code></pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"census2021-ts030-rgn.csv"</span>)) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Set up local workspace:</span></span>
|
||||||
|
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">dir.exists</span>(<span class="st">"data"</span>) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">dir.create</span>(<span class="st">"data"</span>) </span>
|
||||||
|
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>}</span>
|
||||||
|
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">dir.exists</span>(<span class="st">"figures"</span>) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">dir.create</span>(<span class="st">"figures"</span>) </span>
|
||||||
|
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>}</span>
|
||||||
|
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">dir.exists</span>(<span class="st">"derivedData"</span>) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">dir.create</span>(<span class="st">"derivedData"</span>)</span>
|
||||||
|
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>}</span>
|
||||||
|
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"census2021-ts030-rgn.csv"</span>)) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<section id="examining-data" class="level2" data-number="2.2">
|
<section id="examining-data" class="level2" data-number="2.2">
|
||||||
<h2 data-number="2.2" class="anchored" data-anchor-id="examining-data"><span class="header-section-number">2.2</span> Examining data:</h2>
|
<h2 data-number="2.2" class="anchored" data-anchor-id="examining-data"><span class="header-section-number">2.2</span> Examining data:</h2>
|
||||||
<p>What’s in the table? You can take a quick look at either the top of the data frame, or the bottom using one of the following commands:</p>
|
<p>What’s in the table? You can take a quick look at either the top of the data frame, or the bottom using one of the following commands:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(uk_census_2021_religion)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(uk_census_2021_religion)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output cell-output-stdout">
|
<div class="cell-output cell-output-stdout">
|
||||||
<pre><code> geography total no_religion christian buddhist hindu jewish
|
<pre><code> geography total no_religion christian buddhist hindu jewish
|
||||||
1 North East 2647012 1058122 1343948 7026 10924 4389
|
1 North East 2647012 1058122 1343948 7026 10924 4389
|
||||||
|
@ -302,7 +296,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
</div>
|
</div>
|
||||||
<p>This is actually a fairly ugly table, so I’ll use an R tool called kable to give you prettier tables in the future, like this:</p>
|
<p>This is actually a fairly ugly table, so I’ll use an R tool called kable to give you prettier tables in the future, like this:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(<span class="fu">head</span>(uk_census_2021_religion))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(<span class="fu">head</span>(uk_census_2021_religion))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<table class="table table-sm table-striped small">
|
<table class="table table-sm table-striped small">
|
||||||
<colgroup>
|
<colgroup>
|
||||||
|
@ -418,7 +412,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
</div>
|
</div>
|
||||||
<p>You can see how I’ve nested the previous command inside the <code>kable</code> command. For reference, in some cases when you’re working with really complex scripts with many different libraries and functions, they may end up with functions that have the same name. You can specify the library where the function is meant to come from by preceding it with :: as we’ve done <code>knitr::</code> above. The same kind of output can be gotten using <code>tail</code>:</p>
|
<p>You can see how I’ve nested the previous command inside the <code>kable</code> command. For reference, in some cases when you’re working with really complex scripts with many different libraries and functions, they may end up with functions that have the same name. You can specify the library where the function is meant to come from by preceding it with :: as we’ve done <code>knitr::</code> above. The same kind of output can be gotten using <code>tail</code>:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(<span class="fu">tail</span>(uk_census_2021_religion))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(<span class="fu">tail</span>(uk_census_2021_religion))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<table class="table table-sm table-striped small">
|
<table class="table table-sm table-striped small">
|
||||||
<colgroup>
|
<colgroup>
|
||||||
|
@ -546,7 +540,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<p>The first thing you’re going to want to do is to take a smaller subset of a large data set, either by filtering out certain columns or rows. Now let’s say we want to just work with the data from the West Midlands, and we’d like to omit some of the columns. We can choose a specific range of columns using <code>select</code>, like this:</p>
|
<p>The first thing you’re going to want to do is to take a smaller subset of a large data set, either by filtering out certain columns or rows. Now let’s say we want to just work with the data from the West Midlands, and we’d like to omit some of the columns. We can choose a specific range of columns using <code>select</code>, like this:</p>
|
||||||
<p>You can use the <code>filter</code> command to do this. To give an example, <code>filter</code> can pick a single row in the following way:</p>
|
<p>You can use the <code>filter</code> command to do this. To give an example, <code>filter</code> can pick a single row in the following way:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion <span class="sc">%>%</span> <span class="fu">filter</span>(geography<span class="sc">==</span><span class="st">"West Midlands"</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion <span class="sc">%>%</span> <span class="fu">filter</span>(geography<span class="sc">==</span><span class="st">"West Midlands"</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>Now we’ll use select in a different way to narrow our data to specific columns that are needed (no totals!).</p>
|
<p>Now we’ll use select in a different way to narrow our data to specific columns that are needed (no totals!).</p>
|
||||||
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">Some readers will want to pause here and check out Hadley Wickham’s “R For Data Science” book, in the section, <a href="https://r4ds.hadley.nz/data-visualize#introduction">“Data visualisation”</a> to get a fuller explanation of how to explore your data.</span></div></div>
|
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">Some readers will want to pause here and check out Hadley Wickham’s “R For Data Science” book, in the section, <a href="https://r4ds.hadley.nz/data-visualize#introduction">“Data visualisation”</a> to get a fuller explanation of how to explore your data.</span></div></div>
|
||||||
|
@ -556,15 +550,15 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<h2 data-number="2.4" class="anchored" data-anchor-id="making-your-first-data-visulation-the-humble-bar-chart"><span class="header-section-number">2.4</span> Making your first data visulation: the humble bar chart</h2>
|
<h2 data-number="2.4" class="anchored" data-anchor-id="making-your-first-data-visulation-the-humble-bar-chart"><span class="header-section-number">2.4</span> Making your first data visulation: the humble bar chart</h2>
|
||||||
<p>We’ve got a nice lean set of data, so now it’s time to visualise this. We’ll start by making a pie chart:</p>
|
<p>We’ve got a nice lean set of data, so now it’s time to visualise this. We’ll start by making a pie chart:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion_wmids <span class="sc">%>%</span> <span class="fu">select</span>(no_religion<span class="sc">:</span>no_response)</span>
|
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion_wmids <span class="sc">%>%</span> <span class="fu">select</span>(no_religion<span class="sc">:</span>no_response)</span>
|
||||||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> <span class="fu">gather</span>(uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> <span class="fu">gather</span>(uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>There are two basic ways to do visualisations in R. You can work with basic functions in R, often called “base R” or you can work with an alternative library called ggplot:</p>
|
<p>There are two basic ways to do visualisations in R. You can work with basic functions in R, often called “base R” or you can work with an alternative library called ggplot:</p>
|
||||||
<section id="base-r" class="level3" data-number="2.4.1">
|
<section id="base-r" class="level3" data-number="2.4.1">
|
||||||
<h3 data-number="2.4.1" class="anchored" data-anchor-id="base-r"><span class="header-section-number">2.4.1</span> Base R</h3>
|
<h3 data-number="2.4.1" class="anchored" data-anchor-id="base-r"><span class="header-section-number">2.4.1</span> Base R</h3>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> uk_census_2021_religion_wmids[<span class="fu">order</span>(uk_census_2021_religion_wmids<span class="sc">$</span>value,<span class="at">decreasing =</span> <span class="cn">TRUE</span>),]</span>
|
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> uk_census_2021_religion_wmids[<span class="fu">order</span>(uk_census_2021_religion_wmids<span class="sc">$</span>value,<span class="at">decreasing =</span> <span class="cn">TRUE</span>),]</span>
|
||||||
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="fu">barplot</span>(<span class="at">height=</span>df<span class="sc">$</span>value, <span class="at">names=</span>df<span class="sc">$</span>key)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">barplot</span>(<span class="at">height=</span>df<span class="sc">$</span>value, <span class="at">names=</span>df<span class="sc">$</span>key)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -573,48 +567,48 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<section id="ggplot" class="level3 page-columns page-full" data-number="2.4.2">
|
<section id="ggplot" class="level3 page-columns page-full" data-number="2.4.2">
|
||||||
<h3 data-number="2.4.2" class="anchored" data-anchor-id="ggplot"><span class="header-section-number">2.4.2</span> GGPlot</h3>
|
<h3 data-number="2.4.2" class="anchored" data-anchor-id="ggplot"><span class="header-section-number">2.4.2</span> GGPlot</h3>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="annotated-cell-11"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-11-1"><a href="#annotated-cell-11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_wmids, <span class="fu">aes</span>(<span class="at">x =</span> key, <span class="at">y =</span> value)) <span class="sc">+</span></span>
|
<div class="sourceCode cell-code" id="annotated-cell-9"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-9-1"><a href="#annotated-cell-9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_wmids, <span class="fu">aes</span>(<span class="at">x =</span> key, <span class="at">y =</span> value)) <span class="sc">+</span></span>
|
||||||
<span id="annotated-cell-11-2"><a href="#annotated-cell-11-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="annotated-cell-9-2"><a href="#annotated-cell-9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-annotation">
|
<div class="cell-annotation">
|
||||||
<dl class="code-annotation-container-grid">
|
<dl class="code-annotation-container-grid">
|
||||||
<dt data-target-cell="annotated-cell-12" data-target-annotation="2">2</dt>
|
<dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-cell="annotated-cell-12" data-code-annotation="2" data-code-lines="1">We’ll re-order the column by size.</span>
|
<span data-code-lines="1" data-code-cell="annotated-cell-10" data-code-annotation="2">We’ll re-order the column by size.</span>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sourceCode cell-code" id="annotated-cell-12"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-12-1" class="code-annotation-target"><a href="#annotated-cell-12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_wmids, <span class="fu">aes</span>(<span class="at">x=</span> <span class="fu">reorder</span>(key,<span class="sc">-</span>value),value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span><span class="st">"identity"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="annotated-cell-10"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-10" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-10-1" class="code-annotation-target"><a href="#annotated-cell-10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_wmids, <span class="fu">aes</span>(<span class="at">x=</span> <span class="fu">reorder</span>(key,<span class="sc">-</span>value),value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span><span class="st">"identity"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-7-2.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-7-2.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>Let’s assume we’re working with a data set that doesn’t include a “totals” column and that we might want to get sums for each column. This is pretty easy to do in R:</p>
|
<p>Let’s assume we’re working with a data set that doesn’t include a “totals” column and that we might want to get sums for each column. This is pretty easy to do in R:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="annotated-cell-13"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-13-1" class="code-annotation-target"><a href="#annotated-cell-13-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion <span class="sc">%>%</span> <span class="fu">select</span>(no_religion<span class="sc">:</span>no_response)</span>
|
<div class="sourceCode cell-code" id="annotated-cell-11"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-11-1" class="code-annotation-target"><a href="#annotated-cell-11-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion <span class="sc">%>%</span> <span class="fu">select</span>(no_religion<span class="sc">:</span>no_response)</span>
|
||||||
<span id="annotated-cell-13-2"><a href="#annotated-cell-13-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion_totals <span class="sc">%>%</span></span>
|
<span id="annotated-cell-11-2"><a href="#annotated-cell-11-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion_totals <span class="sc">%>%</span></span>
|
||||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-13-3" class="code-annotation-target"><a href="#annotated-cell-13-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="fu">across</span>(<span class="fu">everything</span>(), <span class="sc">~</span> <span class="fu">sum</span>(., <span class="at">na.rm =</span> <span class="cn">TRUE</span>)))</span>
|
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-11-3" class="code-annotation-target"><a href="#annotated-cell-11-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="fu">across</span>(<span class="fu">everything</span>(), <span class="sc">~</span> <span class="fu">sum</span>(., <span class="at">na.rm =</span> <span class="cn">TRUE</span>)))</span>
|
||||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-13-4" class="code-annotation-target"><a href="#annotated-cell-13-4" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> <span class="fu">gather</span>(uk_census_2021_religion_totals)</span>
|
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-11-4" class="code-annotation-target"><a href="#annotated-cell-11-4" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> <span class="fu">gather</span>(uk_census_2021_religion_totals)</span>
|
||||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="4" onclick="event.preventDefault();">4</a><span id="annotated-cell-13-5" class="code-annotation-target"><a href="#annotated-cell-13-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_totals, <span class="fu">aes</span>(<span class="at">x=</span> <span class="fu">reorder</span>(key,<span class="sc">-</span>value),value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span><span class="st">"identity"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<a class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="4" onclick="event.preventDefault();">4</a><span id="annotated-cell-11-5" class="code-annotation-target"><a href="#annotated-cell-11-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_totals, <span class="fu">aes</span>(<span class="at">x=</span> <span class="fu">reorder</span>(key,<span class="sc">-</span>value),value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span><span class="st">"identity"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-annotation">
|
<div class="cell-annotation">
|
||||||
<dl class="code-annotation-container-grid">
|
<dl class="code-annotation-container-grid">
|
||||||
<dt data-target-cell="annotated-cell-13" data-target-annotation="1">1</dt>
|
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-cell="annotated-cell-13" data-code-annotation="1" data-code-lines="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
|
<span data-code-lines="1" data-code-cell="annotated-cell-11" data-code-annotation="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
|
||||||
</dd>
|
</dd>
|
||||||
<dt data-target-cell="annotated-cell-13" data-target-annotation="2">2</dt>
|
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-cell="annotated-cell-13" data-code-annotation="2" data-code-lines="3">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that you’ll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)</code></span>
|
<span data-code-lines="3" data-code-cell="annotated-cell-11" data-code-annotation="2">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that you’ll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)</code></span>
|
||||||
</dd>
|
</dd>
|
||||||
<dt data-target-cell="annotated-cell-13" data-target-annotation="3">3</dt>
|
<dt data-target-cell="annotated-cell-11" data-target-annotation="3">3</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-cell="annotated-cell-13" data-code-annotation="3" data-code-lines="4">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
|
<span data-code-lines="4" data-code-cell="annotated-cell-11" data-code-annotation="3">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
|
||||||
</dd>
|
</dd>
|
||||||
<dt data-target-cell="annotated-cell-13" data-target-annotation="4">4</dt>
|
<dt data-target-cell="annotated-cell-11" data-target-annotation="4">4</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-cell="annotated-cell-13" data-code-annotation="4" data-code-lines="5">Now plot it out and have a look!</span>
|
<span data-code-lines="5" data-code-cell="annotated-cell-11" data-code-annotation="4">Now plot it out and have a look!</span>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
|
@ -624,17 +618,17 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
</div>
|
</div>
|
||||||
<p>You might have noticed that these two dataframes give us somewhat different results. But with data science, it’s much more interesting to compare these two side-by-side in a visualisation. We can join these two dataframes and plot the bars side by side using <code>bind()</code> - which can be done by columns with cbind() and rows using rbind():</p>
|
<p>You might have noticed that these two dataframes give us somewhat different results. But with data science, it’s much more interesting to compare these two side-by-side in a visualisation. We can join these two dataframes and plot the bars side by side using <code>bind()</code> - which can be done by columns with cbind() and rows using rbind():</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>Do you notice there’s going to be a problem here? How can we tell one set from the other? We need to add in something idenfiable first! This isn’t too hard to do as we can simply create a new column for each with identifiable information before we bind them:</p>
|
<p>Do you notice there’s going to be a problem here? How can we tell one set from the other? We need to add in something idenfiable first! This isn’t too hard to do as we can simply create a new column for each with identifiable information before we bind them:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"totals"</span>)</span>
|
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"totals"</span>)</span>
|
||||||
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"wmids"</span>)</span>
|
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"wmids"</span>)</span>
|
||||||
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>Now we’re ready to plot out our data as a grouped barplot:</p>
|
<p>Now we’re ready to plot out our data as a grouped barplot:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span> <span class="fu">reorder</span>(key,<span class="sc">-</span>value), value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span> <span class="fu">reorder</span>(key,<span class="sc">-</span>value), value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -643,12 +637,12 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<p>If you inspect our chart, you can see that we’re getting closer, but it’s not really that helpful to compare the totals. What we need to do is get percentages that can be compared side by side. This is easy to do using another <code>dplyr</code> feature <code>mutate</code>:</p>
|
<p>If you inspect our chart, you can see that we’re getting closer, but it’s not really that helpful to compare the totals. What we need to do is get percentages that can be compared side by side. This is easy to do using another <code>dplyr</code> feature <code>mutate</code>:</p>
|
||||||
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">It’s worth noting that an alternative approach is to leave the numbers intact and simply label them differently so they render as percentages on your charts. You can do this with the `scales() library and the label_percent() function. The downside of this approach is that it won’t transfer to tables if you make them.</span></div></div>
|
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">It’s worth noting that an alternative approach is to leave the numbers intact and simply label them differently so they render as percentages on your charts. You can do this with the `scales() library and the label_percent() function. The downside of this approach is that it won’t transfer to tables if you make them.</span></div></div>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="annotated-cell-17"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-17-1"><a href="#annotated-cell-17-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion_totals <span class="sc">%>%</span> </span>
|
<div class="sourceCode cell-code" id="annotated-cell-15"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-15-1"><a href="#annotated-cell-15-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion_totals <span class="sc">%>%</span> </span>
|
||||||
<span id="annotated-cell-17-2"><a href="#annotated-cell-17-2" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(value <span class="sc">/</span> <span class="fu">sum</span>(value), <span class="at">accuracy =</span> <span class="fl">0.1</span>, <span class="at">trim =</span> <span class="cn">FALSE</span>))</span>
|
<span id="annotated-cell-15-2"><a href="#annotated-cell-15-2" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(value <span class="sc">/</span> <span class="fu">sum</span>(value), <span class="at">accuracy =</span> <span class="fl">0.1</span>, <span class="at">trim =</span> <span class="cn">FALSE</span>))</span>
|
||||||
<span id="annotated-cell-17-3"><a href="#annotated-cell-17-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion_wmids <span class="sc">%>%</span> </span>
|
<span id="annotated-cell-15-3"><a href="#annotated-cell-15-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion_wmids <span class="sc">%>%</span> </span>
|
||||||
<span id="annotated-cell-17-4"><a href="#annotated-cell-17-4" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(value <span class="sc">/</span> <span class="fu">sum</span>(value), <span class="at">accuracy =</span> <span class="fl">0.1</span>, <span class="at">trim =</span> <span class="cn">FALSE</span>))</span>
|
<span id="annotated-cell-15-4"><a href="#annotated-cell-15-4" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(value <span class="sc">/</span> <span class="fu">sum</span>(value), <span class="at">accuracy =</span> <span class="fl">0.1</span>, <span class="at">trim =</span> <span class="cn">FALSE</span>))</span>
|
||||||
<span id="annotated-cell-17-5"><a href="#annotated-cell-17-5" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span>
|
<span id="annotated-cell-15-5"><a href="#annotated-cell-15-5" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span>
|
||||||
<span id="annotated-cell-17-6"><a href="#annotated-cell-17-6" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span>key, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="annotated-cell-15-6"><a href="#annotated-cell-15-6" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span>key, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -657,7 +651,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<p>We’ll draw this data into comparison with later sets in the next chapter. But the one glaring issue which remains for our chart is that it’s lacking in really any aesthetic refinements. This is where <code>ggplot</code> really shines as a tool as you can add all sorts of things.</p>
|
<p>We’ll draw this data into comparison with later sets in the next chapter. But the one glaring issue which remains for our chart is that it’s lacking in really any aesthetic refinements. This is where <code>ggplot</code> really shines as a tool as you can add all sorts of things.</p>
|
||||||
<p>These are basically just added to our <code>ggplot</code> code. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the <code>scale</code> using <code>scale_fill_brewer</code>. This uses a particular tool (and a personal favourite of mine) called <code>colorbrewer</code>. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.</p>
|
<p>These are basically just added to our <code>ggplot</code> code. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the <code>scale</code> using <code>scale_fill_brewer</code>. This uses a particular tool (and a personal favourite of mine) called <code>colorbrewer</code>. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span>key, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span>key, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -665,15 +659,15 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<p>We might also want to add a border to our bars to make them more visually striking (notice the addition of <code>color</code> to the geom_bar below. I’ve also added <code>reorder()</code> to the x value to sort descending from the largest to smallest.</p>
|
<p>We might also want to add a border to our bars to make them more visually striking (notice the addition of <code>color</code> to the geom_bar below. I’ve also added <code>reorder()</code> to the x value to sort descending from the largest to smallest.</p>
|
||||||
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">You can find more information about reordering ggplots on the <a href="https://r-graph-gallery.com/267-reorder-a-variable-in-ggplot2.html">R Graph gallery</a>.</span></div></div>
|
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">You can find more information about reordering ggplots on the <a href="https://r-graph-gallery.com/267-reorder-a-variable-in-ggplot2.html">R Graph gallery</a>.</span></div></div>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">factor</span>(uk_census_2021_religion_merged<span class="sc">$</span>dataset, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">'wmids'</span>, <span class="st">'totals'</span>))</span>
|
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">factor</span>(uk_census_2021_religion_merged<span class="sc">$</span>dataset, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">'wmids'</span>, <span class="st">'totals'</span>))</span>
|
||||||
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>We can fine tune a few other visual features here as well, like adding a title with <code>ggtitle</code> and a them with some prettier fonts with <code>theme_ipsum()</code> (which requires the <code>hrbrthemes()</code> library). We can also remove the x and y axis labels (not the data labels, which are rather important).</p>
|
<p>We can fine tune a few other visual features here as well, like adding a title with <code>ggtitle</code> and a them with some prettier fonts with <code>theme_ipsum()</code> (which requires the <code>hrbrthemes()</code> library). We can also remove the x and y axis labels (not the data labels, which are rather important).</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the UK: 2021"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the UK: 2021"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -687,7 +681,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
|
||||||
<p>Finally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.</p>
|
<p>Finally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.</p>
|
||||||
<p>What does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive, almost completely population level, sample so there is additional validity there. The easiest way to represent all this reality in the form of speaking truthfully about our data is to acknowledge that however valid it may seem, it is nonetheless a snapshot. For this reason, I would always advise that the best title for a chart is one which specifies the data set. We should also probably do something different with those non-responses:</p>
|
<p>What does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive, almost completely population level, sample so there is additional validity there. The easiest way to represent all this reality in the form of speaking truthfully about our data is to acknowledge that however valid it may seem, it is nonetheless a snapshot. For this reason, I would always advise that the best title for a chart is one which specifies the data set. We should also probably do something different with those non-responses:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -713,204 +707,169 @@ What is Nomis?
|
||||||
</div>
|
</div>
|
||||||
<div class="callout-body-container callout-body">
|
<div class="callout-body-container callout-body">
|
||||||
<p>For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It’s worth noting that if you’re not in the UK, there are similar options for other countries. Nearly every R textbook I’ve ever seen works with USA census data, so you’ll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.</p>
|
<p>For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It’s worth noting that if you’re not in the UK, there are similar options for other countries. Nearly every R textbook I’ve ever seen works with USA census data, so you’ll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.</p>
|
||||||
<p>Here’s the process to identify a dataset within the nomis platform:</p>
|
<p>If you want to draw some data from the nomis platform yourself in R, have a look at the <a href="https://github.com/kidwellj/hacking_religion_cookbook/blob/main/nomis.R">companion cookbook repository</a>.</p>
|
||||||
<div class="cell">
|
|
||||||
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Process to explore nomis() data for specific datasets</span></span>
|
|
||||||
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(nomisr)</span>
|
|
||||||
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="co"># temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:</span></span>
|
|
||||||
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a><span class="co">#religion_search <- nomis_search(name = "*Religion*")</span></span>
|
|
||||||
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a><span class="co">#religion_measures <- nomis_get_metadata("ST104", "measures")</span></span>
|
|
||||||
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a><span class="co">#tibble::glimpse(religion_measures)</span></span>
|
|
||||||
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="co">#religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(nomisr)</span>
|
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Get table of Census 2011 religion data from nomis</span></span>
|
||||||
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Get table of Census 2011 religion data from nomis</span></span>
|
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Note: for reproducible code used to generate the dataset used in the book, see the cookbook here: </span></span>
|
||||||
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="co"># temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:</span></span>
|
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>z <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z.rds"</span>)))</span>
|
||||||
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="co">#z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301))</span></span>
|
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a><span class="co">#saveRDS(z, file = "z.rds")</span></span>
|
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
||||||
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a>z <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z.rds"</span>)))</span>
|
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot"><-</span> <span class="fu">filter</span>(z, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> RURAL_URBAN_NAME<span class="sc">==</span><span class="st">"Total"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
|
||||||
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Drop unnecessary columns</span></span>
|
||||||
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot"><-</span> <span class="fu">select</span>(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)</span>
|
||||||
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot"><-</span> <span class="fu">filter</span>(z, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> RURAL_URBAN_NAME<span class="sc">==</span><span class="st">"Total"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
|
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot results</span></span>
|
||||||
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a><span class="co"># Drop unnecessary columns</span></span>
|
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_2011_religion, <span class="fu">aes</span>(<span class="at">x =</span> C_RELPUK11_NAME, <span class="at">y =</span> OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"identity"</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span>
|
||||||
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot"><-</span> <span class="fu">select</span>(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)</span>
|
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a><span class="co"># ggsave(filename = "plot.png", plot = plot1)</span></span>
|
||||||
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot results</span></span>
|
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_2011_religion, <span class="fu">aes</span>(<span class="at">x =</span> C_RELPUK11_NAME, <span class="at">y =</span> OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"identity"</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span>
|
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a><span class="co"># grab daata from nomis for 2001 census religion / ethnicity</span></span>
|
||||||
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a><span class="co"># ggsave(filename = "plot.png", plot = plot1)</span></span>
|
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a>z0 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z0.rds"</span>)))</span>
|
||||||
<span id="cb24-15"><a href="#cb24-15" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-16"><a href="#cb24-16" aria-hidden="true" tabindex="-1"></a><span class="co"># grab daata from nomis for 2001 census religion / ethnicity</span></span>
|
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
||||||
<span id="cb24-17"><a href="#cb24-17" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)</span>
|
||||||
<span id="cb24-18"><a href="#cb24-18" aria-hidden="true" tabindex="-1"></a><span class="co"># temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:</span></span>
|
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
||||||
<span id="cb24-19"><a href="#cb24-19" aria-hidden="true" tabindex="-1"></a><span class="co">#z0 <- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100))</span></span>
|
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2001_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
|
||||||
<span id="cb24-20"><a href="#cb24-20" aria-hidden="true" tabindex="-1"></a><span class="co">#saveRDS(z0, file = "z0.rds")</span></span>
|
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
|
||||||
<span id="cb24-21"><a href="#cb24-21" aria-hidden="true" tabindex="-1"></a>z0 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z0.rds"</span>)))</span>
|
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHHUK11_NAME))</span>
|
||||||
<span id="cb24-22"><a href="#cb24-22" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-23"><a href="#cb24-23" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a><span class="co"># grab data from nomis for 2011 census religion / ethnicity table</span></span>
|
||||||
<span id="cb24-24"><a href="#cb24-24" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)</span>
|
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a>z1 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z1.rds"</span>)))</span>
|
||||||
<span id="cb24-25"><a href="#cb24-25" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
<span id="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-26"><a href="#cb24-26" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2001_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
|
<span id="cb18-26"><a href="#cb18-26" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
||||||
<span id="cb24-27"><a href="#cb24-27" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
|
<span id="cb18-27"><a href="#cb18-27" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)</span>
|
||||||
<span id="cb24-28"><a href="#cb24-28" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHHUK11_NAME))</span>
|
<span id="cb18-28"><a href="#cb18-28" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
||||||
<span id="cb24-29"><a href="#cb24-29" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-29"><a href="#cb18-29" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span> <span class="sc">&</span> C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Ethnic group"</span>)</span>
|
||||||
<span id="cb24-30"><a href="#cb24-30" aria-hidden="true" tabindex="-1"></a><span class="co"># grab data from nomis for 2011 census religion / ethnicity table</span></span>
|
<span id="cb18-30"><a href="#cb18-30" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
|
||||||
<span id="cb24-31"><a href="#cb24-31" aria-hidden="true" tabindex="-1"></a><span class="co"># commenting out nomis_get temporarily until I can get renv working properly here</span></span>
|
<span id="cb18-31"><a href="#cb18-31" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHPUK11_NAME))</span>
|
||||||
<span id="cb24-32"><a href="#cb24-32" aria-hidden="true" tabindex="-1"></a><span class="co">#z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100))</span></span>
|
<span id="cb18-32"><a href="#cb18-32" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-33"><a href="#cb24-33" aria-hidden="true" tabindex="-1"></a><span class="co">#saveRDS(z1, file = "z1.rds")</span></span>
|
<span id="cb18-33"><a href="#cb18-33" aria-hidden="true" tabindex="-1"></a><span class="co"># grab data from nomis for 2021 census religion / ethnicity table</span></span>
|
||||||
<span id="cb24-34"><a href="#cb24-34" aria-hidden="true" tabindex="-1"></a>z1 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z1.rds"</span>)))</span>
|
<span id="cb18-34"><a href="#cb18-34" aria-hidden="true" tabindex="-1"></a>z2 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z2.rds"</span>)))</span>
|
||||||
<span id="cb24-35"><a href="#cb24-35" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-35"><a href="#cb18-35" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-36"><a href="#cb24-36" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
<span id="cb18-36"><a href="#cb18-36" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
||||||
<span id="cb24-37"><a href="#cb24-37" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)</span>
|
<span id="cb18-37"><a href="#cb18-37" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)</span>
|
||||||
<span id="cb24-38"><a href="#cb24-38" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
<span id="cb18-38"><a href="#cb18-38" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
||||||
<span id="cb24-39"><a href="#cb24-39" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span> <span class="sc">&</span> C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Ethnic group"</span>)</span>
|
<span id="cb18-39"><a href="#cb18-39" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C2021_RELIGION_10_NAME <span class="sc">!=</span> <span class="st">"Total"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"Total"</span>)</span>
|
||||||
<span id="cb24-40"><a href="#cb24-40" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
|
<span id="cb18-40"><a href="#cb18-40" aria-hidden="true" tabindex="-1"></a><span class="co"># 2021 census includes white sub-groups so we need to omit those so we just have totals:</span></span>
|
||||||
<span id="cb24-41"><a href="#cb24-41" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHPUK11_NAME))</span>
|
<span id="cb18-41"><a href="#cb18-41" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: English, Welsh, Scottish, Northern Irish or British"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Irish"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Gypsy or Irish Traveller, Roma or Other White"</span>)</span>
|
||||||
<span id="cb24-42"><a href="#cb24-42" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb18-42"><a href="#cb18-42" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb24-43"><a href="#cb24-43" aria-hidden="true" tabindex="-1"></a><span class="co"># grab data from nomis for 2021 census religion / ethnicity table</span></span>
|
<span id="cb18-43"><a href="#cb18-43" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>C_ETHPUK11_NAME, <span class="at">x=</span>C_RELPUK11_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<span id="cb24-44"><a href="#cb24-44" aria-hidden="true" tabindex="-1"></a><span class="co">#z2 <- nomis_get_data(id = "NM_2131_1", time = "latest", geography = "TYPE499", measures=c(20100))</span></span>
|
|
||||||
<span id="cb24-45"><a href="#cb24-45" aria-hidden="true" tabindex="-1"></a><span class="co">#saveRDS(z2, file = "z2.rds")</span></span>
|
|
||||||
<span id="cb24-46"><a href="#cb24-46" aria-hidden="true" tabindex="-1"></a>z2 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"z2.rds"</span>)))</span>
|
|
||||||
<span id="cb24-47"><a href="#cb24-47" aria-hidden="true" tabindex="-1"></a></span>
|
|
||||||
<span id="cb24-48"><a href="#cb24-48" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
|
||||||
<span id="cb24-49"><a href="#cb24-49" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)</span>
|
|
||||||
<span id="cb24-50"><a href="#cb24-50" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
|
||||||
<span id="cb24-51"><a href="#cb24-51" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C2021_RELIGION_10_NAME <span class="sc">!=</span> <span class="st">"Total"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"Total"</span>)</span>
|
|
||||||
<span id="cb24-52"><a href="#cb24-52" aria-hidden="true" tabindex="-1"></a><span class="co"># 2021 census includes white sub-groups so we need to omit those so we just have totals:</span></span>
|
|
||||||
<span id="cb24-53"><a href="#cb24-53" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: English, Welsh, Scottish, Northern Irish or British"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Irish"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Gypsy or Irish Traveller, Roma or Other White"</span>)</span>
|
|
||||||
<span id="cb24-54"><a href="#cb24-54" aria-hidden="true" tabindex="-1"></a></span>
|
|
||||||
<span id="cb24-55"><a href="#cb24-55" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>C_ETHPUK11_NAME, <span class="at">x=</span>C_RELPUK11_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>The trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.</p>
|
<p>The trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
|
||||||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity_white <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME <span class="sc">==</span> <span class="st">"White: Total"</span>)</span>
|
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity_white <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME <span class="sc">==</span> <span class="st">"White: Total"</span>)</span>
|
||||||
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"White: Total"</span>)</span>
|
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"White: Total"</span>)</span>
|
||||||
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>C_ETHPUK11_NAME, <span class="at">x=</span>C_RELPUK11_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>C_ETHPUK11_NAME, <span class="at">x=</span>C_RELPUK11_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>This still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.</p>
|
<p>This still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">x=</span>C_RELPUK11_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>C_ETHPUK11_NAME, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2011 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">x=</span>C_RELPUK11_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>C_ETHPUK11_NAME, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2011 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>For our finale chart, I’d like to take the faceted chart we’ve just done, and add in totals for the previous two census years (2001 and 2011) so we can see how trends are changing in terms of religious affiliation within ethnic self-identification categories. We’ll draw on some techniques we’re already developed above using <code>rbind()</code> to connect up each of these charts (after we’ve added a column identifying each chart by the census year). We will also need to use one new technique to change the wording of ethnic categories as this isn’t consistent from one census to the next and ggplot will struggle to chart things if the terms being used are exactly the same. We’ll use <code>mutate()</code> again to accomplish this with some slightly different code.</p>
|
<p>For our finale chart, I’d like to take the faceted chart we’ve just done, and add in totals for the previous two census years (2001 and 2011) so we can see how trends are changing in terms of religious affiliation within ethnic self-identification categories. We’ll draw on some techniques we’re already developed above using <code>rbind()</code> to connect up each of these charts (after we’ve added a column identifying each chart by the census year). We will also need to use one new technique to change the wording of ethnic categories as this isn’t consistent from one census to the next and ggplot will struggle to chart things if the terms being used are exactly the same. We’ll use <code>mutate()</code> again to accomplish this with some slightly different code.</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co"># First add column to each dataframe so we don't lose track of the census it comes from:</span></span>
|
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co"># First add column to each dataframe so we don't lose track of the census it comes from:</span></span>
|
||||||
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2001"</span>)</span>
|
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2001"</span>)</span>
|
||||||
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2011"</span>)</span>
|
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2011"</span>)</span>
|
||||||
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2021"</span>)</span>
|
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2021"</span>)</span>
|
||||||
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Let's tidy the names of each column:</span></span>
|
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Let's tidy the names of each column:</span></span>
|
||||||
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2001_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2001_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||||
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2011_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2011_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||||
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2021_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2021_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||||
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Next we need to change the terms using mutate()</span></span>
|
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Next we need to change the terms using mutate()</span></span>
|
||||||
<span id="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> </span>
|
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||||
<span id="cb27-14"><a href="#cb27-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-15"><a href="#cb27-15" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-16"><a href="#cb27-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-17"><a href="#cb27-17" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-18"><a href="#cb27-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-19"><a href="#cb27-19" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-20"><a href="#cb27-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-21"><a href="#cb27-21" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black or Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-21"><a href="#cb21-21" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black or Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-22"><a href="#cb27-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-22"><a href="#cb21-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-23"><a href="#cb27-23" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Chinese or Other ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Other"</span>))</span>
|
<span id="cb21-23"><a href="#cb21-23" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Chinese or Other ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Other"</span>))</span>
|
||||||
<span id="cb27-24"><a href="#cb27-24" aria-hidden="true" tabindex="-1"></a> </span>
|
<span id="cb21-24"><a href="#cb21-24" aria-hidden="true" tabindex="-1"></a> </span>
|
||||||
<span id="cb27-25"><a href="#cb27-25" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> </span>
|
<span id="cb21-25"><a href="#cb21-25" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||||
<span id="cb27-26"><a href="#cb27-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-26"><a href="#cb21-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-27"><a href="#cb27-27" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-27"><a href="#cb21-27" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-28"><a href="#cb27-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-28"><a href="#cb21-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-29"><a href="#cb27-29" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed/multiple ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-29"><a href="#cb21-29" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed/multiple ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-30"><a href="#cb27-30" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-30"><a href="#cb21-30" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-31"><a href="#cb27-31" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian/Asian British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-31"><a href="#cb21-31" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian/Asian British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-32"><a href="#cb27-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-32"><a href="#cb21-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-33"><a href="#cb27-33" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black/African/Caribbean/Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-33"><a href="#cb21-33" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black/African/Caribbean/Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-34"><a href="#cb27-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-34"><a href="#cb21-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-35"><a href="#cb27-35" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Other ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Other"</span>))</span>
|
<span id="cb21-35"><a href="#cb21-35" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Other ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Other"</span>))</span>
|
||||||
<span id="cb27-36"><a href="#cb27-36" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-36"><a href="#cb21-36" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-37"><a href="#cb27-37" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> uk_census_2021_religion_ethnicity <span class="sc">%>%</span> </span>
|
<span id="cb21-37"><a href="#cb21-37" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> uk_census_2021_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||||
<span id="cb27-38"><a href="#cb27-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-38"><a href="#cb21-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-39"><a href="#cb27-39" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-39"><a href="#cb21-39" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-40"><a href="#cb27-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-40"><a href="#cb21-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-41"><a href="#cb27-41" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed or Multiple ethnic groups$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-41"><a href="#cb21-41" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed or Multiple ethnic groups$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-42"><a href="#cb27-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-42"><a href="#cb21-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-43"><a href="#cb27-43" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian, Asian British or Asian Welsh$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-43"><a href="#cb21-43" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian, Asian British or Asian Welsh$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-44"><a href="#cb27-44" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-44"><a href="#cb21-44" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-45"><a href="#cb27-45" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black, Black British, Black Welsh, Caribbean or African$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
<span id="cb21-45"><a href="#cb21-45" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black, Black British, Black Welsh, Caribbean or African$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||||
<span id="cb27-46"><a href="#cb27-46" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
<span id="cb21-46"><a href="#cb21-46" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Ethnicity =</span> <span class="fu">str_replace_all</span>(Ethnicity, </span>
|
||||||
<span id="cb27-47"><a href="#cb27-47" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Other ethnic group$"</span>, <span class="at">replacement =</span> <span class="st">"Other"</span>))</span>
|
<span id="cb21-47"><a href="#cb21-47" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Other ethnic group$"</span>, <span class="at">replacement =</span> <span class="st">"Other"</span>))</span>
|
||||||
<span id="cb27-48"><a href="#cb27-48" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-48"><a href="#cb21-48" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-49"><a href="#cb27-49" aria-hidden="true" tabindex="-1"></a><span class="co"># Now let's merge the tables:</span></span>
|
<span id="cb21-49"><a href="#cb21-49" aria-hidden="true" tabindex="-1"></a><span class="co"># Now let's merge the tables:</span></span>
|
||||||
<span id="cb27-50"><a href="#cb27-50" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-50"><a href="#cb21-50" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-51"><a href="#cb27-51" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_ethnicity, uk_census_2011_religion_ethnicity)</span>
|
<span id="cb21-51"><a href="#cb21-51" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_ethnicity, uk_census_2011_religion_ethnicity)</span>
|
||||||
<span id="cb27-52"><a href="#cb27-52" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-52"><a href="#cb21-52" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-53"><a href="#cb27-53" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_merged_religion_ethnicity, uk_census_2001_religion_ethnicity)</span>
|
<span id="cb21-53"><a href="#cb21-53" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_merged_religion_ethnicity, uk_census_2001_religion_ethnicity)</span>
|
||||||
<span id="cb27-54"><a href="#cb27-54" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-54"><a href="#cb21-54" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-55"><a href="#cb27-55" aria-hidden="true" tabindex="-1"></a><span class="co"># As above, we'll split out non-white and white:</span></span>
|
<span id="cb21-55"><a href="#cb21-55" aria-hidden="true" tabindex="-1"></a><span class="co"># As above, we'll split out non-white and white:</span></span>
|
||||||
<span id="cb27-56"><a href="#cb27-56" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-56"><a href="#cb21-56" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-57"><a href="#cb27-57" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_merged_religion_ethnicity, Ethnicity <span class="sc">!=</span> <span class="st">"White"</span>)</span>
|
<span id="cb21-57"><a href="#cb21-57" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_merged_religion_ethnicity, Ethnicity <span class="sc">!=</span> <span class="st">"White"</span>)</span>
|
||||||
<span id="cb27-58"><a href="#cb27-58" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-58"><a href="#cb21-58" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-59"><a href="#cb27-59" aria-hidden="true" tabindex="-1"></a><span class="co"># Time to plot!</span></span>
|
<span id="cb21-59"><a href="#cb21-59" aria-hidden="true" tabindex="-1"></a><span class="co"># Time to plot!</span></span>
|
||||||
<span id="cb27-60"><a href="#cb27-60" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb21-60"><a href="#cb21-60" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb27-61"><a href="#cb27-61" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb21-61"><a href="#cb21-61" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>There are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn’t really very easy to read. You can use the very powerful and flexible <code>scales()</code> library to bring in some more readable formatting of numbers in a variety of places in R including in ggplot visualizations.</p>
|
<p>There are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn’t really very easy to read. You can use the very powerful and flexible <code>scales()</code> library to bring in some more readable formatting of numbers in a variety of places in R including in ggplot visualizations.</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(scales)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(scales) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
<div class="cell-output cell-output-stderr">
|
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> <span class="fu">unit_format</span>(<span class="at">unit =</span> <span class="st">"M"</span>, <span class="at">scale =</span> <span class="fl">1e-6</span>), <span class="at">breaks =</span> <span class="fu">breaks_extended</span>(<span class="dv">8</span>)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<pre><code>
|
|
||||||
Attaching package: 'scales'</code></pre>
|
|
||||||
</div>
|
|
||||||
<div class="cell-output cell-output-stderr">
|
|
||||||
<pre><code>The following object is masked from 'package:purrr':
|
|
||||||
|
|
||||||
discard</code></pre>
|
|
||||||
</div>
|
|
||||||
<div class="cell-output cell-output-stderr">
|
|
||||||
<pre><code>The following object is masked from 'package:readr':
|
|
||||||
|
|
||||||
col_factor</code></pre>
|
|
||||||
</div>
|
|
||||||
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> <span class="fu">unit_format</span>(<span class="at">unit =</span> <span class="st">"M"</span>, <span class="at">scale =</span> <span class="fl">1e-6</span>), <span class="at">breaks =</span> <span class="fu">breaks_extended</span>(<span class="dv">8</span>)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="co"># https://ggplot2-book.org/scales-position#sec-position-continuous-breaks</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="co"># https://ggplot2-book.org/scales-position#sec-position-continuous-breaks</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>This chart shows an increase in almost every category, though it’s a bit hard to read in some cases. However, this information is based on the increase in raw numbers. It’s possbile that numbers may be going up, but in some cases the percentage share for a particular category has actually gone down. Let’s transform and visualise our data as percentages to see what kind of trends we can actually isolate:</p>
|
<p>This chart shows an increase in almost every category, though it’s a bit hard to read in some cases. However, this information is based on the increase in raw numbers. It’s possbile that numbers may be going up, but in some cases the percentage share for a particular category has actually gone down. Let’s transform and visualise our data as percentages to see what kind of trends we can actually isolate:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> uk_census_merged_religion_ethnicity <span class="sc">%>%</span></span>
|
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> uk_census_merged_religion_ethnicity <span class="sc">%>%</span></span>
|
||||||
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(Ethnicity, Year) <span class="sc">%>%</span></span>
|
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(Ethnicity, Year) <span class="sc">%>%</span></span>
|
||||||
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">Percent =</span> Value<span class="sc">/</span><span class="fu">sum</span>(Value))</span>
|
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">Percent =</span> Value<span class="sc">/</span><span class="fu">sum</span>(Value))</span>
|
||||||
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Percent)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">scales=</span><span class="st">"free_x"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span>percent) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Percent)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">scales=</span><span class="st">"free_x"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span>percent) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
<div class="cell-output-display">
|
<div class="cell-output-display">
|
||||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid" width="672"></p>
|
<p><img src="chapter_1_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid" width="672"></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>Now you can see why this shift is important - the visualisation tells a completely different story in some cases across the two different charts. In the first, working off raw numbers we see a net increase in Christianity across all categories. But if we take into account the fact that the overall share of population is growing for each of these groups, their actual composition is changing in a different direction. The proportion of each group is declining across the three census periods (albeit with an exception for the “Other” category from 2011 to 2021).</p>
|
<p>Now you can see why this shift is important - the visualisation tells a completely different story in some cases across the two different charts. In the first, working off raw numbers we see a net increase in Christianity across all categories. But if we take into account the fact that the overall share of population is growing for each of these groups, their actual composition is changing in a different direction. The proportion of each group is declining across the three census periods (albeit with an exception for the “Other” category from 2011 to 2021).</p>
|
||||||
<p>To highlight a few features of this final plot, I’ve used a specific feature within <code>facet_wrap</code> <code>scales = "free_x"</code> to let each of the individual facets adjust the total range on the x-axis. Since we’re looking at trends here and not absolute values, having correspondence across scales isn’t important and this makes for something a bit more visually tidy. I’ve also shifted the code for <code>scale_y_continuous</code> to render values as percentages (rather than millions).</p>
|
<p>To highlight a few features of this final plot, I’ve used a specific feature within <code>facet_wrap</code> <code>scales = "free_x"</code> to let each of the individual facets adjust the total range on the x-axis. Since we’re looking at trends here and not absolute values, having correspondence across scales isn’t important and this makes for something a bit more visually tidy. I’ve also shifted the code for <code>scale_y_continuous</code> to render values as percentages (rather than millions).</p>
|
||||||
<p>In case you want to print this plot out and hang it on your wall, you can use the ggsave tool to render the chart as an image file:</p>
|
<p>In case you want to print this plot out and hang it on your wall, you can use the ggsave tool to render the chart as an image file:</p>
|
||||||
<div class="cell">
|
<div class="cell">
|
||||||
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Percent)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">scales=</span><span class="st">"free_x"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span>percent) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span>
|
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Percent)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">scales=</span><span class="st">"free_x"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span>percent) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span>
|
||||||
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a></span>
|
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"chart.png"</span>, <span class="at">plot=</span>plot1, <span class="at">width =</span> <span class="dv">8</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units=</span><span class="fu">c</span>(<span class="st">"in"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"chart.png"</span>, <span class="at">plot=</span>plot1, <span class="at">width =</span> <span class="dv">8</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units=</span><span class="fu">c</span>(<span class="st">"in"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<section id="references" class="level1 unnumbered">
|
<section id="references" class="level1 unnumbered">
|
||||||
|
|
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 101 KiB |
Before Width: | Height: | Size: 101 KiB After Width: | Height: | Size: 109 KiB |
Before Width: | Height: | Size: 109 KiB After Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 159 KiB |
|
@ -355,15 +355,15 @@ So <em>who’s</em> religious?
|
||||||
<dl class="code-annotation-container-grid">
|
<dl class="code-annotation-container-grid">
|
||||||
<dt data-target-cell="annotated-cell-6" data-target-annotation="1">1</dt>
|
<dt data-target-cell="annotated-cell-6" data-target-annotation="1">1</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-annotation="1" data-code-lines="2" data-code-cell="annotated-cell-6">First we generate new a dataframe with sums per category and</span>
|
<span data-code-annotation="1" data-code-cell="annotated-cell-6" data-code-lines="2">First we generate new a dataframe with sums per category and</span>
|
||||||
</dd>
|
</dd>
|
||||||
<dt data-target-cell="annotated-cell-6" data-target-annotation="2">2</dt>
|
<dt data-target-cell="annotated-cell-6" data-target-annotation="2">2</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-annotation="2" data-code-lines="3" data-code-cell="annotated-cell-6">…sort in descending order</span>
|
<span data-code-annotation="2" data-code-cell="annotated-cell-6" data-code-lines="3">…sort in descending order</span>
|
||||||
</dd>
|
</dd>
|
||||||
<dt data-target-cell="annotated-cell-6" data-target-annotation="3">3</dt>
|
<dt data-target-cell="annotated-cell-6" data-target-annotation="3">3</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span data-code-annotation="3" data-code-lines="5" data-code-cell="annotated-cell-6">Then we add new column with percentages based on the sums you’ve just generated</span>
|
<span data-code-annotation="3" data-code-cell="annotated-cell-6" data-code-lines="5">Then we add new column with percentages based on the sums you’ve just generated</span>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -20,6 +20,40 @@ ul.task-list li input[type="checkbox"] {
|
||||||
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
|
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
}
|
}
|
||||||
|
/* CSS for syntax highlighting */
|
||||||
|
pre > code.sourceCode { white-space: pre; position: relative; }
|
||||||
|
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||||||
|
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||||||
|
.sourceCode { overflow: visible; }
|
||||||
|
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||||||
|
div.sourceCode { margin: 1em 0; }
|
||||||
|
pre.sourceCode { margin: 0; }
|
||||||
|
@media screen {
|
||||||
|
div.sourceCode { overflow: auto; }
|
||||||
|
}
|
||||||
|
@media print {
|
||||||
|
pre > code.sourceCode { white-space: pre-wrap; }
|
||||||
|
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||||||
|
}
|
||||||
|
pre.numberSource code
|
||||||
|
{ counter-reset: source-line 0; }
|
||||||
|
pre.numberSource code > span
|
||||||
|
{ position: relative; left: -4em; counter-increment: source-line; }
|
||||||
|
pre.numberSource code > span > a:first-child::before
|
||||||
|
{ content: counter(source-line);
|
||||||
|
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||||||
|
border: none; display: inline-block;
|
||||||
|
-webkit-touch-callout: none; -webkit-user-select: none;
|
||||||
|
-khtml-user-select: none; -moz-user-select: none;
|
||||||
|
-ms-user-select: none; user-select: none;
|
||||||
|
padding: 0 4px; width: 4em;
|
||||||
|
}
|
||||||
|
pre.numberSource { margin-left: 3em; padding-left: 4px; }
|
||||||
|
div.sourceCode
|
||||||
|
{ }
|
||||||
|
@media screen {
|
||||||
|
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||||||
|
}
|
||||||
/* CSS for citations */
|
/* CSS for citations */
|
||||||
div.csl-bib-body { }
|
div.csl-bib-body { }
|
||||||
div.csl-entry {
|
div.csl-entry {
|
||||||
|
@ -176,7 +210,9 @@ div.csl-indent {
|
||||||
<h2 id="toc-title">Table of contents</h2>
|
<h2 id="toc-title">Table of contents</h2>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="#references" id="toc-references" class="nav-link active" data-scroll-target="#references">References</a></li>
|
<li><a href="#administrative-shapes---the-uk" id="toc-administrative-shapes---the-uk" class="nav-link active" data-scroll-target="#administrative-shapes---the-uk"><span class="header-section-number">5</span> Administrative shapes - the UK</a></li>
|
||||||
|
<li><a href="#load-in-ordnance-survey-openmap-points-data" id="toc-load-in-ordnance-survey-openmap-points-data" class="nav-link" data-scroll-target="#load-in-ordnance-survey-openmap-points-data"><span class="header-section-number">6</span> Load in Ordnance Survey OpenMap Points Data</a></li>
|
||||||
|
<li><a href="#references" id="toc-references" class="nav-link" data-scroll-target="#references">References</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
|
@ -200,8 +236,196 @@ div.csl-indent {
|
||||||
|
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
|
<p>Until recently, most data science books didn’t have a section on geospatial data. It was considered a specialist form of research best left to GIS technicians who tended to use proprietary tools like ArcGIS. This has changed significantly in the past five years, but you’ll still be hard pressed to find an introduction to the subject which strays very far from a few simple data sets (mostly of the USA) and relatively uncomplicated geospatial operations. I actually first began learning R, back in 2013, right when open source geospatial research tools were beginning to be developed with quite a lot more energy and geospatial data is my personal favourite data science playground, so in this book we’re going to go much deeper than is usual. There are also good reasons to take things a few steps further in the particular forms of data and inquiry that religion takes us into.</p>
|
||||||
|
<p>Recommend https://r-spatial.org/book/</p>
|
||||||
|
<p>Geospatial data is, in the most basic form, working with maps. This means that most of your data can be a quite simple dataframe, e.g. just a list of names or categories associated with a set of X and Y coordinates. Once you have a set of items, however, things get interesting very quickly, as you can layer data sets on top of one another. We’re going to begin this chapter by developing a geolocated data set of churches in the UK. This information is readily and freely available online thanks to the UK Ordnance Survey, a quasi-governmental agency which maintains the various (now digital) maps of Britain. Lucky for us, the Ordnance Survey has an open data product that anyone can use!</p>
|
||||||
|
<p>Before we begin, there are some key things we should note about geospatial data. Geospatial data tends to fall into one of two kinds: points and polygons. Points can be any kind of feature: a house, a church, a pub, someone’s favourite ancient oak tree, or some kind of sacred relic. Polygons tend to be associated with wider areas, and as such can be used to describe large features, e.g. an Ocean, a local authority, or a mountain, or also demographic features, like a census Output Area with associated census summaries. Points are very simple data representations, an X and Y coordinate. Polygons are much more complex, often containing dozens or even thousands of points.</p>
|
||||||
|
<p>The most complex aspect of point data relates to the ways that coordinates are encoded, as they will aways need to be associated with a coordinate reference system (CRS) which describes how they are situated with respect to the planet earth. The most common CRS is the WGS, though for our data sets we’ll also come into contact with the BGS, a specifically British coordinate reference system. There are dozens of CRS, usually mapping onto a specific geographical region. Bearing in mind the way that you need to use a CRS to understand how coordinates can be associated with specific parts of the earth, you can see how this is a bit like survey data, where you also need a “codebook” to understand what the specific response values map onto, e.g. a “1” means “strongly agree” and so on. We also saw, in a previous chapter, how some forms of data have the codebook already baked into the factor data, simplifying the process of interpreting the data. In a similar way, some types of geospatial data sets can also come with CRS “baked in” while we’ll need to define CRS for other types. Here are some of the most common types of geospatial data files:</p>
|
||||||
|
<p>CSV: “comma separated values” a plain text file containing various coordinates Shapefile: a legacy file format, often still in use, but being replaced by others for a variety of good reasons. For more on this see [http://switchfromshapefile.org/] Geopackage: one of the more recent ways of packaging up geospatial data. Geopackages can contain a wide variety of different data and are easily portable. GeoJSON: a file format commonly used in other forms of coding, the “JSON” (an acronym for JavaScript Object Notation) is meant to be easily interchangeable across various platforms. GeoJSON is an augmented version of JSON data with coordinates added in.</p>
|
||||||
|
<p>Now that you have a sense of some of the basic aspects of geospatial data, let’s dive in and do a bit of learning in action.</p>
|
||||||
|
<section id="administrative-shapes---the-uk" class="level1" data-number="5">
|
||||||
|
<h1 data-number="5"><span class="header-section-number">5</span> Administrative shapes - the UK</h1>
|
||||||
|
<p>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.</p>
|
||||||
|
<div class="cell">
|
||||||
|
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
|
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(here) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
|
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
|
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">setwd</span>(<span class="st">"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion"</span>)</span>
|
||||||
|
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>here<span class="sc">::</span><span class="fu">i_am</span>(<span class="st">"chapter_3.qmd"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stderr">
|
||||||
|
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Download administrative boundaries for whole UK at country level</span></span>
|
||||||
|
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">file.exists</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_uk_2011_clipped.shp"</span>)) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="fu">download.file</span>(<span class="st">"https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_uk_2011_clipped.zip"</span>, <span class="at">destfile =</span> <span class="st">"data/infuse_uk_2011_clipped.zip"</span>)</span>
|
||||||
|
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="fu">unzip</span>(<span class="st">"data/infuse_uk_2011_clipped.zip"</span>, <span class="at">exdir =</span> <span class="st">"data"</span>)</span>
|
||||||
|
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>}</span>
|
||||||
|
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>uk_countries <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_uk_2011_clipped.shp"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stdout">
|
||||||
|
<pre><code>Reading layer `infuse_uk_2011_clipped' from data source
|
||||||
|
`/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/data/infuse_uk_2011_clipped.shp'
|
||||||
|
using driver `ESRI Shapefile'
|
||||||
|
Simple feature collection with 1 feature and 3 fields
|
||||||
|
Geometry type: MULTIPOLYGON
|
||||||
|
Dimension: XY
|
||||||
|
Bounding box: xmin: -69.1254 ymin: 5337.9 xmax: 655604.7 ymax: 1220302
|
||||||
|
Projected CRS: OSGB36 / British National Grid</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Download administrative boundaries for whole UK at regions level</span></span>
|
||||||
|
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">file.exists</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_rgn_2011_clipped.shp"</span>)) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="fu">download.file</span>(<span class="st">"https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_rgn_2011_clipped.zip"</span>, <span class="at">destfile =</span> <span class="st">"data/infuse_rgn_2011_clipped.zip"</span>)</span>
|
||||||
|
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="fu">unzip</span>(<span class="st">"data/infuse_rgn_2011_clipped.zip"</span>, <span class="at">exdir =</span> <span class="st">"data"</span>)</span>
|
||||||
|
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>}</span>
|
||||||
|
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>uk_rgn <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_rgn_2011_clipped.shp"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stdout">
|
||||||
|
<pre><code>Reading layer `infuse_rgn_2011_clipped' from data source
|
||||||
|
`/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/data/infuse_rgn_2011_clipped.shp'
|
||||||
|
using driver `ESRI Shapefile'
|
||||||
|
Simple feature collection with 9 features and 2 fields
|
||||||
|
Geometry type: MULTIPOLYGON
|
||||||
|
Dimension: XY
|
||||||
|
Bounding box: xmin: 82672 ymin: 5337.9 xmax: 655604.7 ymax: 657534.1
|
||||||
|
Projected CRS: OSGB36 / British National Grid</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Download administrative boundaries for whole UK at local authority level</span></span>
|
||||||
|
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">file.exists</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_dist_lyr_2011_clipped.shp"</span>)) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="fu">download.file</span>(<span class="st">"https://borders.ukdataservice.ac.uk/ukborders/easy_download/prebuilt/shape/infuse_dist_lyr_2011_clipped.zip"</span>, <span class="at">destfile =</span> <span class="st">"data/infuse_dist_lyr_2011_clipped.zip"</span>)</span>
|
||||||
|
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="fu">unzip</span>(<span class="st">"data/infuse_dist_lyr_2011_clipped.zip"</span>, <span class="at">exdir =</span> <span class="st">"data"</span>)</span>
|
||||||
|
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a>}</span>
|
||||||
|
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>local_authorities <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_dist_lyr_2011_clipped.shp"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stdout">
|
||||||
|
<pre><code>Reading layer `infuse_dist_lyr_2011_clipped' from data source
|
||||||
|
`/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/data/infuse_dist_lyr_2011_clipped.shp'
|
||||||
|
using driver `ESRI Shapefile'
|
||||||
|
Simple feature collection with 404 features and 3 fields
|
||||||
|
Geometry type: MULTIPOLYGON
|
||||||
|
Dimension: XY
|
||||||
|
Bounding box: xmin: -69.1254 ymin: 5337.9 xmax: 655604.7 ymax: 1220302
|
||||||
|
Projected CRS: OSGB36 / British National Grid</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Download building outlines for whole UK</span></span>
|
||||||
|
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">file.exists</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg"</span>)) <span class="sc">==</span> <span class="cn">FALSE</span>) {</span>
|
||||||
|
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">download.file</span>(<span class="st">"https://zenodo.org/record/6395804/files/infuse_dist_lyr_2011_simplified_100m_buildings_overlay_simplified.gpkg"</span>, <span class="at">destfile =</span> <span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg"</span>))}</span>
|
||||||
|
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>local_authorities_buildings_clip <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stdout">
|
||||||
|
<pre><code>Reading layer `infuse_dist_lyr_2011_simplified_100_buildings_overlay_simplified' from data source `/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/data/infuse_dist_lyr_2011_simplified_100m_buildings_simplified.gpkg'
|
||||||
|
using driver `GPKG'
|
||||||
|
Simple feature collection with 403 features and 0 fields
|
||||||
|
Geometry type: MULTIPOLYGON
|
||||||
|
Dimension: XY
|
||||||
|
Bounding box: xmin: -69.1254 ymin: 5524.797 xmax: 655986.4 ymax: 1219597
|
||||||
|
Projected CRS: OSGB36 / British National Grid</code></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p>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 <code>geom_sf()</code> to plot the contents of a geospatial data file with polygons which is loaded as a <code>simplefeature</code> in R.</p>
|
||||||
|
<div class="cell">
|
||||||
|
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_countries) <span class="sc">+</span></span>
|
||||||
|
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output-display">
|
||||||
|
<p><img src="chapter_3_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid" width="672"></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section id="load-in-ordnance-survey-openmap-points-data" class="level1" data-number="6">
|
||||||
|
<h1 data-number="6"><span class="header-section-number">6</span> Load in Ordnance Survey OpenMap Points Data</h1>
|
||||||
|
<div class="cell">
|
||||||
|
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Note: for more advanced reproducible scripts which demonstrate how these data surces have been </span></span>
|
||||||
|
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="co"># obtained, see the companion cookbook here: https://github.com/kidwellj/hacking_religion_cookbook/blob/main/ordnance_survey.R</span></span>
|
||||||
|
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a>os_openmap_pow <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"os_openmap_pow.gpkg"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stdout">
|
||||||
|
<pre><code>Reading layer `os_openmap_pow' from data source
|
||||||
|
`/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/data/os_openmap_pow.gpkg'
|
||||||
|
using driver `GPKG'
|
||||||
|
Simple feature collection with 48759 features and 5 fields
|
||||||
|
Geometry type: POLYGON
|
||||||
|
Dimension: XY
|
||||||
|
Bounding box: xmin: 64594.12 ymin: 8287.54 xmax: 655238.1 ymax: 1214662
|
||||||
|
Projected CRS: OSGB36 / British National Grid</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(os_openmap_pow) <span class="sc">+</span></span>
|
||||||
|
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output-display">
|
||||||
|
<p><img src="chapter_3_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid" width="672"></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p>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.</p>
|
||||||
|
<p>Let’s use that data we’ve just loaded to make our first map:</p>
|
||||||
|
<div class="cell">
|
||||||
|
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate choropleth map of respondent locations</span></span>
|
||||||
|
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="co"># using temporary palette here so that 0s are white</span></span>
|
||||||
|
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tmap) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||||
|
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a><span class="co"># palette <- c(white, "#a8ddb5", "#43a2ca")</span></span>
|
||||||
|
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a>map1 <span class="ot"><-</span> <span class="fu">tm_shape</span>(local_authorities) <span class="sc">+</span> </span>
|
||||||
|
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a><span class="co"># tm_fill(col = "surveys_count", , palette = palette, title = "Concentration of survey respondents") +</span></span>
|
||||||
|
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">tm_borders</span>(<span class="at">alpha=</span>.<span class="dv">5</span>, <span class="at">lwd=</span><span class="fl">0.1</span>) <span class="sc">+</span></span>
|
||||||
|
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># for intermediate polygon geometries</span></span>
|
||||||
|
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># tm_shape(local_authorities) +</span></span>
|
||||||
|
<span id="cb15-10"><a href="#cb15-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># tm_borders(lwd=0.6) +</span></span>
|
||||||
|
<span id="cb15-11"><a href="#cb15-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># for dots from original dataset</span></span>
|
||||||
|
<span id="cb15-12"><a href="#cb15-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># tm_dots("red", size = .05, alpha = .4) +</span></span>
|
||||||
|
<span id="cb15-13"><a href="#cb15-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">tm_scale_bar</span>(<span class="at">position =</span> <span class="fu">c</span>(<span class="st">"right"</span>, <span class="st">"bottom"</span>)) <span class="sc">+</span></span>
|
||||||
|
<span id="cb15-14"><a href="#cb15-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">tm_style</span>(<span class="st">"gray"</span>) <span class="sc">+</span></span>
|
||||||
|
<span id="cb15-15"><a href="#cb15-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">tm_credits</span>(<span class="st">"Data: UK Data Service (OGL)</span><span class="sc">\n</span><span class="st">& Jeremy H. Kidwell,</span><span class="sc">\n</span><span class="st">Graphic is CC-by-SA 4.0"</span>, </span>
|
||||||
|
<span id="cb15-16"><a href="#cb15-16" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="fl">0.4</span>, </span>
|
||||||
|
<span id="cb15-17"><a href="#cb15-17" aria-hidden="true" tabindex="-1"></a> <span class="at">position =</span> <span class="fu">c</span>(<span class="st">"left"</span>, <span class="st">"bottom"</span>),</span>
|
||||||
|
<span id="cb15-18"><a href="#cb15-18" aria-hidden="true" tabindex="-1"></a> <span class="at">just =</span> <span class="fu">c</span>(<span class="st">"left"</span>, <span class="st">"bottom"</span>),</span>
|
||||||
|
<span id="cb15-19"><a href="#cb15-19" aria-hidden="true" tabindex="-1"></a> <span class="at">align =</span> <span class="st">"left"</span>) <span class="sc">+</span></span>
|
||||||
|
<span id="cb15-20"><a href="#cb15-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">tm_layout</span>(<span class="at">asp =</span> <span class="cn">NA</span>,</span>
|
||||||
|
<span id="cb15-21"><a href="#cb15-21" aria-hidden="true" tabindex="-1"></a> <span class="at">frame =</span> <span class="cn">FALSE</span>, </span>
|
||||||
|
<span id="cb15-22"><a href="#cb15-22" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Figure 1a"</span>, </span>
|
||||||
|
<span id="cb15-23"><a href="#cb15-23" aria-hidden="true" tabindex="-1"></a> <span class="at">title.size =</span> .<span class="dv">7</span>,</span>
|
||||||
|
<span id="cb15-24"><a href="#cb15-24" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.title.size =</span> .<span class="dv">7</span>,</span>
|
||||||
|
<span id="cb15-25"><a href="#cb15-25" aria-hidden="true" tabindex="-1"></a> <span class="at">inner.margins =</span> <span class="fu">c</span>(<span class="fl">0.1</span>, <span class="fl">0.1</span>, <span class="fl">0.05</span>, <span class="fl">0.05</span>)</span>
|
||||||
|
<span id="cb15-26"><a href="#cb15-26" aria-hidden="true" tabindex="-1"></a> )</span>
|
||||||
|
<span id="cb15-27"><a href="#cb15-27" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb15-28"><a href="#cb15-28" aria-hidden="true" tabindex="-1"></a>map1</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output-display">
|
||||||
|
<p><img src="chapter_3_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid" width="672"></p>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># save image</span></span>
|
||||||
|
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="fu">tmap_save</span>(map1, <span class="fu">here</span>(<span class="st">"figures"</span>, <span class="st">"map.png"</span>), <span class="at">width=</span><span class="dv">1920</span>, <span class="at">height=</span><span class="dv">1080</span>, <span class="at">asp=</span><span class="dv">0</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stderr">
|
||||||
|
<pre><code>Map saved to /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/figures/map.png</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="cell-output cell-output-stderr">
|
||||||
|
<pre><code>Resolution: 1920 by 1080 pixels</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="cell-output cell-output-stderr">
|
||||||
|
<pre><code>Size: 6.4 by 3.6 inches (300 dpi)</code></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="cell">
|
||||||
|
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co"># subsetting ordnance survey openmap data for measuring clusters and proximity</span></span>
|
||||||
|
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>os_openmap_important_buildings <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="fu">here</span>(<span class="st">"data"</span>, <span class="st">"os_openmap_important_buildings.gpkg"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
<div class="cell-output cell-output-stdout">
|
||||||
|
<pre><code>Reading layer `important_buildings' from data source
|
||||||
|
`/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion/data/os_openmap_important_buildings.gpkg'
|
||||||
|
using driver `GPKG'
|
||||||
|
Simple feature collection with 229800 features and 5 fields
|
||||||
|
Geometry type: POLYGON
|
||||||
|
Dimension: XY
|
||||||
|
Bounding box: xmin: 64594.12 ymin: 8125.44 xmax: 655500.5 ymax: 1214662
|
||||||
|
Projected CRS: OSGB36 / British National Grid</code></pre>
|
||||||
|
</div>
|
||||||
|
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="co"># add pubs, check_cashing, pawnbrokers, SSSI</span></span>
|
||||||
|
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="do">## subsets</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
</div>
|
||||||
|
<div class="cell">
|
||||||
|
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="co"># OSM data</span></span>
|
||||||
|
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Note: for more advanced reproducible scripts which demonstrate how these data surces have been </span></span>
|
||||||
|
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a><span class="co"># obtained, see the companion cookbook here: https://github.com/kidwellj/hacking_religion_cookbook/blob/main/ordnance_survey.R</span></span>
|
||||||
|
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||||
|
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="co"># osm_uk_points <- st_read(system.file(here("data", "pow_osm.gpkg", package = "spData")))</span></span>
|
||||||
|
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a><span class="co"># vector_filepath = system.file("data/osm-gb-2018Aug29_pow_osm.pbf", package = "sf")</span></span>
|
||||||
|
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a><span class="co"># osm_uk_points = st_read(vector_filepath)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||||
|
</div>
|
||||||
<p>Guides to geographies: https://rconsortium.github.io/censusguide/ https://ocsi.uk/2019/03/18/lsoas-leps-and-lookups-a-beginners-guide-to-statistical-geographies/</p>
|
<p>Guides to geographies: https://rconsortium.github.io/censusguide/ https://ocsi.uk/2019/03/18/lsoas-leps-and-lookups-a-beginners-guide-to-statistical-geographies/</p>
|
||||||
<p>Extact places of worship from Ordnance survey open data set Calculate proximity to pubs</p>
|
<p>Extact places of worship from Ordnance survey open data set Calculate proximity to pubs</p>
|
||||||
|
</section>
|
||||||
<section id="references" class="level1 unnumbered">
|
<section id="references" class="level1 unnumbered">
|
||||||
<h1 class="unnumbered">References</h1>
|
<h1 class="unnumbered">References</h1>
|
||||||
<div id="refs" role="list" style="display: none">
|
<div id="refs" role="list" style="display: none">
|
||||||
|
|
|
@ -13,9 +13,21 @@ In the example below, we're going to read in data from a comma separated value f
|
||||||
#| include: true
|
#| include: true
|
||||||
#| label: fig-polar
|
#| label: fig-polar
|
||||||
setwd("/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion")
|
setwd("/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion")
|
||||||
library(here) # much better way to manage working paths in R across multiple instances
|
library(here) |> suppressPackageStartupMessages()
|
||||||
library(tidyverse)
|
library(tidyverse) |> suppressPackageStartupMessages()
|
||||||
here::i_am("chapter_1.qmd")
|
here::i_am("chapter_1.qmd")
|
||||||
|
|
||||||
|
# Set up local workspace:
|
||||||
|
if (dir.exists("data") == FALSE) {
|
||||||
|
dir.create("data")
|
||||||
|
}
|
||||||
|
if (dir.exists("figures") == FALSE) {
|
||||||
|
dir.create("figures")
|
||||||
|
}
|
||||||
|
if (dir.exists("derivedData") == FALSE) {
|
||||||
|
dir.create("derivedData")
|
||||||
|
}
|
||||||
|
|
||||||
uk_census_2021_religion <- read.csv(here("example_data", "census2021-ts030-rgn.csv"))
|
uk_census_2021_religion <- read.csv(here("example_data", "census2021-ts030-rgn.csv"))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -196,27 +208,14 @@ One element of R data analysis that can get really interesting is working with m
|
||||||
|
|
||||||
For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It's worth noting that if you're not in the UK, there are similar options for other countries. Nearly every R textbook I've ever seen works with USA census data, so you'll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.
|
For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It's worth noting that if you're not in the UK, there are similar options for other countries. Nearly every R textbook I've ever seen works with USA census data, so you'll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.
|
||||||
|
|
||||||
Here's the process to identify a dataset within the nomis platform:
|
If you want to draw some data from the nomis platform yourself in R, have a look at the [companion cookbook repository](https://github.com/kidwellj/hacking_religion_cookbook/blob/main/nomis.R).
|
||||||
|
|
||||||
```{r}
|
|
||||||
# Process to explore nomis() data for specific datasets
|
|
||||||
library(nomisr)
|
|
||||||
# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
|
|
||||||
#religion_search <- nomis_search(name = "*Religion*")
|
|
||||||
#religion_measures <- nomis_get_metadata("ST104", "measures")
|
|
||||||
#tibble::glimpse(religion_measures)
|
|
||||||
#religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE")
|
|
||||||
```
|
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
library(nomisr)
|
|
||||||
# Get table of Census 2011 religion data from nomis
|
# Get table of Census 2011 religion data from nomis
|
||||||
# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
|
# Note: for reproducible code used to generate the dataset used in the book, see the cookbook here:
|
||||||
#z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301))
|
|
||||||
#saveRDS(z, file = "z.rds")
|
|
||||||
z <- readRDS(file = (here("example_data", "z.rds")))
|
z <- readRDS(file = (here("example_data", "z.rds")))
|
||||||
|
|
||||||
# Filter down to simplified dataset with England / Wales and percentages without totals
|
# Filter down to simplified dataset with England / Wales and percentages without totals
|
||||||
|
@ -228,10 +227,6 @@ plot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE)
|
||||||
# ggsave(filename = "plot.png", plot = plot1)
|
# ggsave(filename = "plot.png", plot = plot1)
|
||||||
|
|
||||||
# grab daata from nomis for 2001 census religion / ethnicity
|
# grab daata from nomis for 2001 census religion / ethnicity
|
||||||
|
|
||||||
# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
|
|
||||||
#z0 <- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100))
|
|
||||||
#saveRDS(z0, file = "z0.rds")
|
|
||||||
z0 <- readRDS(file = (here("example_data", "z0.rds")))
|
z0 <- readRDS(file = (here("example_data", "z0.rds")))
|
||||||
|
|
||||||
# select relevant columns
|
# select relevant columns
|
||||||
|
@ -242,9 +237,6 @@ uk_census_2001_religion_ethnicity <- filter(uk_census_2001_religion_ethnicity, G
|
||||||
uk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% filter(grepl('Total', C_ETHHUK11_NAME))
|
uk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% filter(grepl('Total', C_ETHHUK11_NAME))
|
||||||
|
|
||||||
# grab data from nomis for 2011 census religion / ethnicity table
|
# grab data from nomis for 2011 census religion / ethnicity table
|
||||||
# commenting out nomis_get temporarily until I can get renv working properly here
|
|
||||||
#z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100))
|
|
||||||
#saveRDS(z1, file = "z1.rds")
|
|
||||||
z1 <- readRDS(file = (here("example_data", "z1.rds")))
|
z1 <- readRDS(file = (here("example_data", "z1.rds")))
|
||||||
|
|
||||||
# select relevant columns
|
# select relevant columns
|
||||||
|
@ -255,8 +247,6 @@ uk_census_2011_religion_ethnicity <- filter(uk_census_2011_religion_ethnicity, G
|
||||||
uk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% filter(grepl('Total', C_ETHPUK11_NAME))
|
uk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% filter(grepl('Total', C_ETHPUK11_NAME))
|
||||||
|
|
||||||
# grab data from nomis for 2021 census religion / ethnicity table
|
# grab data from nomis for 2021 census religion / ethnicity table
|
||||||
#z2 <- nomis_get_data(id = "NM_2131_1", time = "latest", geography = "TYPE499", measures=c(20100))
|
|
||||||
#saveRDS(z2, file = "z2.rds")
|
|
||||||
z2 <- readRDS(file = (here("example_data", "z2.rds")))
|
z2 <- readRDS(file = (here("example_data", "z2.rds")))
|
||||||
|
|
||||||
# select relevant columns
|
# select relevant columns
|
||||||
|
@ -355,7 +345,7 @@ ggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion,
|
||||||
There are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn't really very easy to read. You can use the very powerful and flexible `scales()` library to bring in some more readable formatting of numbers in a variety of places in R including in ggplot visualizations.
|
There are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn't really very easy to read. You can use the very powerful and flexible `scales()` library to bring in some more readable formatting of numbers in a variety of places in R including in ggplot visualizations.
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
library(scales)
|
library(scales) |> suppressPackageStartupMessages()
|
||||||
ggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion, y=Value)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~Ethnicity, ncol = 2) + scale_fill_brewer(palette = "Set1") + scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6), breaks = breaks_extended(8)) + ggtitle("Religious Affiliation in the 2001-2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
|
ggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion, y=Value)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~Ethnicity, ncol = 2) + scale_fill_brewer(palette = "Set1") + scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6), breaks = breaks_extended(8)) + ggtitle("Religious Affiliation in the 2001-2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
|
||||||
|
|
||||||
# https://ggplot2-book.org/scales-position#sec-position-continuous-breaks
|
# https://ggplot2-book.org/scales-position#sec-position-continuous-breaks
|
||||||
|
|
|
@ -1,10 +1,147 @@
|
||||||
# Mapping churches: geospatial data science
|
# Mapping churches: geospatial data science
|
||||||
|
|
||||||
|
Until recently, most data science books didn't have a section on geospatial data. It was considered a specialist form of research best left to GIS technicians who tended to use proprietary tools like ArcGIS. This has changed significantly in the past five years, but you'll still be hard pressed to find an introduction to the subject which strays very far from a few simple data sets (mostly of the USA) and relatively uncomplicated geospatial operations. I actually first began learning R, back in 2013, right when open source geospatial research tools were beginning to be developed with quite a lot more energy and geospatial data is my personal favourite data science playground, so in this book we're going to go much deeper than is usual. There are also good reasons to take things a few steps further in the particular forms of data and inquiry that religion takes us into.
|
||||||
|
|
||||||
|
Recommend https://r-spatial.org/book/
|
||||||
|
|
||||||
|
Geospatial data is, in the most basic form, working with maps. This means that most of your data can be a quite simple dataframe, e.g. just a list of names or categories associated with a set of X and Y coordinates. Once you have a set of items, however, things get interesting very quickly, as you can layer data sets on top of one another. We're going to begin this chapter by developing a geolocated data set of churches in the UK. This information is readily and freely available online thanks to the UK Ordnance Survey, a quasi-governmental agency which maintains the various (now digital) maps of Britain. Lucky for us, the Ordnance Survey has an open data product that anyone can use!
|
||||||
|
|
||||||
|
Before we begin, there are some key things we should note about geospatial data. Geospatial data tends to fall into one of two kinds: points and polygons. Points can be any kind of feature: a house, a church, a pub, someone's favourite ancient oak tree, or some kind of sacred relic. Polygons tend to be associated with wider areas, and as such can be used to describe large features, e.g. an Ocean, a local authority, or a mountain, or also demographic features, like a census Output Area with associated census summaries. Points are very simple data representations, an X and Y coordinate. Polygons are much more complex, often containing dozens or even thousands of points.
|
||||||
|
|
||||||
|
The most complex aspect of point data relates to the ways that coordinates are encoded, as they will aways need to be associated with a coordinate reference system (CRS) which describes how they are situated with respect to the planet earth. The most common CRS is the WGS, though for our data sets we'll also come into contact with the BGS, a specifically British coordinate reference system. There are dozens of CRS, usually mapping onto a specific geographical region. Bearing in mind the way that you need to use a CRS to understand how coordinates can be associated with specific parts of the earth, you can see how this is a bit like survey data, where you also need a "codebook" to understand what the specific response values map onto, e.g. a "1" means "strongly agree" and so on. We also saw, in a previous chapter, how some forms of data have the codebook already baked into the factor data, simplifying the process of interpreting the data. In a similar way, some types of geospatial data sets can also come with CRS "baked in" while we'll need to define CRS for other types. Here are some of the most common types of geospatial data files:
|
||||||
|
|
||||||
|
CSV: "comma separated values" a plain text file containing various coordinates
|
||||||
|
Shapefile: a legacy file format, often still in use, but being replaced by others for a variety of good reasons. For more on this see [http://switchfromshapefile.org/]
|
||||||
|
Geopackage: one of the more recent ways of packaging up geospatial data. Geopackages can contain a wide variety of different data and are easily portable.
|
||||||
|
GeoJSON: a file format commonly used in other forms of coding, the "JSON" (an acronym for JavaScript Object Notation) is meant to be easily interchangeable across various platforms. GeoJSON is an augmented version of JSON data with coordinates added in.
|
||||||
|
|
||||||
|
Now that you have a sense of some of the basic aspects of geospatial data, let's dive in and do a bit of learning in action.
|
||||||
|
|
||||||
|
# Administrative shapes - the UK
|
||||||
|
|
||||||
|
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}
|
||||||
|
library(sf) |> suppressPackageStartupMessages()
|
||||||
|
library(here) |> suppressPackageStartupMessages()
|
||||||
|
library(tidyverse)
|
||||||
|
# better video device, more accurate and faster rendering, esp. on macos. Also should enable system fonts for display
|
||||||
|
library(ragg) |> suppressPackageStartupMessages()
|
||||||
|
|
||||||
|
setwd("/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion")
|
||||||
|
here::i_am("chapter_3.qmd")
|
||||||
|
|
||||||
|
# Download administrative boundaries for whole UK at country level
|
||||||
|
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"))
|
||||||
|
|
||||||
|
# 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"))
|
||||||
|
|
||||||
|
# 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"))
|
||||||
|
|
||||||
|
# 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"))
|
||||||
|
```
|
||||||
|
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()
|
||||||
|
```
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Let's use that data we've just loaded to make our first map:
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
# Generate choropleth map of respondent locations
|
||||||
|
# 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) +
|
||||||
|
# for intermediate polygon geometries
|
||||||
|
# tm_shape(local_authorities) +
|
||||||
|
# tm_borders(lwd=0.6) +
|
||||||
|
# for dots from original dataset
|
||||||
|
# tm_dots("red", size = .05, alpha = .4) +
|
||||||
|
tm_scale_bar(position = c("right", "bottom")) +
|
||||||
|
tm_style("gray") +
|
||||||
|
tm_credits("Data: UK Data Service (OGL)\n& Jeremy H. Kidwell,\nGraphic is CC-by-SA 4.0",
|
||||||
|
size = 0.4,
|
||||||
|
position = c("left", "bottom"),
|
||||||
|
just = c("left", "bottom"),
|
||||||
|
align = "left") +
|
||||||
|
tm_layout(asp = NA,
|
||||||
|
frame = FALSE,
|
||||||
|
title = "Figure 1a",
|
||||||
|
title.size = .7,
|
||||||
|
legend.title.size = .7,
|
||||||
|
inner.margins = c(0.1, 0.1, 0.05, 0.05)
|
||||||
|
)
|
||||||
|
|
||||||
|
map1
|
||||||
|
|
||||||
|
# save image
|
||||||
|
tmap_save(map1, here("figures", "map.png"), width=1920, height=1080, asp=0)
|
||||||
|
```
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
# subsetting ordnance survey openmap data for measuring clusters and proximity
|
||||||
|
|
||||||
|
os_openmap_important_buildings <- st_read(here("data", "os_openmap_important_buildings.gpkg"))
|
||||||
|
|
||||||
|
# add pubs, check_cashing, pawnbrokers, SSSI
|
||||||
|
## subsets
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
```{r}
|
||||||
|
# OSM data
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
|
# osm_uk_points <- st_read(system.file(here("data", "pow_osm.gpkg", package = "spData")))
|
||||||
|
# vector_filepath = system.file("data/osm-gb-2018Aug29_pow_osm.pbf", package = "sf")
|
||||||
|
# osm_uk_points = st_read(vector_filepath)
|
||||||
|
```
|
||||||
|
|
||||||
Guides to geographies:
|
Guides to geographies:
|
||||||
https://rconsortium.github.io/censusguide/
|
https://rconsortium.github.io/censusguide/
|
||||||
https://ocsi.uk/2019/03/18/lsoas-leps-and-lookups-a-beginners-guide-to-statistical-geographies/
|
https://ocsi.uk/2019/03/18/lsoas-leps-and-lookups-a-beginners-guide-to-statistical-geographies/
|
||||||
|
|
||||||
Extact places of worship from Ordnance survey open data set
|
|
||||||
Calculate proximity to pubs
|
Calculate proximity to pubs
|
||||||
|
|
||||||
# References {.unnumbered}
|
# References {.unnumbered}
|
||||||
|
|