<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>
</div>
<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>religion_uk <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>
<p>What’s in the table? You can take a quick look at either the top of the data frame, or the bottom using one of the following commands:</p>
<divclass="cell">
<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>(religion_uk)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="cell-output cell-output-stdout">
<pre><code> geography total no_religion christian buddhist hindu jewish
1 North East 2647012 1058122 1343948 7026 10924 4389
2 North West 7417397 2419624 3895779 23028 49749 33285
3 Yorkshire and The Humber 5480774 2161185 2461519 15803 29243 9355
4 East Midlands 4880054 1950354 2214151 14521 120345 4313
5 West Midlands 5950756 1955003 2770559 18804 88116 4394
6 East 6335072 2544509 2955071 26814 86631 42012
muslim sikh other no_response
1 72102 7206 9950 133345
2 563105 11862 28103 392862
3 442533 24034 23618 313484
4 210766 53950 24813 286841
5 569963 172398 31805 339714
6 234744 24284 36380 384627</code></pre>
</div>
</div>
<p>This is actually a fairly ugly table, so I’ll use an R tool called kable to give you prettier tables in the future, like this:</p>
<divclass="cell">
<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>(religion_uk))</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="bi"></i></button></pre></div>
<divclass="cell-output-display">
<tableclass="table table-sm table-striped small">
<colgroup>
<colstyle="width: 22%">
<colstyle="width: 7%">
<colstyle="width: 10%">
<colstyle="width: 9%">
<colstyle="width: 8%">
<colstyle="width: 6%">
<colstyle="width: 6%">
<colstyle="width: 6%">
<colstyle="width: 6%">
<colstyle="width: 5%">
<colstyle="width: 10%">
</colgroup>
<thead>
<trclass="header">
<thstyle="text-align: left;">geography</th>
<thstyle="text-align: right;">total</th>
<thstyle="text-align: right;">no_religion</th>
<thstyle="text-align: right;">christian</th>
<thstyle="text-align: right;">buddhist</th>
<thstyle="text-align: right;">hindu</th>
<thstyle="text-align: right;">jewish</th>
<thstyle="text-align: right;">muslim</th>
<thstyle="text-align: right;">sikh</th>
<thstyle="text-align: right;">other</th>
<thstyle="text-align: right;">no_response</th>
</tr>
</thead>
<tbody>
<trclass="odd">
<tdstyle="text-align: left;">North East</td>
<tdstyle="text-align: right;">2647012</td>
<tdstyle="text-align: right;">1058122</td>
<tdstyle="text-align: right;">1343948</td>
<tdstyle="text-align: right;">7026</td>
<tdstyle="text-align: right;">10924</td>
<tdstyle="text-align: right;">4389</td>
<tdstyle="text-align: right;">72102</td>
<tdstyle="text-align: right;">7206</td>
<tdstyle="text-align: right;">9950</td>
<tdstyle="text-align: right;">133345</td>
</tr>
<trclass="even">
<tdstyle="text-align: left;">North West</td>
<tdstyle="text-align: right;">7417397</td>
<tdstyle="text-align: right;">2419624</td>
<tdstyle="text-align: right;">3895779</td>
<tdstyle="text-align: right;">23028</td>
<tdstyle="text-align: right;">49749</td>
<tdstyle="text-align: right;">33285</td>
<tdstyle="text-align: right;">563105</td>
<tdstyle="text-align: right;">11862</td>
<tdstyle="text-align: right;">28103</td>
<tdstyle="text-align: right;">392862</td>
</tr>
<trclass="odd">
<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="cell">
<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>(religion_uk))</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>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb13"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb13-1"><ahref="#cb13-1"aria-hidden="true"tabindex="-1"></a>wmids_data <spanclass="ot"><-</span> religion_uk <spanclass="sc">%>%</span></span>
<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="cell">
<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>wmids_data <spanclass="ot"><-</span> wmids_data <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>wmids_data <spanclass="ot"><-</span><spanclass="fu">gather</span>(wmids_data)</span></code><buttontitle="Copy to Clipboard"class="code-copy-button"><iclass="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>
<h4data-number="2.2.0.1"class="anchored"data-anchor-id="base-r"><spanclass="header-section-number">2.2.0.1</span> Base R</h4>
<divclass="cell">
<divclass="sourceCode cell-code"id="cb15"><preclass="sourceCode r code-with-copy"><codeclass="sourceCode r"><spanid="cb15-1"><ahref="#cb15-1"aria-hidden="true"tabindex="-1"></a>df <spanclass="ot"><-</span> wmids_data[<spanclass="fu">order</span>(wmids_data<spanclass="sc">$</span>value,<spanclass="at">decreasing =</span><spanclass="cn">TRUE</span>),]</span>
<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="cb16-3"><ahref="#cb16-3"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>
<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><spanclass="co"># with sorting added in</span></span>
<spanid="cb17-2"><ahref="#cb17-2"aria-hidden="true"tabindex="-1"></a><spanclass="fu">ggplot</span>(wmids_data, <spanclass="fu">aes</span>(<spanclass="at">x=</span><spanclass="fu">reorder</span>(key,<spanclass="sc">-</span>value),value)) <spanclass="sc">+</span><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>