<navclass="quarto-page-breadcrumbs"aria-label="breadcrumb"><olclass="breadcrumb"><liclass="breadcrumb-item"><ahref="./chapter_1.html"><spanclass="chapter-number">2</span> <spanclass="chapter-title">The 2021 UK Census</span></a></li></ol></nav>
<spanclass="menu-text"><spanclass="chapter-number">4</span> <spanclass="chapter-title">Mapping churches: geospatial data science</span></span></a>
<spanclass="menu-text"><spanclass="chapter-number">5</span> <spanclass="chapter-title">Data scraping, corpus analysis and wordclouds</span></span></a>
<li><ahref="#your-first-project-the-uk-census"id="toc-your-first-project-the-uk-census"class="nav-link active"data-scroll-target="#your-first-project-the-uk-census"><spanclass="header-section-number">2.1</span> Your first project: the UK Census</a></li>
<li><ahref="#parsing-and-exploring-your-data"id="toc-parsing-and-exploring-your-data"class="nav-link"data-scroll-target="#parsing-and-exploring-your-data"><spanclass="header-section-number">2.3</span> Parsing and Exploring your data</a></li>
<li><ahref="#making-your-first-data-visulation-the-humble-bar-chart"id="toc-making-your-first-data-visulation-the-humble-bar-chart"class="nav-link"data-scroll-target="#making-your-first-data-visulation-the-humble-bar-chart"><spanclass="header-section-number">2.4</span> Making your first data visulation: the humble bar chart</a>
<li><ahref="#is-your-chart-accurate-telling-the-truth-in-data-science"id="toc-is-your-chart-accurate-telling-the-truth-in-data-science"class="nav-link"data-scroll-target="#is-your-chart-accurate-telling-the-truth-in-data-science"><spanclass="header-section-number">2.5</span> Is your chart accurate? Telling the truth in data science</a></li>
<li><ahref="#making-our-script-reproducible"id="toc-making-our-script-reproducible"class="nav-link"data-scroll-target="#making-our-script-reproducible"><spanclass="header-section-number">2.6</span> Making our script reproducible</a></li>
<h2data-number="2.1"class="anchored"data-anchor-id="your-first-project-the-uk-census"><spanclass="header-section-number">2.1</span> Your first project: the UK Census</h2>
<p>Let’s start by importing some data into R. Because R is what is called an object-oriented programming language, we’ll always take our information and give it a home inside a named object. There are many different kinds of objects, which you can specify, but usually R will assign a type that seems to fit best.</p>
<divclass="page-columns page-full"><p></p><divclass="no-row-height column-margin column-container"><spanclass="">If you’d like to explore this all in a bit more depth, you can find a very helpful summary in R for Data Science, chapter 8, <ahref="https://r4ds.hadley.nz/data-import#reading-data-from-a-file">“data import”</a>.</span></div></div>
<p>In the example below, we’re going to read in data from a comma separated value file (“csv”) which has rows of information on separate lines in a text file with each column separated by a comma. This is one of the standard plain text file formats. R has a function you can use to import this efficiently called “read.csv”. Each line of code in R usually starts with the object, and then follows with instructions on what we’re going to put inside it, where that comes from, and how to format it:</p>
<divclass="sourceCode cell-code"id="cb1"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb1-1"><ahref="#cb1-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">setwd</span>(<spanclass="st">"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion"</span>)</span>
<spanid="cb1-2"><ahref="#cb1-2"aria-hidden="true"tabindex="-1"></a><spanclass="fu">library</span>(here) <spanclass="co"># much better way to manage working paths in R across multiple instances</span></span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook</code></pre>
</div>
<divclass="sourceCode cell-code"id="cb3"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb3-1"><ahref="#cb3-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">library</span>(tidyverse)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors</code></pre>
</div>
<divclass="sourceCode cell-code"id="cb6"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb6-1"><ahref="#cb6-1"aria-hidden="true"tabindex="-1"></a>here<spanclass="sc">::</span><spanclass="fu">i_am</span>(<spanclass="st">"chapter_1.qmd"</span>)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="cell-output cell-output-stderr">
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion</code></pre>
<divclass="sourceCode cell-code"id="cb8"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb8-1"><ahref="#cb8-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion <spanclass="ot"><-</span><spanclass="fu">read.csv</span>(<spanclass="fu">here</span>(<spanclass="st">"example_data"</span>, <spanclass="st">"census2021-ts030-rgn.csv"</span>)) </span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="sourceCode cell-code"id="cb9"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb9-1"><ahref="#cb9-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">head</span>(uk_census_2021_religion)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="sourceCode cell-code"id="cb11"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb11-1"><ahref="#cb11-1"aria-hidden="true"tabindex="-1"></a>knitr<spanclass="sc">::</span><spanclass="fu">kable</span>(<spanclass="fu">head</span>(uk_census_2021_religion))</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<tdstyle="text-align: left;">Yorkshire and The Humber</td>
<tdstyle="text-align: right;">5480774</td>
<tdstyle="text-align: right;">2161185</td>
<tdstyle="text-align: right;">2461519</td>
<tdstyle="text-align: right;">15803</td>
<tdstyle="text-align: right;">29243</td>
<tdstyle="text-align: right;">9355</td>
<tdstyle="text-align: right;">442533</td>
<tdstyle="text-align: right;">24034</td>
<tdstyle="text-align: right;">23618</td>
<tdstyle="text-align: right;">313484</td>
</tr>
<trclass="even">
<tdstyle="text-align: left;">East Midlands</td>
<tdstyle="text-align: right;">4880054</td>
<tdstyle="text-align: right;">1950354</td>
<tdstyle="text-align: right;">2214151</td>
<tdstyle="text-align: right;">14521</td>
<tdstyle="text-align: right;">120345</td>
<tdstyle="text-align: right;">4313</td>
<tdstyle="text-align: right;">210766</td>
<tdstyle="text-align: right;">53950</td>
<tdstyle="text-align: right;">24813</td>
<tdstyle="text-align: right;">286841</td>
</tr>
<trclass="odd">
<tdstyle="text-align: left;">West Midlands</td>
<tdstyle="text-align: right;">5950756</td>
<tdstyle="text-align: right;">1955003</td>
<tdstyle="text-align: right;">2770559</td>
<tdstyle="text-align: right;">18804</td>
<tdstyle="text-align: right;">88116</td>
<tdstyle="text-align: right;">4394</td>
<tdstyle="text-align: right;">569963</td>
<tdstyle="text-align: right;">172398</td>
<tdstyle="text-align: right;">31805</td>
<tdstyle="text-align: right;">339714</td>
</tr>
<trclass="even">
<tdstyle="text-align: left;">East</td>
<tdstyle="text-align: right;">6335072</td>
<tdstyle="text-align: right;">2544509</td>
<tdstyle="text-align: right;">2955071</td>
<tdstyle="text-align: right;">26814</td>
<tdstyle="text-align: right;">86631</td>
<tdstyle="text-align: right;">42012</td>
<tdstyle="text-align: right;">234744</td>
<tdstyle="text-align: right;">24284</td>
<tdstyle="text-align: right;">36380</td>
<tdstyle="text-align: right;">384627</td>
</tr>
</tbody>
</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>
<divclass="sourceCode cell-code"id="cb12"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb12-1"><ahref="#cb12-1"aria-hidden="true"tabindex="-1"></a>knitr<spanclass="sc">::</span><spanclass="fu">kable</span>(<spanclass="fu">tail</span>(uk_census_2021_religion))</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<h2data-number="2.3"class="anchored"data-anchor-id="parsing-and-exploring-your-data"><spanclass="header-section-number">2.3</span> Parsing and Exploring your data</h2>
<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>Now we’ll use select in a different way to narrow our data to specific columns that are needed (no totals!).</p>
<divclass="page-columns page-full"><p></p><divclass="no-row-height column-margin column-container"><spanclass="">Some readers will want to pause here and check out Hadley Wickham’s “R For Data Science” book, in the section, <ahref="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>
<h2data-number="2.4"class="anchored"data-anchor-id="making-your-first-data-visulation-the-humble-bar-chart"><spanclass="header-section-number">2.4</span> Making your first data visulation: the humble bar chart</h2>
<divclass="sourceCode cell-code"id="cb14"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb14-1"><ahref="#cb14-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_wmids <spanclass="ot"><-</span> uk_census_2021_religion_wmids <spanclass="sc">%>%</span><spanclass="fu">select</span>(no_religion<spanclass="sc">:</span>no_response)</span>
<spanid="cb14-2"><ahref="#cb14-2"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_wmids <spanclass="ot"><-</span><spanclass="fu">gather</span>(uk_census_2021_religion_wmids)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></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>
<spanid="cb15-2"><ahref="#cb15-2"aria-hidden="true"tabindex="-1"></a><spanclass="fu">barplot</span>(<spanclass="at">height=</span>df<spanclass="sc">$</span>value, <spanclass="at">names=</span>df<spanclass="sc">$</span>key)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<spanid="annotated-cell-11-2"><ahref="#annotated-cell-11-2"aria-hidden="true"tabindex="-1"></a><spanclass="fu">geom_bar</span>(<spanclass="at">stat =</span><spanclass="st">"identity"</span>)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>Let’s assume we’re working with a data set that doesn’t include a “totals” column and that we might want to get sums for each column. This is pretty easy to do in R:</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="annotated-cell-13"><preclass="sourceCode r code-annotation-code code-with-copy code-annotated"><codeclass="sourceCode r"><aclass="code-annotation-anchor"data-target-cell="annotated-cell-13"data-target-annotation="1"onclick="event.preventDefault();">1</a><spanid="annotated-cell-13-1"class="code-annotation-target"><ahref="#annotated-cell-13-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_totals <spanclass="ot"><-</span> uk_census_2021_religion <spanclass="sc">%>%</span><spanclass="fu">select</span>(no_religion<spanclass="sc">:</span>no_response)</span>
<spandata-code-annotation="1"data-code-cell="annotated-cell-13"data-code-lines="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
<spandata-code-annotation="2"data-code-cell="annotated-cell-13"data-code-lines="3">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that you’ll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)</code></span>
<spandata-code-annotation="3"data-code-cell="annotated-cell-13"data-code-lines="4">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
<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>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb16"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb16-1"><ahref="#cb16-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_merged <spanclass="ot"><-</span><spanclass="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></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>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb17"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb17-1"><ahref="#cb17-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_totals<spanclass="sc">$</span>dataset <spanclass="ot"><-</span><spanclass="fu">c</span>(<spanclass="st">"totals"</span>)</span>
<spanid="cb17-3"><ahref="#cb17-3"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_merged <spanclass="ot"><-</span><spanclass="fu">rbind</span>(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>If you’re looking closely, you will notice that I’ve added two elements to our previous ggplot. I’ve asked ggplot to fill in the columns with reference to the <code>dataset</code> column we’ve just created. Then I’ve also asked ggplot to alter the <code>position="dodge"</code> which places bars side by side rather than stacked on top of one another. You can give it a try without this instruction to see how this works. We will use stacked bars in a later chapter, so remember this feature.</p>
<p>If you inspect our chart, you can see that we’re getting closer, but it’s not really that helpful to compare the totals. What we need to do is get percentages that can be compared side by side. This is easy to do using another <code>dplyr</code> feature <code>mutate</code>:</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="annotated-cell-17"><preclass="sourceCode r code-annotation-code code-with-copy"><codeclass="sourceCode r"><spanid="annotated-cell-17-1"><ahref="#annotated-cell-17-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_totals <spanclass="ot"><-</span> uk_census_2021_religion_totals <spanclass="sc">%>%</span></span>
<p>Now you can see a very rough comparison, which sets bars from the W Midlands data and overall 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. 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>These are basically just added to our <code>ggplot</code> code. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the <code>scale</code> using <code>scale_fill_brewer</code>. This uses a particular tool (and a personal favourite of mine) called <code>colorbrewer</code>. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb19"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb19-1"><ahref="#cb19-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(uk_census_2021_religion_merged, <spanclass="fu">aes</span>(<spanclass="at">fill=</span>dataset, <spanclass="at">x=</span>key, <spanclass="at">y=</span>perc)) <spanclass="sc">+</span><spanclass="fu">geom_bar</span>(<spanclass="at">position=</span><spanclass="st">"dodge"</span>, <spanclass="at">stat =</span><spanclass="st">"identity"</span>) <spanclass="sc">+</span><spanclass="fu">scale_fill_brewer</span>(<spanclass="at">palette =</span><spanclass="st">"Set1"</span>)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>We might also want to add a border to our bars to make them more visually striking (notice the addition of <code>color</code> to the geom_bar below. I’ve also added <code>reorder()</code> to the x value to sort descending from the largest to smallest.</p>
<divclass="page-columns page-full"><p></p><divclass="no-row-height column-margin column-container"><spanclass="">You can find more information about reordering ggplots on the <ahref="https://r-graph-gallery.com/267-reorder-a-variable-in-ggplot2.html">R Graph gallery</a>.</span></div></div>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb20"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb20-1"><ahref="#cb20-1"aria-hidden="true"tabindex="-1"></a>uk_census_2021_religion_merged<spanclass="sc">$</span>dataset <spanclass="ot"><-</span><spanclass="fu">factor</span>(uk_census_2021_religion_merged<spanclass="sc">$</span>dataset, <spanclass="at">levels =</span><spanclass="fu">c</span>(<spanclass="st">'wmids'</span>, <spanclass="st">'totals'</span>))</span>
<p>We can fine tune a few other visual features here as well, like adding a title with <code>ggtitle</code> and a them with some prettier fonts with <code>theme_ipsum()</code> (which requires the <code>hrbrthemes()</code> library). We can also remove the x and y axis labels (not the data labels, which are rather important).</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb21"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb21-1"><ahref="#cb21-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(uk_census_2021_religion_merged, <spanclass="fu">aes</span>(<spanclass="at">fill=</span><spanclass="fu">fct_reorder</span>(dataset, value), <spanclass="at">x=</span><spanclass="fu">reorder</span>(key,<spanclass="sc">-</span>value),value, <spanclass="at">y=</span>perc)) <spanclass="sc">+</span><spanclass="fu">geom_bar</span>(<spanclass="at">position=</span><spanclass="st">"dodge"</span>, <spanclass="at">stat =</span><spanclass="st">"identity"</span>, <spanclass="at">colour =</span><spanclass="st">"black"</span>) <spanclass="sc">+</span><spanclass="fu">scale_fill_brewer</span>(<spanclass="at">palette =</span><spanclass="st">"Set1"</span>) <spanclass="sc">+</span><spanclass="fu">ggtitle</span>(<spanclass="st">"Religious Affiliation in the UK: 2021"</span>) <spanclass="sc">+</span><spanclass="fu">xlab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">ylab</span>(<spanclass="st">""</span>)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<h2data-number="2.5"class="anchored"data-anchor-id="is-your-chart-accurate-telling-the-truth-in-data-science"><spanclass="header-section-number">2.5</span> Is your chart accurate? Telling the truth in data science</h2>
<p>There is some technical work yet to be done fine-tuning the visualisation of our chart here. But I’d like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. 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. But 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, but the census hasn’t capture this so we simply don’t know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This 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. Finally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.</p>
<p>What does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive, almost completely population level, sample so there is additional validity there. The easiest way to represent all this reality in the form of speaking truthfully about our data is to acknowledge that however valid it may seem, it is nonetheless a snapshot. For this reason, I would always advise that the best title for a chart is one which specifies the data set. We should also probably do something different with those non-responses:</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb22"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb22-1"><ahref="#cb22-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(uk_census_2021_religion_merged, <spanclass="fu">aes</span>(<spanclass="at">fill=</span><spanclass="fu">fct_reorder</span>(dataset, value), <spanclass="at">x=</span><spanclass="fu">reorder</span>(key,<spanclass="sc">-</span>value),value, <spanclass="at">y=</span>perc)) <spanclass="sc">+</span><spanclass="fu">geom_bar</span>(<spanclass="at">position=</span><spanclass="st">"dodge"</span>, <spanclass="at">stat =</span><spanclass="st">"identity"</span>, <spanclass="at">colour =</span><spanclass="st">"black"</span>) <spanclass="sc">+</span><spanclass="fu">scale_fill_brewer</span>(<spanclass="at">palette =</span><spanclass="st">"Set1"</span>) <spanclass="sc">+</span><spanclass="fu">ggtitle</span>(<spanclass="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <spanclass="sc">+</span><spanclass="fu">xlab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">ylab</span>(<spanclass="st">""</span>)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<h2data-number="2.6"class="anchored"data-anchor-id="making-our-script-reproducible"><spanclass="header-section-number">2.6</span> Making our script reproducible</h2>
<p>Let’s take a moment to review our hacker code. I’ve just spent some time addressing how we can be truthful in our data science work. We haven’t done much yet to talk abour reproducibility.</p>
<p>One element of R data analysis 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 quantative variable (also known as bivariate data) into the mix, however can also generate a lot more information and we have to think about visualising it in different ways which can still communicate with visual clarity in spite of the additional visual noise which is inevitable with enhanced complexity. Let’s have a look at the way that religion in England and Wales breaks down by ethnicity.</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb23"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb23-1"><ahref="#cb23-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">library</span>(nomisr)</span>
<spanid="cb23-3"><ahref="#cb23-3"aria-hidden="true"tabindex="-1"></a><spanclass="co"># Process to explore nomis() data for specific datasets</span></span>
<spanid="cb23-6"><ahref="#cb23-6"aria-hidden="true"tabindex="-1"></a>tibble<spanclass="sc">::</span><spanclass="fu">glimpse</span>(religion_measures)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<spanid="cb25-5"><ahref="#cb25-5"aria-hidden="true"tabindex="-1"></a><spanclass="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
<spanid="cb25-6"><ahref="#cb25-6"aria-hidden="true"tabindex="-1"></a>uk_census_2011_religion <spanclass="ot"><-</span><spanclass="fu">filter</span>(z, GEOGRAPHY_NAME<spanclass="sc">==</span><spanclass="st">"England and Wales"</span><spanclass="sc">&</span> RURAL_URBAN_NAME<spanclass="sc">==</span><spanclass="st">"Total"</span><spanclass="sc">&</span> C_RELPUK11_NAME <spanclass="sc">!=</span><spanclass="st">"All categories: Religion"</span>)</span>
<spanid="cb25-7"><ahref="#cb25-7"aria-hidden="true"tabindex="-1"></a><spanclass="co"># Drop unnecessary columns</span></span>
<spanid="cb25-11"><ahref="#cb25-11"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggsave</span>(<spanclass="at">filename =</span><spanclass="st">"plot.png"</span>, <spanclass="at">plot =</span> plot1)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<divclass="sourceCode cell-code"id="cb27"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb27-1"><ahref="#cb27-1"aria-hidden="true"tabindex="-1"></a><spanclass="co"># grab data from nomis for 2011 census religion / ethnicity table</span></span>
<spanid="cb27-5"><ahref="#cb27-5"aria-hidden="true"tabindex="-1"></a><spanclass="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
<spanid="cb27-7"><ahref="#cb27-7"aria-hidden="true"tabindex="-1"></a><spanclass="co"># Simplify data to only include general totals and omit subcategories</span></span>
<spanid="cb27-10"><ahref="#cb27-10"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(uk_census_2011_religion_ethnicitity, <spanclass="fu">aes</span>(<spanclass="at">fill=</span>C_ETHPUK11_NAME, <spanclass="at">x=</span>C_RELPUK11_NAME, <spanclass="at">y=</span>OBS_VALUE)) <spanclass="sc">+</span><spanclass="fu">geom_bar</span>(<spanclass="at">position=</span><spanclass="st">"dodge"</span>, <spanclass="at">stat =</span><spanclass="st">"identity"</span>, <spanclass="at">colour =</span><spanclass="st">"black"</span>) <spanclass="sc">+</span><spanclass="fu">scale_fill_brewer</span>(<spanclass="at">palette =</span><spanclass="st">"Set1"</span>) <spanclass="sc">+</span><spanclass="fu">ggtitle</span>(<spanclass="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <spanclass="sc">+</span><spanclass="fu">xlab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">ylab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">theme</span>(<spanclass="at">axis.text.x =</span><spanclass="fu">element_text</span>(<spanclass="at">angle =</span><spanclass="dv">90</span>, <spanclass="at">vjust =</span><spanclass="fl">0.5</span>, <spanclass="at">hjust=</span><spanclass="dv">1</span>))</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>The trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb28"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb28-1"><ahref="#cb28-1"aria-hidden="true"tabindex="-1"></a><spanclass="co"># Filter down to simplified dataset with England / Wales and percentages without totals</span></span>
<spanid="cb28-5"><ahref="#cb28-5"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(uk_census_2011_religion_ethnicitity_nonwhite, <spanclass="fu">aes</span>(<spanclass="at">fill=</span>C_ETHPUK11_NAME, <spanclass="at">x=</span>C_RELPUK11_NAME, <spanclass="at">y=</span>OBS_VALUE)) <spanclass="sc">+</span><spanclass="fu">geom_bar</span>(<spanclass="at">position=</span><spanclass="st">"dodge"</span>, <spanclass="at">stat =</span><spanclass="st">"identity"</span>, <spanclass="at">colour =</span><spanclass="st">"black"</span>) <spanclass="sc">+</span><spanclass="fu">scale_fill_brewer</span>(<spanclass="at">palette =</span><spanclass="st">"Set1"</span>) <spanclass="sc">+</span><spanclass="fu">ggtitle</span>(<spanclass="st">"Religious Affiliation in the 2021 Census of England and Wales"</span>) <spanclass="sc">+</span><spanclass="fu">xlab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">ylab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">theme</span>(<spanclass="at">axis.text.x =</span><spanclass="fu">element_text</span>(<spanclass="at">angle =</span><spanclass="dv">90</span>, <spanclass="at">vjust =</span><spanclass="fl">0.5</span>, <spanclass="at">hjust=</span><spanclass="dv">1</span>))</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>This still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.</p>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb29"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb29-1"><ahref="#cb29-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(uk_census_2011_religion_ethnicitity_nonwhite, <spanclass="fu">aes</span>(<spanclass="at">x=</span>C_RELPUK11_NAME, <spanclass="at">y=</span>OBS_VALUE)) <spanclass="sc">+</span><spanclass="fu">geom_bar</span>(<spanclass="at">position=</span><spanclass="st">"dodge"</span>, <spanclass="at">stat =</span><spanclass="st">"identity"</span>, <spanclass="at">colour =</span><spanclass="st">"black"</span>) <spanclass="sc">+</span><spanclass="fu">facet_wrap</span>(<spanclass="sc">~</span>C_ETHPUK11_NAME, <spanclass="at">ncol =</span><spanclass="dv">2</span>) <spanclass="sc">+</span><spanclass="fu">scale_fill_brewer</span>(<spanclass="at">palette =</span><spanclass="st">"Set1"</span>) <spanclass="sc">+</span><spanclass="fu">ggtitle</span>(<spanclass="st">"Religious Affiliation in the 2011 Census of England and Wales"</span>) <spanclass="sc">+</span><spanclass="fu">xlab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">ylab</span>(<spanclass="st">""</span>) <spanclass="sc">+</span><spanclass="fu">theme</span>(<spanclass="at">axis.text.x =</span><spanclass="fu">element_text</span>(<spanclass="at">angle =</span><spanclass="dv">90</span>, <spanclass="at">vjust =</span><spanclass="fl">0.5</span>, <spanclass="at">hjust=</span><spanclass="dv">1</span>))</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>