mirror of
https://github.com/kidwellj/hacking_religion_textbook.git
synced 2025-06-15 16:54:10 +00:00
updated chapters
This commit is contained in:
parent
f8735b6edf
commit
3a657d6b2f
8 changed files with 217 additions and 269 deletions
|
@ -573,7 +573,7 @@ div.csl-indent {
|
|||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-lines="1" data-code-cell="annotated-cell-10" data-code-annotation="2">We’ll re-order the column by size.</span>
|
||||
<span data-code-cell="annotated-cell-10" data-code-lines="1" data-code-annotation="2">We’ll re-order the column by size.</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
@ -596,19 +596,19 @@ div.csl-indent {
|
|||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<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>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="1" 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>
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<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>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="3" 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>
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="3">3</dt>
|
||||
<dd>
|
||||
<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>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="4" 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>
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="4">4</dt>
|
||||
<dd>
|
||||
<span data-code-lines="5" data-code-cell="annotated-cell-11" data-code-annotation="4">Now plot it out and have a look!</span>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="5" data-code-annotation="4">Now plot it out and have a look!</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
@ -712,48 +712,47 @@ What is Nomis?
|
|||
</div>
|
||||
<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="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="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="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></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="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="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Drop unnecessary columns</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="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot results</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="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="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a></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="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="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a></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="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="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="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="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="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="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a></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="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="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a></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="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="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="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="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="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="cb18-32"><a href="#cb18-32" aria-hidden="true" tabindex="-1"></a></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="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="cb18-35"><a href="#cb18-35" aria-hidden="true" tabindex="-1"></a></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="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="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="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="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="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="cb18-42"><a href="#cb18-42" aria-hidden="true" tabindex="-1"></a></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="cb18-2"><a href="#cb18-2" 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-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-4"><a href="#cb18-4" 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-5"><a href="#cb18-5" 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-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Drop unnecessary columns</span></span>
|
||||
<span id="cb18-7"><a href="#cb18-7" 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-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot results</span></span>
|
||||
<span id="cb18-9"><a href="#cb18-9" 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-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a><span class="co"># ggsave(filename = "plot.png", plot = plot1)</span></span>
|
||||
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a><span class="co"># grab daata from nomis for 2001 census religion / ethnicity</span></span>
|
||||
<span id="cb18-13"><a href="#cb18-13" 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-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
||||
<span id="cb18-16"><a href="#cb18-16" 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-17"><a href="#cb18-17" 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-18"><a href="#cb18-18" 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-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
|
||||
<span id="cb18-20"><a href="#cb18-20" 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-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a><span class="co"># grab data from nomis for 2011 census religion / ethnicity table</span></span>
|
||||
<span id="cb18-23"><a href="#cb18-23" 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-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
|
||||
<span id="cb18-26"><a href="#cb18-26" 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-27"><a href="#cb18-27" 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-28"><a href="#cb18-28" 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-29"><a href="#cb18-29" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
|
||||
<span id="cb18-30"><a href="#cb18-30" 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-31"><a href="#cb18-31" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-32"><a href="#cb18-32" aria-hidden="true" tabindex="-1"></a><span class="co"># grab data from nomis for 2021 census religion / ethnicity table</span></span>
|
||||
<span id="cb18-33"><a href="#cb18-33" 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="cb18-34"><a href="#cb18-34" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-35"><a href="#cb18-35" 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>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="cb18-37"><a href="#cb18-37" 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>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="cb18-39"><a href="#cb18-39" 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="cb18-40"><a href="#cb18-40" 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="cb18-41"><a href="#cb18-41" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-42"><a href="#cb18-42" 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">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue