updated ch1

This commit is contained in:
Jeremy Kidwell 2023-10-07 19:01:40 +01:00
parent 8ad4dca354
commit 34f7797b2a
7 changed files with 256 additions and 73 deletions

View file

@ -579,7 +579,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
<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-12" data-target-annotation="2">2</dt>
<dd> <dd>
<span data-code-annotation="2" data-code-cell="annotated-cell-12" data-code-lines="1">Well re-order the column by size.</span> <span data-code-cell="annotated-cell-12" data-code-annotation="2" data-code-lines="1">Well re-order the column by size.</span>
</dd> </dd>
</dl> </dl>
</div> </div>
@ -602,19 +602,19 @@ i Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all
<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-13" data-target-annotation="1">1</dt>
<dd> <dd>
<span data-code-annotation="1" data-code-cell="annotated-cell-13" 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-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>
</dd> </dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="2">2</dt> <dt data-target-cell="annotated-cell-13" data-target-annotation="2">2</dt>
<dd> <dd>
<span data-code-annotation="2" data-code-cell="annotated-cell-13" 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 &lt;- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that youll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals &lt;- as.data.frame(uk_census_2021_religion_totals)</code></span> <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 &lt;- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that youll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals &lt;- 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-13" data-target-annotation="3">3</dt>
<dd> <dd>
<span data-code-annotation="3" data-code-cell="annotated-cell-13" 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-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>
</dd> </dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="4">4</dt> <dt data-target-cell="annotated-cell-13" data-target-annotation="4">4</dt>
<dd> <dd>
<span data-code-annotation="4" data-code-cell="annotated-cell-13" data-code-lines="5">Now plot it out and have a look!</span> <span data-code-cell="annotated-cell-13" data-code-annotation="4" data-code-lines="5">Now plot it out and have a look!</span>
</dd> </dd>
</dl> </dl>
</div> </div>
@ -641,6 +641,7 @@ i Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all
</div> </div>
<p>If youre looking closely, you will notice that Ive added two elements to our previous ggplot. Ive asked ggplot to fill in the columns with reference to the <code>dataset</code> column weve just created. Then Ive also asked ggplot to alter the <code>position="dodge"</code> which places bars side by side rather than stacked on top of one another. You can give it a try without this instruction to see how this works. We will use stacked bars in a later chapter, so remember this feature.</p> <p>If youre looking closely, you will notice that Ive added two elements to our previous ggplot. Ive asked ggplot to fill in the columns with reference to the <code>dataset</code> column weve just created. Then Ive also asked ggplot to alter the <code>position="dodge"</code> which places bars side by side rather than stacked on top of one another. You can give it a try without this instruction to see how this works. We will use stacked bars in a later chapter, so remember this feature.</p>
<p>If you inspect our chart, you can see that were getting closer, but its 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 were getting closer, but its 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="">Its 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 wont 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">&lt;-</span> uk_census_2021_religion_totals <span class="sc">%&gt;%</span> </span> <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">&lt;-</span> uk_census_2021_religion_totals <span class="sc">%&gt;%</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-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>
@ -681,7 +682,9 @@ i Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all
</section> </section>
<section id="is-your-chart-accurate-telling-the-truth-in-data-science" class="level2" data-number="2.5"> <section id="is-your-chart-accurate-telling-the-truth-in-data-science" class="level2" data-number="2.5">
<h2 data-number="2.5" class="anchored" data-anchor-id="is-your-chart-accurate-telling-the-truth-in-data-science"><span class="header-section-number">2.5</span> Is your chart accurate? Telling the truth in data science</h2> <h2 data-number="2.5" class="anchored" data-anchor-id="is-your-chart-accurate-telling-the-truth-in-data-science"><span class="header-section-number">2.5</span> Is your chart accurate? Telling the truth in data science</h2>
<p>There is some technical work yet to be done fine-tuning the visualisation of our chart here. But Id like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. Its possible (though perhaps unlikely) that all those non-responses were Sikh respondents who felt uncomfortable identifying themselves on such a survey. If even half of the non-responses were of this nature, this would dramatically shift the results especially in comparison to other minority groups. So there is some work for us to do here in representing non-response as a category on the census. But its equally possible that someone might feel uncertain when answering, but nonetheless land on a particular decision marking “Christian” when they wondered if they should instead tick “no religion. Some surveys attempt to capture uncertainty in this way, asking respondents to mark how confident they are about their answers, but the census hasnt capture this so we simply dont know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This hasnt been done on the census (which would have been quite hard work given that most of the instruments were printed hard copies!), so again, we cant really be sure if those answers given are stable. 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, its 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>There is some technical work yet to be done fine-tuning the visualisation of our chart here. But Id like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. Its possible (though perhaps unlikely) that all those non-responses were Sikh respondents who felt uncomfortable identifying themselves on such a survey. If even half of the non-responses were of this nature, this would dramatically shift the results especially in comparison to other minority groups. So there is some work for us to do here in representing non-response as a category on the census.</p>
<p>Its equally possible that someone might feel uncertain when answering, but nonetheless land on a particular decision marking “Christian” when they wondered if they should instead tick “no religion. Some surveys attempt to capture uncertainty in this way, asking respondents to mark how confident they are about their answers, but the census hasnt capture this so we simply dont know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This hasnt been done on the census (which would have been quite hard work given that most of the instruments were printed hard copies!), so again, we cant really be sure if those answers given are stable.</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, its 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 dont 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 dont 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="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>
@ -699,69 +702,216 @@ i Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all
<section id="multifactor-visualisation" class="level2" data-number="2.7"> <section id="multifactor-visualisation" class="level2" data-number="2.7">
<h2 data-number="2.7" class="anchored" data-anchor-id="multifactor-visualisation"><span class="header-section-number">2.7</span> Multifactor Visualisation</h2> <h2 data-number="2.7" class="anchored" data-anchor-id="multifactor-visualisation"><span class="header-section-number">2.7</span> Multifactor Visualisation</h2>
<p>One element of R data analysis that can get really interesting is working with multiple variables. Above weve looked at the breakdown of religious affiliation across the whole of England and Wales (Scotland operates an independent census), and by placing this data alongside a specific region, weve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantative variable (also known as bivariate data) into the mix, however can also generate a lot more information and we have to think about visualising it in different ways which can still communicate with visual clarity in spite of the additional visual noise which is inevitable with enhanced complexity. Lets have a look at the way that religion in England and Wales breaks down by ethnicity.</p> <p>One element of R data analysis that can get really interesting is working with multiple variables. Above weve looked at the breakdown of religious affiliation across the whole of England and Wales (Scotland operates an independent census), and by placing this data alongside a specific region, weve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantative variable (also known as bivariate data) into the mix, however can also generate a lot more information and we have to think about visualising it in different ways which can still communicate with visual clarity in spite of the additional visual noise which is inevitable with enhanced complexity. Lets have a look at the way that religion in England and Wales breaks down by ethnicity.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
What is Nomis?
</div>
</div>
<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. Its worth noting that if youre not in the UK, there are similar options for other countries. Nearly every R textbook Ive ever seen works with USA census data, so youll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.</p>
<p>Heres the process to identify a dataset within the nomis platform:</p>
<div class="cell"> <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="fu">library</span>(nomisr)</span> <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> <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"># Process to explore nomis() data for specific datasets</span></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>religion_search <span class="ot">&lt;-</span> <span class="fu">nomis_search</span>(<span class="at">name =</span> <span class="st">"*Religion*"</span>)</span> <span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a><span class="co">#religion_search &lt;- nomis_search(name = "*Religion*")</span></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a>religion_measures <span class="ot">&lt;-</span> <span class="fu">nomis_get_metadata</span>(<span class="st">"NM_529_1"</span>, <span class="st">"measures"</span>)</span> <span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a><span class="co">#religion_measures &lt;- nomis_get_metadata("ST104", "measures")</span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a>tibble<span class="sc">::</span><span class="fu">glimpse</span>(religion_measures)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div> <span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a><span class="co">#tibble::glimpse(religion_measures)</span></span>
<div class="cell-output cell-output-stdout"> <span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a><span class="co">#religion_geography &lt;- 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>
<pre><code>Rows: 2 </div>
Columns: 3
$ id &lt;chr&gt; "20100", "20301"
$ label.en &lt;chr&gt; "value", "percent"
$ description.en &lt;chr&gt; "value", "percent"</code></pre>
</div>
<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>religion_geography <span class="ot">&lt;-</span> <span class="fu">nomis_get_metadata</span>(<span class="st">"NM_529_1"</span>, <span class="st">"geography"</span>, <span class="st">"TYPE"</span>)</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Get table of Census 2011 religion data from nomis</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a>z <span class="ot">&lt;-</span> <span class="fu">nomis_get_data</span>(<span class="at">id =</span> <span class="st">"NM_529_1"</span>, <span class="at">time =</span> <span class="st">"latest"</span>, <span class="at">geography =</span> <span class="st">"TYPE499"</span>, <span class="at">measures=</span><span class="fu">c</span>(<span class="dv">20301</span>))</span>
<span id="cb25-5"><a href="#cb25-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="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot">&lt;-</span> <span class="fu">filter</span>(z, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&amp;</span> RURAL_URBAN_NAME<span class="sc">==</span><span class="st">"Total"</span> <span class="sc">&amp;</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Drop unnecessary columns</span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot">&lt;-</span> <span class="fu">select</span>(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot results</span></span>
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot">&lt;-</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="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="at">filename =</span> <span class="st">"plot.png"</span>, <span class="at">plot =</span> plot1)</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>Saving 7 x 5 in image</code></pre>
</div>
<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"># grab data from nomis for 2011 census religion / ethnicity table</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>z1 <span class="ot">&lt;-</span> <span class="fu">nomis_get_data</span>(<span class="at">id =</span> <span class="st">"NM_659_1"</span>, <span class="at">time =</span> <span class="st">"latest"</span>, <span class="at">geography =</span> <span class="st">"TYPE499"</span>, <span class="at">measures=</span><span class="fu">c</span>(<span class="dv">20100</span>))</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a><span class="co"># select relevant columns</span></span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicitity <span class="ot">&lt;-</span> <span class="fu">select</span>(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)</span>
<span id="cb27-5"><a href="#cb27-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="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicitity <span class="ot">&lt;-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicitity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&amp;</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span> <span class="sc">&amp;</span> C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Ethnic group"</span>)</span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Simplify data to only include general totals and omit subcategories</span></span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicitity <span class="ot">&lt;-</span> uk_census_2011_religion_ethnicitity <span class="sc">%&gt;%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHPUK11_NAME))</span>
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicitity, <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> </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>
<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="co"># Filter down to simplified dataset with England / Wales and percentages without totals</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><span class="fu">library</span>(nomisr)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicitity_white <span class="ot">&lt;-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicitity, C_ETHPUK11_NAME <span class="sc">==</span> <span class="st">"White: Total"</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="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicitity_nonwhite <span class="ot">&lt;-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicitity, C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"White: Total"</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="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a></span> <span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="co">#z &lt;- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301))</span></span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicitity_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="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a><span class="co">#saveRDS(z, file = "z.rds")</span></span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a>z <span class="ot">&lt;-</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-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a></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="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot">&lt;-</span> <span class="fu">filter</span>(z, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&amp;</span> RURAL_URBAN_NAME<span class="sc">==</span><span class="st">"Total"</span> <span class="sc">&amp;</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</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="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion <span class="ot">&lt;-</span> <span class="fu">select</span>(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)</span>
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot results</span></span>
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot">&lt;-</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-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a><span class="co"># ggsave(filename = "plot.png", plot = plot1)</span></span>
<span id="cb24-15"><a href="#cb24-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="cb24-17"><a href="#cb24-17" aria-hidden="true" tabindex="-1"></a></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="cb24-19"><a href="#cb24-19" aria-hidden="true" tabindex="-1"></a><span class="co">#z0 &lt;- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100))</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="cb24-21"><a href="#cb24-21" aria-hidden="true" tabindex="-1"></a>z0 <span class="ot">&lt;-</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-22"><a href="#cb24-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="cb24-24"><a href="#cb24-24" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot">&lt;-</span> <span class="fu">select</span>(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)</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="cb24-26"><a href="#cb24-26" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot">&lt;-</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">&amp;</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</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="cb24-28"><a href="#cb24-28" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot">&lt;-</span> uk_census_2001_religion_ethnicity <span class="sc">%&gt;%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHHUK11_NAME))</span>
<span id="cb24-29"><a href="#cb24-29" aria-hidden="true" tabindex="-1"></a></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="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="cb24-32"><a href="#cb24-32" aria-hidden="true" tabindex="-1"></a><span class="co">#z1 &lt;- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100))</span></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="cb24-34"><a href="#cb24-34" aria-hidden="true" tabindex="-1"></a>z1 <span class="ot">&lt;-</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-35"><a href="#cb24-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="cb24-37"><a href="#cb24-37" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot">&lt;-</span> <span class="fu">select</span>(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_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="cb24-39"><a href="#cb24-39" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot">&lt;-</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">&amp;</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span> <span class="sc">&amp;</span> C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Ethnic group"</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="cb24-41"><a href="#cb24-41" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot">&lt;-</span> uk_census_2011_religion_ethnicity <span class="sc">%&gt;%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHPUK11_NAME))</span>
<span id="cb24-42"><a href="#cb24-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="cb24-44"><a href="#cb24-44" aria-hidden="true" tabindex="-1"></a><span class="co">#z2 &lt;- 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">&lt;-</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">&lt;-</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">&lt;-</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">&amp;</span> C2021_RELIGION_10_NAME <span class="sc">!=</span> <span class="st">"Total"</span> <span class="sc">&amp;</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">&lt;-</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">&amp;</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Irish"</span> <span class="sc">&amp;</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-18-1.png" class="img-fluid" width="672"></p>
</div> </div>
</div> </div>
<p>This still doesnt quite render with as much visual clarity and communication as Id 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>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="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2011_religion_ethnicitity_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="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>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity_white <span class="ot">&lt;-</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">&lt;-</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="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>
<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-19-1.png" class="img-fluid" width="672"></p>
</div> </div>
</div> </div>
<!-- <p>This still doesnt quite render with as much visual clarity and communication as Id 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>
Reference on callout box syntax here: https://quarto.org/docs/authoring/callouts.html <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="cell-output-display">
<p><img src="chapter_1_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>For our finale chart, Id like to take the faceted chart weve 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. Well draw on some techniques were already developed above using <code>rbind()</code> to connect up each of these charts (after weve 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 isnt consistent from one census to the next and ggplot will struggle to chart things if the terms being used are exactly the same. Well use <code>mutate()</code> again to accomplish this with some slightly different code.</p>
<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>
<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">&lt;-</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">&lt;-</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">&lt;-</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="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="cb27-7"><a href="#cb27-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">&lt;-</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">&lt;-</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">&lt;-</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="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="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot">&lt;-</span> uk_census_2001_religion_ethnicity <span class="sc">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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="cb27-24"><a href="#cb27-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">&lt;-</span> uk_census_2011_religion_ethnicity <span class="sc">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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="cb27-36"><a href="#cb27-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">&lt;-</span> uk_census_2021_religion_ethnicity <span class="sc">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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">%&gt;%</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="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="cb27-48"><a href="#cb27-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="cb27-50"><a href="#cb27-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">&lt;-</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="cb27-53"><a href="#cb27-53" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot">&lt;-</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="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="cb27-56"><a href="#cb27-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">&lt;-</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="cb27-59"><a href="#cb27-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="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>
<div class="cell-output-display">
<p><img src="chapter_1_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>There are a few formatting issues which remain. Our y-axis number labels are in scientific format which isnt 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="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="cell-output cell-output-stderr">
<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">
<p><img src="chapter_1_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid" width="672"></p>
</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>
<p>This chart shows an increase in almost every category, though its a bit hard to read in some cases. However, this information is based on the increase in raw numbers. Its possbile that numbers may be going up, but in some cases the percentage share for a particular category has actually gone down. Lets transform and visualise our data as percentages to see what kind of trends we can actually isolate:</p>
<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">&lt;-</span> uk_census_merged_religion_ethnicity <span class="sc">%&gt;%</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">%&gt;%</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="cb34-4"><a href="#cb34-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>
<div class="cell-output-display">
<p><img src="chapter_1_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid" width="672"></p>
</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>To highlight a few features of this final plot, Ive 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 were looking at trends here and not absolute values, having correspondence across scales isnt important and this makes for something a bit more visually tidy. Ive 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>
<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">&lt;-</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="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>
</div>
</section> </section>
<section id="references" class="level1 unnumbered"> <section id="references" class="level1 unnumbered">
<h1 class="unnumbered">References</h1> <h1 class="unnumbered">References</h1>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 94 KiB

View file

@ -355,15 +355,15 @@ So <em>whos</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-cell="annotated-cell-6" data-code-lines="2" data-code-annotation="1">First we generate new a dataframe with sums per category and</span> <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>
</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-cell="annotated-cell-6" data-code-lines="3" data-code-annotation="2">…sort in descending order</span> <span data-code-annotation="2" data-code-lines="3" data-code-cell="annotated-cell-6">…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-cell="annotated-cell-6" data-code-lines="5" data-code-annotation="3">Then we add new column with percentages based on the sums youve just generated</span> <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 youve just generated</span>
</dd> </dd>
</dl> </dl>
</div> </div>
@ -395,7 +395,25 @@ So <em>whos</em> religious?
<p><img src="chapter_2_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid" width="672"></p> <p><img src="chapter_2_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid" width="672"></p>
</div> </div>
</div> </div>
<p>Add colours Use mutate to put “prefer not to say” at the bottom # Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels</p> <p>Ive added one feature to our chart that wasnt in the bar charts in chapter 1, text labels with the actual value on each bar.</p>
<p>You may be thinking about the plots weve just finished in chapter 1 and wondering how they compare. Lets use the same facet approach that weve just used to render this data in a subsetted way.</p>
<div class="cell">
<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="co"># First we need to add in data on ethnic self-identification from our respondents:</span></span>
<span id="annotated-cell-9-2"><a href="#annotated-cell-9-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">select</span>(climate_experience_data, Q56, Q0)</span>
<span id="annotated-cell-9-3"><a href="#annotated-cell-9-3" aria-hidden="true" tabindex="-1"></a>religious_affiliation_ethnicity <span class="ot">&lt;-</span> <span class="fu">as_tibble</span>(<span class="fu">as_factor</span>(df))</span>
<span id="annotated-cell-9-4"><a href="#annotated-cell-9-4" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(religious_affiliation_ethnicity) <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>)</span>
<span id="annotated-cell-9-5"><a href="#annotated-cell-9-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-9-6"><a href="#annotated-cell-9-6" aria-hidden="true" tabindex="-1"></a>religious_affiliation_ethnicity_sums <span class="ot">&lt;-</span> religious_affiliation_ethnicity <span class="sc">%&gt;%</span> </span>
<span id="annotated-cell-9-7"><a href="#annotated-cell-9-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(Ethnicity) <span class="sc">%&gt;%</span></span>
<span id="annotated-cell-9-8"><a href="#annotated-cell-9-8" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">count</span>(Religion, <span class="at">sort =</span> <span class="cn">TRUE</span>) <span class="sc">%&gt;%</span></span>
<span id="annotated-cell-9-9"><a href="#annotated-cell-9-9" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">Religion =</span> forcats<span class="sc">::</span><span class="fu">fct_rev</span>(forcats<span class="sc">::</span><span class="fu">fct_inorder</span>(Religion)))</span>
<span id="annotated-cell-9-10"><a href="#annotated-cell-9-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-9-11"><a href="#annotated-cell-9-11" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot">&lt;-</span> <span class="fu">ggplot</span>(religious_affiliation_ethnicity_sums, <span class="fu">aes</span>(<span class="at">x =</span> n, <span class="at">y =</span> Religion)) <span class="sc">+</span></span>
<span id="annotated-cell-9-12"><a href="#annotated-cell-9-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</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>
<span id="annotated-cell-9-13"><a href="#annotated-cell-9-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-9-14"><a href="#annotated-cell-9-14" 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>
<p>Use mutate to put “prefer not to say” at the bottom # Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels</p>
</section> </section>
<section id="q56-follow-ups" class="level1" data-number="6"> <section id="q56-follow-ups" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Q56 follow-ups</h1> <h1 data-number="6"><span class="header-section-number">6</span> Q56 follow-ups</h1>

File diff suppressed because one or more lines are too long

View file

@ -201,10 +201,11 @@ Here's the process to identify a dataset within the nomis platform:
```{r} ```{r}
# Process to explore nomis() data for specific datasets # Process to explore nomis() data for specific datasets
library(nomisr) library(nomisr)
religion_search <- nomis_search(name = "*Religion*") # temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
religion_measures <- nomis_get_metadata("ST104", "measures") #religion_search <- nomis_search(name = "*Religion*")
tibble::glimpse(religion_measures) #religion_measures <- nomis_get_metadata("ST104", "measures")
religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE") #tibble::glimpse(religion_measures)
#religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE")
``` ```
::: :::
@ -212,9 +213,12 @@ religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE")
```{r} ```{r}
library(nomisr) library(nomisr)
# Get table of Census 2011 religion data from nomis # Get table of Census 2011 religion data from nomis
z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301)) # temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
#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")))
# Filter down to simplified dataset with England / Wales and percentages without totals # Filter down to simplified dataset with England / Wales and percentages without totals
uk_census_2011_religion <- filter(z, GEOGRAPHY_NAME=="England and Wales" & RURAL_URBAN_NAME=="Total" & C_RELPUK11_NAME != "All categories: Religion") uk_census_2011_religion <- filter(z, GEOGRAPHY_NAME=="England and Wales" & RURAL_URBAN_NAME=="Total" & C_RELPUK11_NAME != "All categories: Religion")
# Drop unnecessary columns # Drop unnecessary columns
@ -225,7 +229,11 @@ plot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE)
# grab daata from nomis for 2001 census religion / ethnicity # grab daata from nomis for 2001 census religion / ethnicity
z0 <- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100)) # 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")))
# select relevant columns # select relevant columns
uk_census_2001_religion_ethnicity <- select(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE) uk_census_2001_religion_ethnicity <- select(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)
# Filter down to simplified dataset with England / Wales and percentages without totals # Filter down to simplified dataset with England / Wales and percentages without totals
@ -234,7 +242,11 @@ 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
z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100)) # 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")))
# select relevant columns # select relevant columns
uk_census_2011_religion_ethnicity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE) uk_census_2011_religion_ethnicity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)
# Filter down to simplified dataset with England / Wales and percentages without totals # Filter down to simplified dataset with England / Wales and percentages without totals
@ -243,7 +255,10 @@ 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)) #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")))
# select relevant columns # select relevant columns
uk_census_2021_religion_ethnicity <- select(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE) uk_census_2021_religion_ethnicity <- select(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)
# Filter down to simplified dataset with England / Wales and percentages without totals # Filter down to simplified dataset with England / Wales and percentages without totals