<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-building-a-pie-chart"id="toc-your-first-project-building-a-pie-chart"class="nav-link active"data-scroll-target="#your-first-project-building-a-pie-chart"><spanclass="header-section-number">2.1</span> Your first project: building a pie chart</a>
<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.1.2</span> Parsing and Exploring your data</a></li>
</ul></li>
<li><ahref="#making-your-first-chart"id="toc-making-your-first-chart"class="nav-link"data-scroll-target="#making-your-first-chart"><spanclass="header-section-number">2.2</span> Making your first chart</a></li>
<h2data-number="2.1"class="anchored"data-anchor-id="your-first-project-building-a-pie-chart"><spanclass="header-section-number">2.1</span> Your first project: building a pie chart</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="cell">
<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="co"># R Setup -----------------------------------------------------------------</span></span>
<spanid="cb1-3"><ahref="#cb1-3"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>
<divclass="cell-output cell-output-stderr">
<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>
<h3data-number="2.1.2"class="anchored"data-anchor-id="parsing-and-exploring-your-data"><spanclass="header-section-number">2.1.2</span> Parsing and Exploring your data</h3>
<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>
<spanid="cb13-2"><ahref="#cb13-2"aria-hidden="true"tabindex="-1"></a><spanclass="fu">filter</span>(geography<spanclass="sc">==</span><spanclass="st">"West Midlands"</span>) </span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="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>
<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.2"class="anchored"data-anchor-id="making-your-first-chart"><spanclass="header-section-number">2.2</span> Making your first chart</h2>
<p>We’ve got a nice lean set of data, so now it’s time to visualise this. We’ll start by making a pie chart:</p>
<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-cell="annotated-cell-13"data-code-annotation="1"data-code-lines="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
<spandata-code-cell="annotated-cell-13"data-code-annotation="2"data-code-lines="3">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that you’ll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)</code></span>
<spandata-code-cell="annotated-cell-13"data-code-annotation="3"data-code-lines="4">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
<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>