mirror of
https://github.com/kidwellj/hacking_religion_textbook.git
synced 2025-07-01 15:44:08 +00:00
chapter 1 updates to github web hosting
This commit is contained in:
parent
59902f218f
commit
93adab4675
80 changed files with 3008 additions and 197 deletions
|
@ -2,7 +2,7 @@
|
|||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||||
|
||||
<meta charset="utf-8">
|
||||
<meta name="generator" content="quarto-1.4.549">
|
||||
<meta name="generator" content="quarto-1.4.551">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||||
|
||||
|
@ -174,7 +174,7 @@ div.csl-indent {
|
|||
<li class="sidebar-item">
|
||||
<div class="sidebar-item-container">
|
||||
<a href="./chapter_2.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">Getting into the nitty-gritty details</span></span></a>
|
||||
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">Different ways to measure religion using data science</span></span></a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="sidebar-item">
|
||||
|
@ -253,7 +253,8 @@ div.csl-indent {
|
|||
</header>
|
||||
|
||||
|
||||
<p>We’ll get to the good stuff in a moment, but first we need to do a bit of setup. The code provided here is intended to set up your workspace and is also necessary for the <code>quarto</code> application we use to build this book. Quarto is an application which blends together text and blocks of code. You can ignore most of it for now, though if you’re running the code as we go along, you’ll definitely want to include these lines, as they create directories where your files will go as you create charts and extract data below and tells R where to find those files:</p>
|
||||
<p>In this chapter we’re going to do some exciting things with census data. This is a very important dataset, often analysed, but much less frequently with regards to the subject of religion and almost never with the level of granularity you’ll learn to work with over the course of this chapter.</p>
|
||||
<p>We’ll get to the good stuff in a moment, but first we need to do a bit of setup. The code provided here is intended to set up your workspace and is also necessary for the <code>quarto</code> application we use to build this book. If you hadn’t already noticed, this book is also generated by live (and living!) R code. Quarto is an application which blends together text and blocks of code to produce books. You can ignore most of it for now, though if you’re running the code as we go along, you’ll definitely want to include these lines, as they create directories where your files will go as you create charts and extract data below and tells R where to find those files:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">setwd</span>(<span class="st">"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion"</span>)</span>
|
||||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(here) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||
|
@ -423,7 +424,7 @@ div.csl-indent {
|
|||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<p>You can see how I’ve nested the previous command inside the <code>kable</code> command. For reference, in some cases when you’re working with really complex scripts with many different libraries and functions, they may end up with functions that have the same name. You can specify the library where the function is meant to come from by preceding it with :: as we’ve done <code>knitr::</code> above. The same kind of output can be gotten using <code>tail</code>:</p>
|
||||
<p>You can see how I’ve nested the previous command inside the <code>kable</code> command. For reference, in some cases when you’re working with really complex scripts with many different libraries and functions, they may end up with functions that have the same name, and you may unwittingly run a function from the wrong library. You can specify the library where the function is meant to come from by preceding it with :: as we’ve done <code>knitr::</code> above. The same kind of output can be gotten using <code>tail</code> which shows the final lines of a given data object:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(<span class="fu">tail</span>(uk_census_2021_religion))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
|
@ -550,12 +551,14 @@ div.csl-indent {
|
|||
</section>
|
||||
<section id="parsing-and-exploring-your-data" class="level1 page-columns page-full" data-number="5">
|
||||
<h1 data-number="5"><span class="header-section-number">5</span> Parsing and Exploring your data</h1>
|
||||
<p>The first thing you’re going to want to do is to take a smaller subset of a large data set, either by filtering out certain columns or rows. Now let’s say we want to just work with the data from the West Midlands, and we’d like to omit some of the columns. We can choose a specific range of columns using <code>select</code>, like this:</p>
|
||||
<p>You can use the <code>filter</code> command to do this. To give an example, <code>filter</code> can pick a single row in the following way:</p>
|
||||
<p>The first thing you’re going to want to do is to take a smaller subset of a large data set, either by filtering out certain columns or rows. Let’s say we want to just work with the data from the West Midlands and we’d like to omit some of the other columns which relate to different geographic areas. We can choose a specific range of columns using <code>select</code>, like this:</p>
|
||||
<p>You can use the <code>filter</code> command to do this. To give an example, <code>filter</code> can pick a single <em>row</em> in the following way:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion <span class="sc">%>%</span> <span class="fu">filter</span>(geography<span class="sc">==</span><span class="st">"West Midlands"</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Now we’ll use select in a different way to narrow our data to specific columns that are needed (no totals!).</p>
|
||||
<p>In the line above, you’ll see that we’ve created a new object which contains this more specific subset of the original data. You can also overwrite your original object with the new information, and as you go along you’ll need to make decisions about whether to keep many iterations as different objects, or if you want to try and hold onto only the bare essentials.</p>
|
||||
<p>It’s also worth noting that there are only a few rules for naming objects (you can’t have spaces, for one thing), so you’ll want to come up with a specific convention that works for you. I tend to assign a name for each object that indicates the dataset it has come from and then chain on further names using underscore characters which indicate what kind of subset it is. You may want to be careful about letting your names get too long, and find comprehensible ways to abbreviate.</p>
|
||||
<p>Now we’ll use select in a different way to narrow our data to specific <em>columns</em> that are needed (no totals!).</p>
|
||||
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="margin-aside">Some readers will want to pause here and check out Hadley Wickham’s “R For Data Science” book, in the section, <a href="https://r4ds.hadley.nz/data-visualize#introduction">“Data visualisation”</a> to get a fuller explanation of how to explore your data.</span></div></div>
|
||||
<p>In keeping with my goal to demonstrate data science through examples, we’re going to move on to producing some snappy looking charts for this data.</p>
|
||||
</section>
|
||||
|
@ -566,9 +569,10 @@ div.csl-indent {
|
|||
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> uk_census_2021_religion_wmids <span class="sc">%>%</span> <span class="fu">select</span>(no_religion<span class="sc">:</span>no_response)</span>
|
||||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids <span class="ot"><-</span> <span class="fu">gather</span>(uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>There are two basic ways to do visualisations in R. You can work with basic functions in R, often called “base R” or you can work with an alternative library called ggplot:</p>
|
||||
<p>There are two basic ways to do visualisations in R. You can work with basic functions in R, often called “base R” or you can work with an alternative (and extremely popular) library called ggplot which aims to streamline the coding you need to make a chart:</p>
|
||||
<section id="base-r" class="level2" data-number="6.1">
|
||||
<h2 data-number="6.1" class="anchored" data-anchor-id="base-r"><span class="header-section-number">6.1</span> Base R</h2>
|
||||
<p>Here’s the code you can use to create a new data object which contains the information necessary for our chart. I’ve just used the generic name “df” because we won’t hold on to this chart. You’ll also see that I’ve organised the data in descending order using the base R function <code>order()</code>. In the next line, we use the Base R function “barplot” to create a chart.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> uk_census_2021_religion_wmids[<span class="fu">order</span>(uk_census_2021_religion_wmids<span class="sc">$</span>value,<span class="at">decreasing =</span> <span class="cn">TRUE</span>),]</span>
|
||||
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">barplot</span>(<span class="at">height=</span>df<span class="sc">$</span>value, <span class="at">names=</span>df<span class="sc">$</span>key)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
|
@ -583,13 +587,14 @@ div.csl-indent {
|
|||
</section>
|
||||
<section id="ggplot" class="level2 page-columns page-full" data-number="6.2">
|
||||
<h2 data-number="6.2" class="anchored" data-anchor-id="ggplot"><span class="header-section-number">6.2</span> GGPlot</h2>
|
||||
<p>The conventions of GGPlot take a bit of getting used to, but it’s a very powerful tool which will scale to quite complicated charts.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-10"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-10-1"><a href="#annotated-cell-10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_wmids, <span class="fu">aes</span>(<span class="at">x =</span> key, <span class="at">y =</span> value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">stat =</span> <span class="st">"identity"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-annotation">
|
||||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="1" data-code-annotation="2">We’ll re-order the column by size.</span>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="1" data-code-annotation="2">Then we re-order the column by size.</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
@ -609,7 +614,7 @@ div.csl-indent {
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Let’s assume we’re working with a data set that doesn’t include a “totals” column and that we might want to get sums for each column. This is pretty easy to do in R. As you’ll see below, we are going to take the original table, and overwrite it with a new column added:</p>
|
||||
<p>This initial chart doesn’t include a “totals” column, as it isn’t in the data and these plotting tools simply represent whatever data you put into them. It’s nice to have a list of sums for each column, and this is pretty easy to do in R. As you’ll see below, we are going to take the original table, and overwrite it with a new column added:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-12"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-12-1" class="code-annotation-target"><a href="#annotated-cell-12-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion <span class="sc">%>%</span> <span class="fu">select</span>(no_religion<span class="sc">:</span>no_response)</span>
|
||||
<span id="annotated-cell-12-2"><a href="#annotated-cell-12-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals <span class="ot"><-</span> uk_census_2021_religion_totals <span class="sc">%>%</span></span>
|
||||
|
@ -628,7 +633,7 @@ div.csl-indent {
|
|||
</dd>
|
||||
<dt data-target-cell="annotated-cell-12" data-target-annotation="3">3</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-12" data-code-lines="4" data-code-annotation="3">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
|
||||
<span data-code-cell="annotated-cell-12" data-code-lines="4" data-code-annotation="3">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using <code>gather()</code></span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-12" data-target-annotation="4">4</dt>
|
||||
<dd>
|
||||
|
@ -644,11 +649,11 @@ div.csl-indent {
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>You might have noticed that these two dataframes give us somewhat different results. But with data science, it’s much more interesting to compare these two side-by-side in a visualisation. We can join these two dataframes and plot the bars side by side using <code>bind()</code> - which can be done by columns with cbind() and rows using rbind():</p>
|
||||
<p>You might notice that these two dataframes give us somewhat different results. But with data science, it’s much more interesting to compare these two side-by-side in a visualisation. We can join these two dataframes and plot the bars side by side using <code>bind()</code> - which can be done by columns with <code>cbind()</code> and rows using <code>rbind()</code>:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_merged <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Do you notice there’s going to be a problem here? How can we tell one set from the other? We need to add in something idenfiable first! This isn’t too hard to do as we can simply create a new column for each with identifiable information before we bind them:</p>
|
||||
<p>Do you notice there’s going to be a problem here? How can we tell one set from the other? We need to add in something idenfiable first! To do this we can simply create a new column for each with identifiable information before we bind them:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_totals<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"totals"</span>)</span>
|
||||
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_wmids<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"wmids"</span>)</span>
|
||||
|
@ -684,9 +689,9 @@ div.csl-indent {
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now you can see a very rough comparison, which sets bars from the West Midlands data and UK-wide total data side by side for each category. The same principles that we’ve used here can be applied to draw in more data. You could, for example, compare census data from different years, e.g. 2001 2011 and 2021, as we’ll do below. Our use of <code>dplyr::mutate</code> above can be repeated to add an infinite number of further series’ which can be plotted in bar groups.</p>
|
||||
<p>This chart gives us a comparison which sets bars from the West Midlands data and UK-wide total data side by side for each category. The same principles that we’ve used here can be applied to draw in more data. You could, for example, compare census data from different years, e.g. 2001 2011 and 2021, as we’ll do below. Our use of <code>dplyr::mutate</code> above can be repeated to add an infinite number of further series’ which can be plotted in bar groups.</p>
|
||||
<p>We’ll draw this data into comparison with later sets in the next chapter. But the one glaring issue which remains for our chart is that it’s lacking in really any aesthetic refinements. This is where <code>ggplot</code> really shines as a tool as you can add all sorts of things.</p>
|
||||
<p>The <code>ggplot</code> tool basically works by stacking on additional elements using <code>+</code>. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the <code>scale</code> by tacking on <code>scale_fill_brewer</code>. This uses a particular tool (and a personal favourite of mine) called <code>colorbrewer</code>. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.</p>
|
||||
<p>The <code>ggplot</code> tool works by stacking additional elements on to your original plot using <code>+</code>. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the <code>scale</code> by tacking on <code>scale_fill_brewer</code>. This uses a particular tool (and a personal favourite of mine) called <code>colorbrewer</code>. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span>dataset, <span class="at">x=</span>key, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
|
@ -721,22 +726,9 @@ div.csl-indent {
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="telling-the-truth-in-data-science-is-your-chart-accurate" class="level1" data-number="7">
|
||||
<h1 data-number="7"><span class="header-section-number">7</span> Telling the truth in data science: Is your chart accurate?</h1>
|
||||
<p>If you’ve been following along up until this point, you’ll now have produced a fairly complete data visualisation for the UK census. There is some technical work yet to be done fine-tuning the visualisation of our chart here, but I’d like to pause for a moment and consider an ethical question drawn from the principles I outlined in the introduction: is the title of this chart truthful and accurate?</p>
|
||||
<p>On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument, e.g. something like “what is your religious affiliation”. 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. It’s 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>It’s 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, or allowing respondents to choose multiple answers, but the census hasn’t captured this so we simply don’t know. It’s possible that a large portion of respondents in the”Christian” category were hovering between this and another response, and they might shift their answers when responding on a different day or in the context of a particular experience like a good or bad day attending church, or perhaps having just had a conversation with a friend which shifted their thinking.</p>
|
||||
<p>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. If someone were to ask you on a survey “are you hungry” you might say “no,” but if they’d previously asked you a hundred questions about your favourite pizza toppings you might have been primed to think about food and when you arrive at the same question, even at the same time in the day, answer differently. This can be the case for some ethnicity and religion pairings, which we’ll explore a bit more in the next chapter.</p>
|
||||
<p>Given this challenge, some survey instruments randomise the order of questions. This hasn’t 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 can’t really be sure if those answers given are stable in such a way.</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”. One might assume that these are different religions without many doctrinal overlaps, but researchers have found that in actual practice, it’s perfectly possible for people to inhabit two categories which the researcher might assume are opposed.</p>
|
||||
<p>The UK census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Did they feel a bit more Buddhist that day? Or spiritual?</p>
|
||||
<p>Finally, it is interesting for us to consider exactly what it means for a person when they tick a box like this. The census doesn’t specify how one should calculate the basis of your participation. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.</p>
|
||||
<p>What does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive dataset, covering much of the population, and this large sample size does afford some additional validity. 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.</p>
|
||||
<p>So if we are going to fine-tune our visuals to ensure they comport with our hacker principles and speak truthfully, we should also probably do something different with those non-responses:</p>
|
||||
<p>It’s also a bit hard to read our Y-axis labels with everything getting cramped down there, so let’s rotate that text to 180 degrees so those labels are clear:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><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="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><span class="fu">fct_reorder</span>(dataset, value), <span class="at">x=</span><span class="fu">reorder</span>(key,<span class="sc">-</span>value),value, <span class="at">y=</span>perc)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the UK: 2021"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span 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>
|
||||
<figure class="figure">
|
||||
|
@ -746,9 +738,33 @@ div.csl-indent {
|
|||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section id="multifactor-visualisation" class="level1 page-columns page-full" data-number="8">
|
||||
</section>
|
||||
<section id="telling-the-truth-in-data-science-is-your-chart-accurate" class="level1" data-number="7">
|
||||
<h1 data-number="7"><span class="header-section-number">7</span> Telling the truth in data science: Is your chart accurate?</h1>
|
||||
<p>If you’ve been following along up until this point, you’ll have produced a fairly complete data visualisation for the UK census. There is some technical work yet to be done fine-tuning the visualisation of our chart here, but I’d like to pause for a moment and consider an ethical question drawn from the principles I outlined in the introduction: is the title of this chart truthful and accurate?</p>
|
||||
<p>On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument, e.g. something like “what is your religious affiliation”. However, as you will see in the next chapter, other large data sets from the same year which involved a similar question yielded 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. It’s possible (though perhaps unlikely) that all those non-responses were (to pick one random example) Jedi religion practitioners who felt uncomfortable identifying themselves on such a census 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>It’s 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, or allowing respondents to choose multiple answers, but the makers of the census made a specific choice not to capture this so we simply don’t know. It’s possible that a large portion of respondents in the”Christian” category were hovering between this and another response and they might shift their answers when responding on a different day or in the context of a particular experience like a good or bad day attending church, or perhaps having just had a conversation with a friend which shifted their thinking.</p>
|
||||
<p>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. If someone were to ask you on a survey “are you hungry” you might say “no,” but if they’d previously asked you a hundred questions about your favourite pizza toppings you might have been primed to think about food and when you arrive at the same question, even at the same time in the day, your answer would be an enthusiastic “yes”. This can be the case for some ethnicity and religion pairings which may have priming interrelations, which we’ll explore a bit more in the next chapter.</p>
|
||||
<p>Given this challenge, some survey instruments randomise the order of questions. This hasn’t 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 can’t really be sure if those answers given are stable in such a way.</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 do in practice identify with less expected hybrid religious identities as well, such as “Christian” and “Hindu”. One might assume that these are different religions without many doctrinal overlaps, but researchers have found that in actual practice, it’s perfectly possible for some people to inhabit two or more categories which the researcher might assume are opposed.</p>
|
||||
<p>The UK census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Did they feel a bit more Buddhist that day? Or spiritual?</p>
|
||||
<p>Finally, it is interesting for us to consider exactly what it means for a person when they tick a box like this. The census doesn’t specify how one should calculate the basis of your participation. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.</p>
|
||||
<p>What does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive dataset, covering much of the population, and this large sample size does afford some additional validity. 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.</p>
|
||||
<p>So if we are going to fine-tune our visuals to ensure they comport with our hacker principles and speak truthfully, we should also probably do something different with those non-responses:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_merged, <span class="fu">aes</span>(<span class="at">fill=</span><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 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>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section id="multifactor-visualisation" class="level1" data-number="8">
|
||||
<h1 data-number="8"><span class="header-section-number">8</span> Multifactor Visualisation</h1>
|
||||
<p>One element of R data analysis of census datasets that can get really interesting is working with multiple variables. Above we’ve 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, we’ve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantitative variable (also known as bivariate data when you have <em>two</em> variables) 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. Let’s have a look at the way that religion in England and Wales breaks down by ethnicity.</p>
|
||||
<p>One element of R data analysis of census datasets that can get really interesting is working with multiple variables. Above we’ve looked at the breakdown of religious affiliation across the whole of England and Wales (Scotland operates an independent census so we haven’t included it here) and by placing this data alongside a specific region, we’ve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantitative variable (also known as bivariate data when you have <em>two</em> variables) 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. Let’s 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" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
|
||||
<div class="callout-icon-container">
|
||||
|
@ -761,49 +777,57 @@ What is Nomis?
|
|||
</div>
|
||||
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
|
||||
<div class="callout-body-container callout-body">
|
||||
<p>For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It’s worth noting that if you’re not in the UK, there are similar options for other countries. Nearly every R textbook I’ve ever seen works with USA census data, so you’ll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.</p>
|
||||
<p>For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It’s worth noting that if you’re not in the UK, there are similar options for other countries. Nearly every R textbook I’ve ever seen works with USA census data (which is part of the reason I’ve taken the opportunity to work with a different national census dataset here in this book), so you’ll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.</p>
|
||||
<p>If you want to draw some data from the nomis platform yourself in R, have a look at the nomis script in our <a href="https://github.com/kidwellj/hacking_religion_cookbook/blob/main/nomis.R">companion cookbook repository</a>. For now, we’ll provide some data extracts for you to use.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Let’s start by loading in some of the enhanced tables from nomis with the 2021 religion / ethnicity tables:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>nomis_extract_census2021 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"nomis_extract_census2021.rds"</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>nomis_extract_census2021 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"nomis_extract_census2021.rds"</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>I’m hoping that readers of this book will feel free to pause along the way and “hack” the code to explore questions of their own, perhaps in this case probing the NOMIS data for answers to their own questions. If I tidy things up too much, however, you’re likely to be surprised when you get to the real life data sets. So that you can use the code in this book in a reproducible way, I’ve started this exercise with what is a more or less raw dump from NOMIS. This means that the data is a bit messy and needs to be filtered down quite a bit so that it only includes the basic stuff that we’d like to examine for this particular question. The upside of this is that you can modify this code to draw in different columns etc.</p>
|
||||
<div class="cell page-columns page-full">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-22"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-22" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-22-1" class="code-annotation-target"><a href="#annotated-cell-22-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(nomis_extract_census2021, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-22" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-22-2" class="code-annotation-target"><a href="#annotated-cell-22-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C2021_RELIGION_10_NAME <span class="sc">!=</span> <span class="st">"Total"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"Total"</span>)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-22" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-22-3" class="code-annotation-target"><a href="#annotated-cell-22-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: English, Welsh, Scottish, Northern Irish or British"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Irish"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Gypsy or Irish Traveller, Roma or Other White"</span>)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-22" data-target-annotation="4" onclick="event.preventDefault();">4</a><span id="annotated-cell-22-4" class="code-annotation-target"><a href="#annotated-cell-22-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>C2021_ETH_8_NAME, <span class="at">x=</span>C2021_RELIGION_10_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><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-23"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-23-1" class="code-annotation-target"><a href="#annotated-cell-23-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(nomis_extract_census2021, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)</span>
|
||||
<span id="annotated-cell-23-2"><a href="#annotated-cell-23-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-23-3" class="code-annotation-target"><a href="#annotated-cell-23-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C2021_RELIGION_10_NAME <span class="sc">!=</span> <span class="st">"Total"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"Total"</span>)</span>
|
||||
<span id="annotated-cell-23-4"><a href="#annotated-cell-23-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-23-5" class="code-annotation-target"><a href="#annotated-cell-23-5" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: English, Welsh, Scottish, Northern Irish or British"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Irish"</span> <span class="sc">&</span> C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White: Gypsy or Irish Traveller, Roma or Other White"</span>)</span>
|
||||
<span id="annotated-cell-23-6"><a href="#annotated-cell-23-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="annotated-cell-23-7"><a href="#annotated-cell-23-7" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>C2021_ETH_8_NAME, <span class="at">x=</span>C2021_RELIGION_10_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-23-8"><a href="#annotated-cell-23-8" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="annotated-cell-23-9"><a href="#annotated-cell-23-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> </span>
|
||||
<span id="annotated-cell-23-10"><a href="#annotated-cell-23-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> </span>
|
||||
<span id="annotated-cell-23-11"><a href="#annotated-cell-23-11" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="4" onclick="event.preventDefault();">4</a><span id="annotated-cell-23-12" class="code-annotation-target"><a href="#annotated-cell-23-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-annotation">
|
||||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-22" data-target-annotation="1">1</dt>
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-22" data-code-lines="1" data-code-annotation="1">Select relevant columns</span>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="1" data-code-annotation="1">Select relevant columns</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-22" data-target-annotation="2">2</dt>
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-22" data-code-lines="2" data-code-annotation="2">Filter down to simplified dataset with England / Wales and percentages without totals</span>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="3" data-code-annotation="2">Filter down to simplified dataset with England / Wales and percentages without totals</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-22" data-target-annotation="3">3</dt>
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="3">3</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-22" data-code-lines="3" data-code-annotation="3">The 2021 census data includes white sub-groups so we need to omit those</span>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="5" data-code-annotation="3">The 2021 census data includes white sub-groups so we need to omit those</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-22" data-target-annotation="4">4</dt>
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="4">4</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-22" data-code-lines="4" data-code-annotation="4">Let’s plot it out and see how things look!</span>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="12" data-code-annotation="4">Let’s plot it out and see how things look!</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="no-row-height column-margin column-container"><div class="cell-output-display">
|
||||
<div class="cell-output-display">
|
||||
<div>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div></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 <a href="https://en.wikipedia.org/wiki/Logarithm#Probability_theory_and_statistics">logarithmic</a> rather than linear scaling as an option, but this is hard for many general public audiences to appreciate 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="callout callout-style-default callout-note callout-titled">
|
||||
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
|
||||
|
@ -817,41 +841,32 @@ Statistics 101: Logarithmic Visualisation
|
|||
</div>
|
||||
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
|
||||
<div class="callout-body-container callout-body">
|
||||
<p>Content TBD.</p>
|
||||
<p>Usually, when we display data we think of numbers in a linear way, that is, each centimetre of the x-axis on our chart represents the same quantity as the cm above and below it. This is generally a preferred way to display data, and as close to a “common sense” way of showing things as we might get. However, this kind of linear visualisation works best only in cases where the difference between one category on our chart and the next is relatively uniform. This is, for the most part, the case with our charts above. However, we’ve hit another scenario here, the difference between the “White” subcategory and all the others is large enough that those other four categories aren’t really easily perceived on our chart. One way to address this is to leave behind a linear approach to displaying that x-axis data. What if, for example, each step up on our chart didn’t represent the same amount of value, e.g. 10, 20, 30, 40, 50 etc. but instead represented an increase which followed orders of magnitude, so something more like 10, 100, 1000, 10000, etc. That’s the essence of a logarithmic visualisation, which can much more easily display data that has a very large range or with disparities from one category to another.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell page-columns page-full">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-23"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-23-1" class="code-annotation-target"><a href="#annotated-cell-23-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity_white <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">==</span> <span class="st">"White"</span>)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-23-2" class="code-annotation-target"><a href="#annotated-cell-23-2" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White"</span>)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-23" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-23-3" class="code-annotation-target"><a href="#annotated-cell-23-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>C2021_ETH_8_NAME, <span class="at">x=</span>C2021_RELIGION_10_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><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-24"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-24" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-24-1" class="code-annotation-target"><a href="#annotated-cell-24-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity_white <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">==</span> <span class="st">"White"</span>)</span>
|
||||
<span id="annotated-cell-24-2"><a href="#annotated-cell-24-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-24" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-24-3" class="code-annotation-target"><a href="#annotated-cell-24-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME <span class="sc">!=</span> <span class="st">"White"</span>)</span>
|
||||
<span id="annotated-cell-24-4"><a href="#annotated-cell-24-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-24" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-24-5" class="code-annotation-target"><a href="#annotated-cell-24-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>C2021_ETH_8_NAME, <span class="at">x=</span>C2021_RELIGION_10_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><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-annotation">
|
||||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="1">1</dt>
|
||||
<dt data-target-cell="annotated-cell-24" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="1" data-code-annotation="1">Filter down to simplified dataset with England / Wales and percentages without totals</span>
|
||||
<span data-code-cell="annotated-cell-24" data-code-lines="1" data-code-annotation="1">Filter down to simplified dataset with England / Wales and percentages without totals</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="2">2</dt>
|
||||
<dt data-target-cell="annotated-cell-24" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="2" data-code-annotation="2">Filtering with <code>!=</code> allows us to create a subset where that response is excluded</span>
|
||||
<span data-code-cell="annotated-cell-24" data-code-lines="3" data-code-annotation="2">Filtering with <code>!=</code> allows us to create a subset where that response is excluded</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-23" data-target-annotation="3">3</dt>
|
||||
<dt data-target-cell="annotated-cell-24" data-target-annotation="3">3</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-23" data-code-lines="3" data-code-annotation="3">Let’s plot it out and see where we’ve gotten to!</span>
|
||||
<span data-code-cell="annotated-cell-24" data-code-lines="5" data-code-annotation="3">Let’s plot it out and see where we’ve gotten to!</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="no-row-height column-margin column-container"><div class="cell-output-display">
|
||||
<div>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div></div></div>
|
||||
<p>As you’ll notice, this is a bit better, but this still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another. This is just intended to whet you appetite for facetted plots, so I won’t break down all the separate elements in great detail as there are other guides which will walk you through the full details of how to use this technique if you want to do a deep dive. For now, you’ll want to observe that we’ve augmented the <code>ggplot</code> with a new element called <code>facet_wrap</code> which takes the ethnicity data column as the basis for rendering separate charts.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">x=</span>C2021_RELIGION_10_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>C2021_ETH_8_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 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>
|
||||
<figure class="figure">
|
||||
|
@ -860,139 +875,153 @@ Statistics 101: Logarithmic Visualisation
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>As you’ll notice, this is a bit better, but this still doesn’t quite render with as much visual clarity and communication as I’d like. Another approach we can take is to represent each bar as a percentage of the total for that ethnicity subgroup rather than as raw values. We can do this by adding an extra step to our visualisation drawing on the mutate() function which enables us to create a series of groups based on a specific column (e.g. C2021_ETH_8_NAME) and then create an additional column in our dataframe which represents values within each of our groups as percentages of the total rather than raw values:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity_percents <span class="ot"><-</span> uk_census_2021_religion_ethnicity <span class="sc">%>%</span></span>
|
||||
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(C2021_ETH_8_NAME) <span class="sc">%>%</span></span>
|
||||
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Percentage =</span> OBS_VALUE <span class="sc">/</span> <span class="fu">sum</span>(OBS_VALUE) <span class="sc">*</span> <span class="dv">100</span>)</span>
|
||||
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_2021_religion_ethnicity_percents, <span class="fu">aes</span>(<span class="at">fill=</span>C2021_ETH_8_NAME, <span class="at">x=</span>C2021_RELIGION_10_NAME, <span class="at">y=</span>Percentage)) <span class="sc">+</span></span>
|
||||
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> </span>
|
||||
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <span class="sc">+</span> </span>
|
||||
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>As you can see, this gives us a really different sense of representation within each group. Another option we can use here is a technique in R called “faceting” which creates a series of small charts which can be viewed alongside one another. This is just intended to whet you appetite for facetted plots, so I won’t break down all the separate elements in great detail as there are other guides which will walk you through the full details of how to use this technique if you want to do a deep dive. For now, you’ll want to observe that we’ve augmented the <code>ggplot</code> with a new element called <code>facet_wrap</code> which takes the ethnicity data column as the basis for rendering separate charts.</p>
|
||||
<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_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">x=</span>C2021_RELIGION_10_NAME, <span class="at">y=</span>OBS_VALUE)) <span class="sc">+</span> </span>
|
||||
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>C2021_ETH_8_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>
|
||||
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>That’s a bit better! Now we have a much more accessible set of visual information which compares across categories and renders most of the information we’re trying to capture.</p>
|
||||
<p>To take this chart just one step further, I’d like to take the faceted chart we’ve just done and add in totals for the previous two census years (2001 and 2011) so we can see how trends are changing in terms of religious affiliation within ethnic self-identification categories. We’ll draw on some techniques we’re already developed above using <code>rbind()</code> to connect up each of these charts (after we’ve added a column identifying each chart by the census year). We will also need to use one new technique to change the wording of ethnic categories as this isn’t consistent from one census to the next and ggplot will struggle to chart things if the terms being used are exactly the same. We’ll use <code>mutate()</code> again to accomplish this with some slightly different code.</p>
|
||||
<p>First we need to get the tables of Census 2011 and 2001 religion data from nomis:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>nomis_extract_census2001 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"nomis_extract_census2001.rds"</span>)))</span>
|
||||
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>nomis_extract_census2011 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"nomis_extract_census2011.rds"</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>nomis_extract_census2001 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"nomis_extract_census2001.rds"</span>)))</span>
|
||||
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a>nomis_extract_census2011 <span class="ot"><-</span> <span class="fu">readRDS</span>(<span class="at">file =</span> (<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"nomis_extract_census2011.rds"</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Next, as we’ve already done above, we need to filter and tidy the tables:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-26"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-26" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-26-1" class="code-annotation-target"><a href="#annotated-cell-26-1" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(nomis_extract_census2001, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)</span>
|
||||
<span id="annotated-cell-26-2"><a href="#annotated-cell-26-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(nomis_extract_census2011, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-26" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-26-3" class="code-annotation-target"><a href="#annotated-cell-26-3" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2001_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
|
||||
<span id="annotated-cell-26-4"><a href="#annotated-cell-26-4" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span> <span class="sc">&</span> C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Ethnic group"</span>)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-26" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-26-5" class="code-annotation-target"><a href="#annotated-cell-26-5" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHHUK11_NAME))</span>
|
||||
<span id="annotated-cell-26-6"><a href="#annotated-cell-26-6" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHPUK11_NAME))</span>
|
||||
<span id="annotated-cell-26-7"><a href="#annotated-cell-26-7" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_plot <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity, <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><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="sourceCode cell-code" id="annotated-cell-28"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-28" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-28-1" class="code-annotation-target"><a href="#annotated-cell-28-1" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(nomis_extract_census2001, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)</span>
|
||||
<span id="annotated-cell-28-2"><a href="#annotated-cell-28-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">select</span>(nomis_extract_census2011, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-28" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-28-3" class="code-annotation-target"><a href="#annotated-cell-28-3" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2001_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span>)</span>
|
||||
<span id="annotated-cell-28-4"><a href="#annotated-cell-28-4" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_2011_religion_ethnicity, GEOGRAPHY_NAME<span class="sc">==</span><span class="st">"England and Wales"</span> <span class="sc">&</span> C_RELPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Religion"</span> <span class="sc">&</span> C_ETHPUK11_NAME <span class="sc">!=</span> <span class="st">"All categories: Ethnic group"</span>)</span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-28" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-28-5" class="code-annotation-target"><a href="#annotated-cell-28-5" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHHUK11_NAME))</span>
|
||||
<span id="annotated-cell-28-6"><a href="#annotated-cell-28-6" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> <span class="fu">filter</span>(<span class="fu">grepl</span>(<span class="st">'Total'</span>, C_ETHPUK11_NAME))</span>
|
||||
<span id="annotated-cell-28-7"><a href="#annotated-cell-28-7" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_plot <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_2011_religion_ethnicity, <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><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-annotation">
|
||||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-26" data-target-annotation="1">1</dt>
|
||||
<dt data-target-cell="annotated-cell-28" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-26" data-code-lines="1,2" data-code-annotation="1">Select columns</span>
|
||||
<span data-code-cell="annotated-cell-28" data-code-lines="1,2" data-code-annotation="1">Select columns</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-26" data-target-annotation="2">2</dt>
|
||||
<dt data-target-cell="annotated-cell-28" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-26" data-code-lines="3,4" data-code-annotation="2">Filter down to simplified dataset with England / Wales and percentages without totals</span>
|
||||
<span data-code-cell="annotated-cell-28" data-code-lines="3,4" data-code-annotation="2">Filter down to simplified dataset with England / Wales and percentages without totals</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-26" data-target-annotation="3">3</dt>
|
||||
<dt data-target-cell="annotated-cell-28" data-target-annotation="3">3</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-26" data-code-lines="5,6" data-code-annotation="3">Drop unnecessary columns</span>
|
||||
<span data-code-cell="annotated-cell-28" data-code-lines="5,6" data-code-annotation="3">Drop unnecessary columns</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
<p>The <code>bind</code> tool we’re going to use is very picky and expects everything to match perfectly so that it don’t join up data that is unrelated. Unfortunately, the census table data format has changed in each decade, so we need to harmonise the column titles so that we can join the data and avoid confusing R.</p>
|
||||
<p>The <code>bind</code> tool we’re going to use is very picky and expects everything to match perfectly so that it doesn’t join up data that is unrelated. Unfortunately, the census table data format has changed in each decade, so we need to harmonise the column titles so that we can join the data and avoid confusing R. This is a pretty common problem you’ll face in working with multiple datasets in the same chart, so well worth noticing the extra necessary step here.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-27"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-27" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-27-1" class="code-annotation-target"><a href="#annotated-cell-27-1" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2001"</span>)</span>
|
||||
<span id="annotated-cell-27-2"><a href="#annotated-cell-27-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2011"</span>)</span>
|
||||
<span id="annotated-cell-27-3"><a href="#annotated-cell-27-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2021"</span>)</span>
|
||||
<span id="annotated-cell-27-4"><a href="#annotated-cell-27-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-27" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-27-5" class="code-annotation-target"><a href="#annotated-cell-27-5" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2001_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||
<span id="annotated-cell-27-6"><a href="#annotated-cell-27-6" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2011_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||
<span id="annotated-cell-27-7"><a href="#annotated-cell-27-7" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2021_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="sourceCode cell-code" id="annotated-cell-29"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><a class="code-annotation-anchor" data-target-cell="annotated-cell-29" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-29-1" class="code-annotation-target"><a href="#annotated-cell-29-1" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2001"</span>)</span>
|
||||
<span id="annotated-cell-29-2"><a href="#annotated-cell-29-2" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2011"</span>)</span>
|
||||
<span id="annotated-cell-29-3"><a href="#annotated-cell-29-3" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity<span class="sc">$</span>dataset <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"2021"</span>)</span>
|
||||
<span id="annotated-cell-29-4"><a href="#annotated-cell-29-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-29" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-29-5" class="code-annotation-target"><a href="#annotated-cell-29-5" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2001_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||
<span id="annotated-cell-29-6"><a href="#annotated-cell-29-6" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2011_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span>
|
||||
<span id="annotated-cell-29-7"><a href="#annotated-cell-29-7" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(uk_census_2021_religion_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Geography"</span>, <span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>, <span class="st">"Value"</span>, <span class="st">"Year"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-annotation">
|
||||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-27" data-target-annotation="1">1</dt>
|
||||
<dt data-target-cell="annotated-cell-29" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-27" data-code-lines="1,2,3" data-code-annotation="1">First we add a column to each dataframe letting us know which census year it is from so we don’t lose track of the census it comes from when they’re all combined into a single table.</span>
|
||||
<span data-code-cell="annotated-cell-29" data-code-lines="1,2,3" data-code-annotation="1">First we add a column to each dataframe letting us know which census year it is from so we don’t lose track of the census it comes from when they’re all combined into a single table.</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-27" data-target-annotation="2">2</dt>
|
||||
<dt data-target-cell="annotated-cell-29" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-cell="annotated-cell-27" data-code-lines="5,6,7" data-code-annotation="2">Next, we tidy the <code>names</code> of each column by overwriting them all with more legible versions.</span>
|
||||
<span data-code-cell="annotated-cell-29" data-code-lines="5,6,7" data-code-annotation="2">Next, we tidy the <code>names</code> of each column by overwriting them all with more legible versions.</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now that we have column titles all sorted, we also need to adjust the category descriptions as the formatting has also changed in subsequent decades. To do this, we’ll use the very handy tool <code>mutate</code> which is a bit like a “find and replace text” tool in R:</p>
|
||||
<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="co"># Next we need to change the terms using mutate()</span></span>
|
||||
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||
<span id="cb22-3"><a href="#cb22-3" 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="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-5"><a href="#cb22-5" 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="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-7"><a href="#cb22-7" 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="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-9"><a href="#cb22-9" 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="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black or Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-11"><a href="#cb22-11" 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="cb22-12"><a href="#cb22-12" 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="cb22-13"><a href="#cb22-13" aria-hidden="true" tabindex="-1"></a> </span>
|
||||
<span id="cb22-14"><a href="#cb22-14" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||
<span id="cb22-15"><a href="#cb22-15" 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="cb22-16"><a href="#cb22-16" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-17"><a href="#cb22-17" 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="cb22-18"><a href="#cb22-18" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed/multiple ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-19"><a href="#cb22-19" 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="cb22-20"><a href="#cb22-20" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian/Asian British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-21"><a href="#cb22-21" 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="cb22-22"><a href="#cb22-22" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black/African/Caribbean/Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-23"><a href="#cb22-23" 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="cb22-24"><a href="#cb22-24" 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="cb22-25"><a href="#cb22-25" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb22-26"><a href="#cb22-26" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> uk_census_2021_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||
<span id="cb22-27"><a href="#cb22-27" 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="cb22-28"><a href="#cb22-28" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-29"><a href="#cb22-29" 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="cb22-30"><a href="#cb22-30" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed or Multiple ethnic groups$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-31"><a href="#cb22-31" 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="cb22-32"><a href="#cb22-32" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian, Asian British or Asian Welsh$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-33"><a href="#cb22-33" 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="cb22-34"><a href="#cb22-34" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black, Black British, Black Welsh, Caribbean or African$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb22-35"><a href="#cb22-35" 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="cb22-36"><a href="#cb22-36" 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></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<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="co"># Next we need to change the terms using mutate()</span></span>
|
||||
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>uk_census_2001_religion_ethnicity <span class="ot"><-</span> uk_census_2001_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||
<span id="cb24-3"><a href="#cb24-3" 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="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-5"><a href="#cb24-5" 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="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-7"><a href="#cb24-7" 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="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-9"><a href="#cb24-9" 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="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black or Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-11"><a href="#cb24-11" 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="cb24-12"><a href="#cb24-12" 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="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a> </span>
|
||||
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a>uk_census_2011_religion_ethnicity <span class="ot"><-</span> uk_census_2011_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||
<span id="cb24-15"><a href="#cb24-15" 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="cb24-16"><a href="#cb24-16" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-17"><a href="#cb24-17" 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="cb24-18"><a href="#cb24-18" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed/multiple ethnic group: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-19"><a href="#cb24-19" 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="cb24-20"><a href="#cb24-20" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian/Asian British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-21"><a href="#cb24-21" 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="cb24-22"><a href="#cb24-22" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black/African/Caribbean/Black British: Total$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-23"><a href="#cb24-23" 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="cb24-24"><a href="#cb24-24" 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="cb24-25"><a href="#cb24-25" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb24-26"><a href="#cb24-26" aria-hidden="true" tabindex="-1"></a>uk_census_2021_religion_ethnicity <span class="ot"><-</span> uk_census_2021_religion_ethnicity <span class="sc">%>%</span> </span>
|
||||
<span id="cb24-27"><a href="#cb24-27" 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="cb24-28"><a href="#cb24-28" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^White: Total$"</span>, <span class="at">replacement =</span> <span class="st">"White"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-29"><a href="#cb24-29" 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="cb24-30"><a href="#cb24-30" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Mixed or Multiple ethnic groups$"</span>, <span class="at">replacement =</span> <span class="st">"Mixed"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-31"><a href="#cb24-31" 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="cb24-32"><a href="#cb24-32" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Asian, Asian British or Asian Welsh$"</span>, <span class="at">replacement =</span> <span class="st">"Asian"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-33"><a href="#cb24-33" 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="cb24-34"><a href="#cb24-34" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> <span class="st">"^Black, Black British, Black Welsh, Caribbean or African$"</span>, <span class="at">replacement =</span> <span class="st">"Black"</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb24-35"><a href="#cb24-35" 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="cb24-36"><a href="#cb24-36" 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></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Now that we have all the columns and data in formats which will match for merge, let’d do the merge! This is only two (rather than three operations) as we combine 2021 and 2011 and then do a second combine that grafts in the 2001 data:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_ethnicity, uk_census_2011_religion_ethnicity)</span>
|
||||
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_merged_religion_ethnicity, uk_census_2001_religion_ethnicity)</span></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>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_2021_religion_ethnicity, uk_census_2011_religion_ethnicity)</span>
|
||||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity <span class="ot"><-</span> <span class="fu">rbind</span>(uk_census_merged_religion_ethnicity, uk_census_2001_religion_ethnicity)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>As we realised in the work above, we need to split out non-white and white data so that the data is visually comprehensible:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_merged_religion_ethnicity, Ethnicity <span class="sc">!=</span> <span class="st">"White"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<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>uk_census_merged_religion_ethnicity_nonwhite <span class="ot"><-</span> <span class="fu">filter</span>(uk_census_merged_religion_ethnicity, Ethnicity <span class="sc">!=</span> <span class="st">"White"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Hopefully if everything went properly, we can now do an initial <code>ggplot</code> to see how things look side-by-side:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<div>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-28-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>We’re getting there, but as you can see there are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn’t 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="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">library</span>(scales) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> <span class="fu">unit_format</span>(<span class="at">unit =</span> <span class="st">"M"</span>, <span class="at">scale =</span> <span class="fl">1e-6</span>), <span class="at">breaks =</span> <span class="fu">breaks_extended</span>(<span class="dv">8</span>)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<div>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-29-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>This chart shows an increase in almost every category for each decade, though it’s a bit hard to read in some cases. However, if we attend to our hacker principles, there’s another element here which can produce some misleading information. Consider for a moment how this information is based on the increase in <em>raw numbers</em>. It’s possbile that the numbers for each religion category may be going up, but population levels are also rising, and it’s possible here that the percentage share for a particular category may have gone up a bit less than population increase, e.g. the share of the population for that category has actually gone <em>down</em>. This is easy to fix and provide some more accurate information by normalising those figures based on the share of overall population for each decade. Let’s transform and visualise our data as percentages to see what kind of trends we can actually isolate:</p>
|
||||
<div class="cell">
|
||||
<div class="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>uk_census_merged_religion_ethnicity <span class="ot"><-</span> uk_census_merged_religion_ethnicity <span class="sc">%>%</span></span>
|
||||
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(Ethnicity, Year) <span class="sc">%>%</span></span>
|
||||
<span id="cb27-3"><a href="#cb27-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="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb27-5"><a href="#cb27-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 Censuses 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="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="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>
|
||||
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> </span>
|
||||
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<figure class="figure">
|
||||
|
@ -1001,13 +1030,47 @@ Statistics 101: Logarithmic Visualisation
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now you can see why this shift is important - the visualisation tells a completely different story in some cases across the two different charts. In the first, working off raw numbers we see a net increase in Christianity across all categories. But if we take into account the fact that the overall share of population is growing for each of these groups, their actual composition is changing in a different direction. The proportion of each group is declining across the three census periods (albeit with an exception for the “Other” category from 2011 to 2021).</p>
|
||||
<p>We’re getting there, but as you can see there are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn’t 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 class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity_nonwhite, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Value)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">ncol =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> <span class="fu">unit_format</span>(<span class="at">unit =</span> <span class="st">"M"</span>, <span class="at">scale =</span> <span class="fl">1e-6</span>), <span class="at">breaks =</span> <span class="fu">breaks_extended</span>(<span class="dv">8</span>)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Census of England and Wales"</span>) <span class="sc">+</span> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">ylab</span>(<span class="st">""</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>, <span class="at">vjust =</span> <span class="fl">0.5</span>, <span class="at">hjust=</span><span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<div>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-31-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>This chart shows an increase in almost every category for each decade, though it’s a bit hard to read in some cases. However, if we attend to our hacker principles, there’s another element here which can produce some misleading information. Consider for a moment how this information is based on the increase in <em>raw numbers</em>. It’s possbile that the numbers for each religion category may be going up, but population levels are also rising, and it’s possible here that the percentage share for a particular category may have gone up a bit less than population increase, e.g. the share of the population for that category has actually gone <em>down</em>. This is easy to fix and provide some more accurate information by normalising those figures based on the share of overall population for each decade. Let’s transform and visualise our data as percentages to see what kind of trends we can actually isolate:</p>
|
||||
<div class="cell">
|
||||
<div class="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>uk_census_merged_religion_ethnicity <span class="ot"><-</span> uk_census_merged_religion_ethnicity <span class="sc">%>%</span></span>
|
||||
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(Ethnicity, Year) <span class="sc">%>%</span></span>
|
||||
<span id="cb29-3"><a href="#cb29-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="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb29-5"><a href="#cb29-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 Censuses 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>
|
||||
<figure class="figure">
|
||||
<p><img src="chapter_1_files/figure-html/unnamed-chunk-32-1.png" class="img-fluid figure-img" width="672"></p>
|
||||
</figure>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now you can see why this shift is important - the visualisation tells a completely different story in some cases across the two different charts. In the first (working off raw numbers) we see a net increase in Christianity across all categories. But if we take into account the fact that the overall share of population is growing for each of these groups, their actual composition is changing in a different direction. The proportion of each group is declining across the three census periods (albeit with an exception for the “Other” category from 2011 to 2021).</p>
|
||||
<p>To highlight a few of the technical features I’ve added for this final plot, I’ve used a specific feature within <code>facet_wrap</code> <code>scales = "free_x"</code> to let each of the individual facets adjust the total range on the x-axis. Since we’re looking at trends here and not absolute values, having correspondence across scales isn’t important and this makes for something a bit more visually tidy. I’ve also shifted the code for <code>scale_y_continuous</code> to render values as percentages (rather than millions).</p>
|
||||
<p>In case you want to print this plot out and hang it on your wall, you can use the <code>ggsave</code> tool to render the chart as an image file which you can print or email to a friend (or professor!):</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>uk_census_merged_religion_ethnicity_plot <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Percent)) <span class="sc">+</span> <span class="fu">geom_bar</span>(<span class="at">position=</span><span class="st">"dodge"</span>, <span class="at">stat =</span><span class="st">"identity"</span>, <span class="at">colour =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">scales=</span><span class="st">"free_x"</span>) <span class="sc">+</span> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span>percent) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Censuses 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="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/chart.png"</span>, <span class="at">plot=</span>uk_census_merged_religion_ethnicity_plot, <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 class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>uk_census_merged_religion_ethnicity_plot <span class="ot"><-</span> <span class="fu">ggplot</span>(uk_census_merged_religion_ethnicity, <span class="fu">aes</span>(<span class="at">fill=</span>Year, <span class="at">x=</span>Religion, <span class="at">y=</span>Percent)) <span class="sc">+</span> </span>
|
||||
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Set1"</span>) <span class="sc">+</span> </span>
|
||||
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span>percent) <span class="sc">+</span> </span>
|
||||
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggtitle</span>(<span class="st">"Religious Affiliation in the 2001-2021 Censuses of England and Wales"</span>) <span class="sc">+</span> </span>
|
||||
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a> <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>
|
||||
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a> <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="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb30-10"><a href="#cb30-10" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/chart.png"</span>, <span class="at">plot=</span>uk_census_merged_religion_ethnicity_plot, <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>That’s a pretty good day’s work. We’ve covered bifactorial analysis of the census data, compared this across years, and checked in each case to be sure that we’re representing the data accurately in the various visual elements of our charts. For the next chapter, we’re going to explore a wider range of ways to measure and represent religion.</p>
|
||||
<p>In the meantime, if you want to download the R code without all the commentary here so you can try running it in a browser, you can download that from the cookbook repository.</p>
|
||||
|
@ -1098,6 +1161,24 @@ window.document.addEventListener("DOMContentLoaded", function (event) {
|
|||
// clear code selection
|
||||
e.clearSelection();
|
||||
});
|
||||
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
|
||||
var mailtoRegex = new RegExp(/^mailto:/);
|
||||
var filterRegex = new RegExp('/' + window.location.host + '/');
|
||||
var isInternal = (href) => {
|
||||
return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
|
||||
}
|
||||
// Inspect non-navigation links and adorn them if external
|
||||
var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)');
|
||||
for (var i=0; i<links.length; i++) {
|
||||
const link = links[i];
|
||||
if (!isInternal(link.href)) {
|
||||
// undo the damage that might have been done by quarto-nav.js in the case of
|
||||
// links that we want to consider external
|
||||
if (link.dataset.originalHref !== undefined) {
|
||||
link.href = link.dataset.originalHref;
|
||||
}
|
||||
}
|
||||
}
|
||||
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
|
||||
const config = {
|
||||
allowHTML: true,
|
||||
|
@ -1132,7 +1213,11 @@ window.document.addEventListener("DOMContentLoaded", function (event) {
|
|||
try { href = new URL(href).hash; } catch {}
|
||||
const id = href.replace(/^#\/?/, "");
|
||||
const note = window.document.getElementById(id);
|
||||
return note.innerHTML;
|
||||
if (note) {
|
||||
return note.innerHTML;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
}
|
||||
const xrefs = window.document.querySelectorAll('a.quarto-xref');
|
||||
|
@ -1410,13 +1495,13 @@ window.document.addEventListener("DOMContentLoaded", function (event) {
|
|||
</script>
|
||||
<nav class="page-navigation">
|
||||
<div class="nav-page nav-page-previous">
|
||||
<a href="./index.html" class="pagination-link aria-label=" introduction:="" hacking="" religion"="">
|
||||
<a href="./index.html" class="pagination-link" aria-label="Introduction: Hacking Religion">
|
||||
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text">Introduction: Hacking Religion</span>
|
||||
</a>
|
||||
</div>
|
||||
<div class="nav-page nav-page-next">
|
||||
<a href="./chapter_2.html" class="pagination-link" aria-label="<span class='chapter-number'>2</span> <span class='chapter-title'>Getting into the nitty-gritty details</span>">
|
||||
<span class="nav-page-text"><span class="chapter-number">2</span> <span class="chapter-title">Getting into the nitty-gritty details</span></span> <i class="bi bi-arrow-right-short"></i>
|
||||
<a href="./chapter_2.html" class="pagination-link" aria-label="Different ways to measure religion using data science">
|
||||
<span class="nav-page-text"><span class="chapter-number">2</span> <span class="chapter-title">Different ways to measure religion using data science</span></span> <i class="bi bi-arrow-right-short"></i>
|
||||
</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue