mirror of
https://github.com/kidwellj/hacking_religion_textbook.git
synced 2024-11-01 01:12:20 +00:00
1214 lines
111 KiB
HTML
1214 lines
111 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||
|
||
<meta charset="utf-8">
|
||
<meta name="generator" content="quarto-1.3.450">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||
|
||
|
||
<title>Hacking Religion: TRS & Data Science in Action - 2 Survey Data: Spotlight Project</title>
|
||
<style>
|
||
code{white-space: pre-wrap;}
|
||
span.smallcaps{font-variant: small-caps;}
|
||
div.columns{display: flex; gap: min(4vw, 1.5em);}
|
||
div.column{flex: auto; overflow-x: auto;}
|
||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||
ul.task-list{list-style: none;}
|
||
ul.task-list li input[type="checkbox"] {
|
||
width: 0.8em;
|
||
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
|
||
vertical-align: middle;
|
||
}
|
||
/* CSS for syntax highlighting */
|
||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||
.sourceCode { overflow: visible; }
|
||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||
div.sourceCode { margin: 1em 0; }
|
||
pre.sourceCode { margin: 0; }
|
||
@media screen {
|
||
div.sourceCode { overflow: auto; }
|
||
}
|
||
@media print {
|
||
pre > code.sourceCode { white-space: pre-wrap; }
|
||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||
}
|
||
pre.numberSource code
|
||
{ counter-reset: source-line 0; }
|
||
pre.numberSource code > span
|
||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||
pre.numberSource code > span > a:first-child::before
|
||
{ content: counter(source-line);
|
||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||
border: none; display: inline-block;
|
||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||
-khtml-user-select: none; -moz-user-select: none;
|
||
-ms-user-select: none; user-select: none;
|
||
padding: 0 4px; width: 4em;
|
||
}
|
||
pre.numberSource { margin-left: 3em; padding-left: 4px; }
|
||
div.sourceCode
|
||
{ }
|
||
@media screen {
|
||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||
}
|
||
/* CSS for citations */
|
||
div.csl-bib-body { }
|
||
div.csl-entry {
|
||
clear: both;
|
||
}
|
||
.hanging-indent div.csl-entry {
|
||
margin-left:2em;
|
||
text-indent:-2em;
|
||
}
|
||
div.csl-left-margin {
|
||
min-width:2em;
|
||
float:left;
|
||
}
|
||
div.csl-right-inline {
|
||
margin-left:2em;
|
||
padding-left:1em;
|
||
}
|
||
div.csl-indent {
|
||
margin-left: 2em;
|
||
}</style>
|
||
|
||
|
||
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
|
||
<script src="site_libs/quarto-nav/headroom.min.js"></script>
|
||
<script src="site_libs/clipboard/clipboard.min.js"></script>
|
||
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
|
||
<script src="site_libs/quarto-search/fuse.min.js"></script>
|
||
<script src="site_libs/quarto-search/quarto-search.js"></script>
|
||
<meta name="quarto:offset" content="./">
|
||
<link href="./chapter_3.html" rel="next">
|
||
<link href="./chapter_1.html" rel="prev">
|
||
<script src="site_libs/quarto-html/quarto.js"></script>
|
||
<script src="site_libs/quarto-html/popper.min.js"></script>
|
||
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
|
||
<script src="site_libs/quarto-html/anchor.min.js"></script>
|
||
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
|
||
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
|
||
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
|
||
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
|
||
<script id="quarto-search-options" type="application/json">{
|
||
"location": "sidebar",
|
||
"copy-button": false,
|
||
"collapse-after": 3,
|
||
"panel-placement": "start",
|
||
"type": "textbox",
|
||
"limit": 20,
|
||
"language": {
|
||
"search-no-results-text": "No results",
|
||
"search-matching-documents-text": "matching documents",
|
||
"search-copy-link-title": "Copy link to search",
|
||
"search-hide-matches-text": "Hide additional matches",
|
||
"search-more-match-text": "more match in this document",
|
||
"search-more-matches-text": "more matches in this document",
|
||
"search-clear-button-title": "Clear",
|
||
"search-detached-cancel-button-title": "Cancel",
|
||
"search-submit-button-title": "Submit",
|
||
"search-label": "Search"
|
||
}
|
||
}</script>
|
||
|
||
|
||
</head>
|
||
|
||
<body class="nav-sidebar floating slimcontent">
|
||
|
||
<div id="quarto-search-results"></div>
|
||
<header id="quarto-header" class="headroom fixed-top">
|
||
<nav class="quarto-secondary-nav">
|
||
<div class="container-fluid d-flex">
|
||
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
|
||
<i class="bi bi-layout-text-sidebar-reverse"></i>
|
||
</button>
|
||
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./chapter_2.html"><span class="chapter-number">2</span> <span class="chapter-title">Survey Data: Spotlight Project</span></a></li></ol></nav>
|
||
<a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
|
||
</a>
|
||
<button type="button" class="btn quarto-search-button" aria-label="" onclick="window.quartoOpenSearch();">
|
||
<i class="bi bi-search"></i>
|
||
</button>
|
||
</div>
|
||
</nav>
|
||
</header>
|
||
<!-- content -->
|
||
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
|
||
<!-- sidebar -->
|
||
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal sidebar-navigation floating overflow-auto">
|
||
<div class="pt-lg-2 mt-2 text-left sidebar-header">
|
||
<div class="sidebar-title mb-0 py-0">
|
||
<a href="./">Hacking Religion: TRS & Data Science in Action</a>
|
||
</div>
|
||
</div>
|
||
<div class="mt-2 flex-shrink-0 align-items-center">
|
||
<div class="sidebar-search">
|
||
<div id="quarto-search" class="" title="Search"></div>
|
||
</div>
|
||
</div>
|
||
<div class="sidebar-menu-container">
|
||
<ul class="list-unstyled mt-1">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./index.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text">Introduction: Hacking Religion</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./chapter_1.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">The 2021 UK Census</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./chapter_2.html" class="sidebar-item-text sidebar-link active">
|
||
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">Survey Data: Spotlight Project</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./chapter_3.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">Mapping churches: geospatial data science</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./chapter_4.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Data scraping, corpus analysis and wordclouds</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./chapter_5.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">What’s next?</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./summary.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text"><span class="chapter-number">6</span> <span class="chapter-title">Summary</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="./references.html" class="sidebar-item-text sidebar-link">
|
||
<span class="menu-text">References</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
<div id="quarto-sidebar-glass" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass"></div>
|
||
<!-- margin-sidebar -->
|
||
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
|
||
<nav id="TOC" role="doc-toc" class="toc-active">
|
||
<h2 id="toc-title">Table of contents</h2>
|
||
|
||
<ul>
|
||
<li><a href="#loading-in-some-data" id="toc-loading-in-some-data" class="nav-link active" data-scroll-target="#loading-in-some-data"><span class="header-section-number">2.1</span> Loading in some data</a></li>
|
||
<li><a href="#how-can-you-ask-about-religion" id="toc-how-can-you-ask-about-religion" class="nav-link" data-scroll-target="#how-can-you-ask-about-religion"><span class="header-section-number">2.2</span> How can you ask about religion?</a>
|
||
<ul class="collapse">
|
||
<li><a href="#what-is-your-religion" id="toc-what-is-your-religion" class="nav-link" data-scroll-target="#what-is-your-religion"><span class="header-section-number">2.2.1</span> “What is your religion?”</a></li>
|
||
<li><a href="#how-religious-would-you-say-you-are" id="toc-how-religious-would-you-say-you-are" class="nav-link" data-scroll-target="#how-religious-would-you-say-you-are"><span class="header-section-number">2.2.2</span> “How religious would you say you are?”</a></li>
|
||
<li><a href="#participation-in-worship" id="toc-participation-in-worship" class="nav-link" data-scroll-target="#participation-in-worship"><span class="header-section-number">2.2.3</span> Participation in Worship</a></li>
|
||
<li><a href="#spirituality" id="toc-spirituality" class="nav-link" data-scroll-target="#spirituality"><span class="header-section-number">2.2.4</span> Spirituality</a></li>
|
||
</ul></li>
|
||
<li><a href="#exploring-data-around-religious-affiliation" id="toc-exploring-data-around-religious-affiliation" class="nav-link" data-scroll-target="#exploring-data-around-religious-affiliation"><span class="header-section-number">2.3</span> Exploring data around religious affiliation:</a></li>
|
||
<li><a href="#working-with-a-continum-religiosity-and-spirituality" id="toc-working-with-a-continum-religiosity-and-spirituality" class="nav-link" data-scroll-target="#working-with-a-continum-religiosity-and-spirituality"><span class="header-section-number">2.4</span> Working With a Continum: Religiosity and Spirituality</a></li>
|
||
<li><a href="#references" id="toc-references" class="nav-link" data-scroll-target="#references">References</a></li>
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
<!-- main -->
|
||
<main class="content page-columns page-full" id="quarto-document-content">
|
||
|
||
<header id="title-block-header" class="quarto-title-block default">
|
||
<div class="quarto-title">
|
||
<h1 class="title"><span class="chapter-number">2</span> <span class="chapter-title">Survey Data: Spotlight Project</span></h1>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="quarto-title-meta">
|
||
|
||
|
||
|
||
|
||
</div>
|
||
|
||
|
||
</header>
|
||
|
||
<p>In the last chapter we explored some high level data about religion in the UK. This was a census sample, which usually refers to an attempt to get as comprehensive a sample as possible. But this is actually fairly unusual in practice. Depending on how complex a subject is, and how representative we want our data to be, it’s much more common to use selective sampling, that is survey responses at n=100 or n=1000 at a maximum. The advantage of a census sample is that you can explore how a wide range of other factors - particularly demographics - intersect with your question. And this can be really valuable in the study of religion, particularly as you will see as we go along that responses to some questions are more strongly correlated to things like economic status or educational attainment than they are to religious affiliation. It can be hard to tell if this is the case unless you have enough of a sample to break down into a number of different kinds of subsets. But census samples are complex and expensive to gather, so they’re quite rare in practice.</p>
|
||
<p>For this chapter, I’m going to walk you through a data set that a colleague (Charles Ogunbode) and I collected in 2021. Another problem with smaller, more selective samples is that researchers can often undersample minoritised ethnic groups. This is particularly the case with climate change research. Until the time we conducted this research, there had not been a single study investigating the specific experiences of people of colour in relation to climate change in the UK. Past researchers had been content to work with large samples, and assumed that if they had done 1000 surveys and 50 of these were completed by people of colour, they could “tick” the box. But 5% is actually well below levels of representation in the UK generally, and even more sharply the case for specific communities. And if we bear in mind that non-white respondents are (of course!) a highly heterogenous group, we’re even more behind in terms of collecting data that can improve our knowledge. Up until recently researchers just haven’t been paying close enough attention to catch the significant neglect of the empirical field that this represents.</p>
|
||
<p>While I’ve framed my comments above in terms of climate change research, it is also the case that, especially in diverse societies like the USA, Canada, the UK etc., paying attention to non-majority groups and people and communities of colour automatically draws in a strongly religious sample. This is highlighted in one recent study done in the UK, the “<a href="https://www.cam.ac.uk/stories/black-british-voices-report">Black British Voices Report</a>” in which the researchers observed that “84% of respondents described themselves as religious and/or spiritual”. My comments above in terms of controlling for other factors remains important here - these same researchers also note that “despire their significant important to the lives of Black Britons, only 7% of survey respondents reported that their religion was more defining of their identity than their race”.</p>
|
||
<p>We’ve decided to open up access to our data and I’m highlighting it in this book because it’s a unique opportunitiy to explore a dataset that emphasises diversity from the start, and by extension, provides some really interesting ways to use data science techniques to explore religion in the UK.</p>
|
||
<section id="loading-in-some-data" class="level2" data-number="2.1">
|
||
<h2 data-number="2.1" class="anchored" data-anchor-id="loading-in-some-data"><span class="header-section-number">2.1</span> Loading in some data</h2>
|
||
<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="co"># R Setup -----------------------------------------------------------------</span></span>
|
||
<span id="cb1-2"><a href="#cb1-2" 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-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(here) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co"># used for importing SPSS .sav files</span></span>
|
||
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(haven) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>here<span class="sc">::</span><span class="fu">i_am</span>(<span class="st">"chapter_2.qmd"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion</code></pre>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> <span class="fu">read_sav</span>(<span class="fu">here</span>(<span class="st">"example_data"</span>, <span class="st">"climate_experience_data.sav"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>The first thing to note here is that we’ve drawn in a different type of data file, this time from an <code>.sav</code> file, usully produced by the statistics software package SPSS. This uses a different R Library (I use <code>haven</code> for this). The upside is that in some cases where you have survey data with both a code and a value like “1” is eqivalent to “very much agree” this will preserve both in the R dataframe that is created. Now that you’ve loaded in data, you have a new R dataframe called “climate_experience_data” with a lot of columns with just under 1000 survey responses.</p>
|
||
</section>
|
||
<section id="how-can-you-ask-about-religion" class="level2" data-number="2.2">
|
||
<h2 data-number="2.2" class="anchored" data-anchor-id="how-can-you-ask-about-religion"><span class="header-section-number">2.2</span> How can you ask about religion?</h2>
|
||
<p>One of the challenges we faced when running this study is how to gather responsible data from surveys regarding religious identity. We’ll dive into this in depth as we do analysis and look at some of the agreements and conflicts in terms of respondent attribution. Just to set the stage, we used the following kinds of question to ask about religion and spirituality:</p>
|
||
<section id="what-is-your-religion" class="level3" data-number="2.2.1">
|
||
<h3 data-number="2.2.1" class="anchored" data-anchor-id="what-is-your-religion"><span class="header-section-number">2.2.1</span> “What is your religion?”</h3>
|
||
<ol type="1">
|
||
<li>Question 56 asks respondents simply, “What is your religion?” and then provides a range of possible answers. We included follow-up questions regarding denomination for respondents who indicated they were “Christian” or “Muslim”. For respondents who ticked “Christian” we asked, “What is your denomination?” nad for respondents who ticked “Muslim” we asked “Which of the following would you identify with?” and then left a range of possible options which could be ticked such as “Sunni,” “Shia,” “Sufi” etc.</li>
|
||
</ol>
|
||
<p>This is one way of measuring religion, that is, to ask a person if they consider themselves formally affiliated with a particular group. This kind of question has some (serious) limitations, but we’ll get to that in a moment.</p>
|
||
</section>
|
||
<section id="how-religious-would-you-say-you-are" class="level3" data-number="2.2.2">
|
||
<h3 data-number="2.2.2" class="anchored" data-anchor-id="how-religious-would-you-say-you-are"><span class="header-section-number">2.2.2</span> “How religious would you say you are?”</h3>
|
||
<p>We also asked respondents (Q57): “Regardless of whether you belong to a particular religion, how religious would you say you are?” and then provided a slider from 0 (not religious at all) to 10 (very religious).</p>
|
||
</section>
|
||
<section id="participation-in-worship" class="level3" data-number="2.2.3">
|
||
<h3 data-number="2.2.3" class="anchored" data-anchor-id="participation-in-worship"><span class="header-section-number">2.2.3</span> Participation in Worship</h3>
|
||
<p>We included some classic indicators about how often respondents go to worship (Q58): Apart from weddings, funerals and other special occasions, how often do you attend religious services? and (Q59): “Q59 Apart from when you are at religious services, how often do you pray?”</p>
|
||
<ul>
|
||
<li>More than once a week (1)</li>
|
||
<li>Once a week (2)</li>
|
||
<li>At least once a month (3)</li>
|
||
<li>Only on special holy days (4)</li>
|
||
<li>Never (5)</li>
|
||
</ul>
|
||
<p>Each of these measures a particular kind of dimension, and it is interesting to note that sometimes there are stronger correlations between how often a person attends worship services (weekly versus once a year) and a particular view, than there is between their affiliation (if they are Christian or Pagan). We’ll do some exploratory work shortly to see how this is the case in our sample.</p>
|
||
</section>
|
||
<section id="spirituality" class="level3" data-number="2.2.4">
|
||
<h3 data-number="2.2.4" class="anchored" data-anchor-id="spirituality"><span class="header-section-number">2.2.4</span> Spirituality</h3>
|
||
<p>We also included a series of questions about spirituality in Q52 and used a slightly overlapping nature relatedness scale Q51.</p>
|
||
<p>You’ll find that many surveys will only use one of these forms of question and ignore the rest. I think this is a really bad idea as religious belonging, identity, and spirituality are far too complex to work off a single form of response. We can also test out how these different attributions relate to other demographic features, like interest in politics, economic attainment, etc.</p>
|
||
<div class="callout callout-style-default callout-tip callout-titled">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon"></i>
|
||
</div>
|
||
<div class="callout-title-container flex-fill">
|
||
So <em>Who’s</em> Religious?
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>As I’ve already hinted in the previous chapter, measuring religiosity is complicated. I suspect some readers may be wondering something like, “what’s the right question to ask?” here. Do we get the most accurate representation by asking people to self-report their religious affiliation? Or is it more accurate to ask individuals to report on how religious they are? Is it, perhaps, better to assume that the indirect query about practice, e.g. how frequently one attends services at a place of worship may be the most reliable proxy?</p>
|
||
<p>Highlight challenges of various approaches pointing to literature.</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="exploring-data-around-religious-affiliation" class="level2" data-number="2.3">
|
||
<h2 data-number="2.3" class="anchored" data-anchor-id="exploring-data-around-religious-affiliation"><span class="header-section-number">2.3</span> Exploring data around religious affiliation:</h2>
|
||
<p>Let’s dive into the data and see how this all works out. We’ll start with the question 56 data, around religious affiliation:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="annotated-cell-3"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-3-1"><a href="#annotated-cell-3-1" aria-hidden="true" tabindex="-1"></a>religious_affiliation <span class="ot"><-</span> <span class="fu">as_tibble</span>(<span class="fu">as_factor</span>(climate_experience_data<span class="sc">$</span>Q56))</span>
|
||
<span id="annotated-cell-3-2"><a href="#annotated-cell-3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(religious_affiliation) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"response"</span>)</span>
|
||
<span id="annotated-cell-3-3"><a href="#annotated-cell-3-3" aria-hidden="true" tabindex="-1"></a>religious_affiliation <span class="ot"><-</span> <span class="fu">filter</span>(religious_affiliation, <span class="sc">!</span><span class="fu">is.na</span>(response))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>There are few things we need to do here to get the data into initial proper shape. This might be called “cleaning” the data:</p>
|
||
<ol type="1">
|
||
<li>Because we imported this data from an SPSS <code>.sav</code> file format using the R <code>haven()</code> library, we need to start by adapting the data into a format that our visualation engine ggplot can handle (a dataframe).</li>
|
||
<li>Next we’ll rename the columns so these names are a bit more useful.</li>
|
||
<li>We need to omit non-responses so these don’t mess with the counting (these are <code>NA</code> in R)</li>
|
||
</ol>
|
||
<p>If we pause at this point to view the data, you’ll see it’s basically just a long list of survey responses. What we need is a count of each unique response (or <code>factor</code>). This will take a few more steps:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="annotated-cell-4"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-4-1"><a href="#annotated-cell-4-1" aria-hidden="true" tabindex="-1"></a>religious_affiliation_sums <span class="ot"><-</span> religious_affiliation <span class="sc">%>%</span> </span>
|
||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-4-2" class="code-annotation-target"><a href="#annotated-cell-4-2" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">count</span>(response, <span class="at">sort =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
|
||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-4-3" class="code-annotation-target"><a href="#annotated-cell-4-3" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">response =</span> forcats<span class="sc">::</span><span class="fu">fct_rev</span>(forcats<span class="sc">::</span><span class="fu">fct_inorder</span>(response)))</span>
|
||
<span id="annotated-cell-4-4"><a href="#annotated-cell-4-4" aria-hidden="true" tabindex="-1"></a>religious_affiliation_sums <span class="ot"><-</span> religious_affiliation_sums <span class="sc">%>%</span> </span>
|
||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-4-5" class="code-annotation-target"><a href="#annotated-cell-4-5" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(n <span class="sc">/</span> <span class="fu">sum</span>(n), <span class="at">accuracy =</span> .<span class="dv">1</span>, <span class="at">trim =</span> <span class="cn">FALSE</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-4" data-target-annotation="1">1</dt>
|
||
<dd>
|
||
<span data-code-lines="2" data-code-cell="annotated-cell-4" data-code-annotation="1">First we generate new a dataframe with sums per category and</span>
|
||
</dd>
|
||
<dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt>
|
||
<dd>
|
||
<span data-code-lines="3" data-code-cell="annotated-cell-4" data-code-annotation="2">…sort in descending order</span>
|
||
</dd>
|
||
<dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt>
|
||
<dd>
|
||
<span data-code-lines="5" data-code-cell="annotated-cell-4" data-code-annotation="3">Then we add new column with percentages based on the sums you’ve just generated</span>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
<p>That should give us a tidy table of results, which you can see if you view the contents of our new <code>religious_affiliation_sums</code> dataframe:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(religious_affiliation_sums)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code># A tibble: 6 x 3
|
||
response n perc
|
||
<fct> <int> <chr>
|
||
1 Christian 342 "33.9%"
|
||
2 Muslim 271 "26.9%"
|
||
3 No religion 108 "10.7%"
|
||
4 Hindu 72 " 7.1%"
|
||
5 Atheist 54 " 5.4%"
|
||
6 Spiritual but not religious 38 " 3.8%"</code></pre>
|
||
</div>
|
||
</div>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># make plot</span></span>
|
||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religious_affiliation_sums, <span class="fu">aes</span>(<span class="at">x =</span> n, <span class="at">y =</span> response)) <span class="sc">+</span></span>
|
||
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>) <span class="sc">+</span> </span>
|
||
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="do">## add percentage labels</span></span>
|
||
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> perc),</span>
|
||
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="do">## make labels left-aligned and white</span></span>
|
||
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> <span class="at">hjust =</span> <span class="dv">1</span>, <span class="at">nudge_x =</span> <span class="sc">-</span>.<span class="dv">5</span>, <span class="at">colour =</span> <span class="st">"white"</span>, <span class="at">size=</span><span class="dv">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>I’ve added one feature to our chart that wasn’t in the bar charts in chapter 1, text labels with the actual value on each bar.</p>
|
||
<p>You may be thinking about the plots we’ve just finished in chapter 1 and wondering how they compare. Let’s use the same facet approach that we’ve just used to render this data in a subsetted way.</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="annotated-cell-7"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-7-1"><a href="#annotated-cell-7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># First we need to add in data on ethnic self-identification from our respondents:</span></span>
|
||
<span id="annotated-cell-7-2"><a href="#annotated-cell-7-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">select</span>(climate_experience_data, Q56, Q0)</span>
|
||
<span id="annotated-cell-7-3"><a href="#annotated-cell-7-3" aria-hidden="true" tabindex="-1"></a>religious_affiliation_ethnicity <span class="ot"><-</span> <span class="fu">as_tibble</span>(<span class="fu">as_factor</span>(df))</span>
|
||
<span id="annotated-cell-7-4"><a href="#annotated-cell-7-4" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(religious_affiliation_ethnicity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Religion"</span>, <span class="st">"Ethnicity"</span>)</span>
|
||
<span id="annotated-cell-7-5"><a href="#annotated-cell-7-5" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="annotated-cell-7-6"><a href="#annotated-cell-7-6" aria-hidden="true" tabindex="-1"></a>religious_affiliation_ethnicity_sums <span class="ot"><-</span> religious_affiliation_ethnicity <span class="sc">%>%</span> </span>
|
||
<span id="annotated-cell-7-7"><a href="#annotated-cell-7-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(Ethnicity) <span class="sc">%>%</span></span>
|
||
<span id="annotated-cell-7-8"><a href="#annotated-cell-7-8" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">count</span>(Religion, <span class="at">sort =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
|
||
<span id="annotated-cell-7-9"><a href="#annotated-cell-7-9" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">Religion =</span> forcats<span class="sc">::</span><span class="fu">fct_rev</span>(forcats<span class="sc">::</span><span class="fu">fct_inorder</span>(Religion)))</span>
|
||
<span id="annotated-cell-7-10"><a href="#annotated-cell-7-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="annotated-cell-7-11"><a href="#annotated-cell-7-11" aria-hidden="true" tabindex="-1"></a>plot1 <span class="ot"><-</span> <span class="fu">ggplot</span>(religious_affiliation_ethnicity_sums, <span class="fu">aes</span>(<span class="at">x =</span> n, <span class="at">y =</span> Religion)) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-7-12"><a href="#annotated-cell-7-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>) <span class="sc">+</span> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Ethnicity, <span class="at">scales=</span><span class="st">"free_x"</span>)</span>
|
||
<span id="annotated-cell-7-13"><a href="#annotated-cell-7-13" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="annotated-cell-7-14"><a href="#annotated-cell-7-14" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"chart.png"</span>, <span class="at">plot=</span>plot1, <span class="at">width =</span> <span class="dv">8</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units=</span><span class="fu">c</span>(<span class="st">"in"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="working-with-a-continum-religiosity-and-spirituality" class="level2 page-columns page-full" data-number="2.4">
|
||
<h2 data-number="2.4" class="anchored" data-anchor-id="working-with-a-continum-religiosity-and-spirituality"><span class="header-section-number">2.4</span> Working With a Continum: Religiosity and Spirituality</h2>
|
||
<p>So far we’ve just worked with bar plots, but there are a lot of other possible visualisations and types of data which demand them.</p>
|
||
<p>As I’ve mentioned above, on this survey we also asked respondents to tell us on by rating themselves on a scale of 0-10 with 0 being “not religious at all” and 10 being “very religious” in response to the question, “Regardless of whether you belong to a particular religion, how religious would you say you are?”</p>
|
||
<p>We’ll recycle some code from our previous import to bring in the Q57 data:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="annotated-cell-8"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-8-1"><a href="#annotated-cell-8-1" aria-hidden="true" tabindex="-1"></a>religiosity <span class="ot"><-</span> <span class="fu">as_tibble</span>(<span class="fu">as_factor</span>(climate_experience_data<span class="sc">$</span>Q57_1))</span>
|
||
<span id="annotated-cell-8-2"><a href="#annotated-cell-8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(religiosity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"response"</span>)</span>
|
||
<span id="annotated-cell-8-3"><a href="#annotated-cell-8-3" aria-hidden="true" tabindex="-1"></a>religiosity <span class="ot"><-</span> <span class="fu">filter</span>(religiosity, <span class="sc">!</span><span class="fu">is.na</span>(response))</span>
|
||
<span id="annotated-cell-8-4"><a href="#annotated-cell-8-4" aria-hidden="true" tabindex="-1"></a>religiosity_sums <span class="ot"><-</span> religiosity <span class="sc">%>%</span> </span>
|
||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-8" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-8-5" class="code-annotation-target"><a href="#annotated-cell-8-5" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">count</span>(response) <span class="sc">%>%</span></span>
|
||
<span id="annotated-cell-8-6"><a href="#annotated-cell-8-6" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">response =</span> forcats<span class="sc">::</span><span class="fu">fct_rev</span>(forcats<span class="sc">::</span><span class="fu">fct_inorder</span>(response)))</span>
|
||
<span id="annotated-cell-8-7"><a href="#annotated-cell-8-7" aria-hidden="true" tabindex="-1"></a>religiosity_sums <span class="ot"><-</span> religiosity_sums <span class="sc">%>%</span> </span>
|
||
<span id="annotated-cell-8-8"><a href="#annotated-cell-8-8" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(n <span class="sc">/</span> <span class="fu">sum</span>(n), <span class="at">accuracy =</span> .<span class="dv">1</span>, <span class="at">trim =</span> <span class="cn">FALSE</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-8" data-target-annotation="1">1</dt>
|
||
<dd>
|
||
<span data-code-lines="5" data-code-cell="annotated-cell-8" data-code-annotation="1">Note: we have removed <code>sort = TRUE</code> in the above statement as it will enforce sorting the data by quantities rather than the factor order. It wouldn’t really make sense to plot this chart in the order of response.</span>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
<p>Now, let’s plot that data:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="annotated-cell-9"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-9-1"><a href="#annotated-cell-9-1" aria-hidden="true" tabindex="-1"></a>caption <span class="ot"><-</span> <span class="st">"Respondent Religiosity"</span></span>
|
||
<span id="annotated-cell-9-2"><a href="#annotated-cell-9-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span></span>
|
||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-9" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-9-3" class="code-annotation-target"><a href="#annotated-cell-9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-9-4"><a href="#annotated-cell-9-4" aria-hidden="true" tabindex="-1"></a> <span class="do">## get rid of all elements except y axis labels + adjust plot margin</span></span>
|
||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-9" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-9-5" class="code-annotation-target"><a href="#annotated-cell-9-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
|
||
<span id="annotated-cell-9-6"><a href="#annotated-cell-9-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-9-7"><a href="#annotated-cell-9-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption)</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-9" data-target-annotation="1">1</dt>
|
||
<dd>
|
||
<span data-code-lines="3" data-code-cell="annotated-cell-9" data-code-annotation="1">We’ve added colors, because colours are fun.</span>
|
||
</dd>
|
||
<dt data-target-cell="annotated-cell-9" data-target-annotation="2">2</dt>
|
||
<dd>
|
||
<span data-code-lines="5" data-code-cell="annotated-cell-9" data-code-annotation="2">Also new here is <code>coord_flip</code> to rotate the chart so we have bars going horizontally</span>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>Since we’re thinking about how things look just now, let’s play with themes for a minute. <code>ggplot</code> is a really powerful tool for visualising information, but it also has some quite nice features for making things look pretty.</p>
|
||
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">If you’d like to take a proper deep dive on all this theme stuff, R-Charts has a great set of examples showing you how a number of different theme packages look in practice, <a href="https://r-charts.com/ggplot2/themes/">“R-Charts on Themes”</a>.</span></div></div>
|
||
<p>R has a number of built-in themes, but these are mostly driven by functional concerns, such as whether you might want to print your chart or have a less heavy look overall. So for example you might use <code>theme_light()</code> in the following way:</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>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-10-2"><a href="#annotated-cell-10-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-10-3"><a href="#annotated-cell-10-3" aria-hidden="true" tabindex="-1"></a> <span class="do">## get rid of all elements except y axis labels + adjust plot margin</span></span>
|
||
<span id="annotated-cell-10-4"><a href="#annotated-cell-10-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
|
||
<span id="annotated-cell-10-5"><a href="#annotated-cell-10-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-10-6"><a href="#annotated-cell-10-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption) <span class="sc">+</span></span>
|
||
<span id="annotated-cell-10-7"><a href="#annotated-cell-10-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_light</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>You can also use additional packages like <code>ggthemes()</code> or <code>hrbrthemes()</code> so for example we might want to try the <code>pander</code> theme which has it’s own special (and very cheerful) colour palette.</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggthemes) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span> <span class="fu">labs</span>(<span class="at">caption =</span> caption) <span class="sc">+</span> </span>
|
||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_pander</span>() <span class="sc">+</span></span>
|
||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_pander</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>Or, you might try the well-crafted typgraphy from <code>hbrthemes</code> in the <code>theme_ipsum_pub</code> theme:</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><span class="fu">library</span>(hrbrthemes) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span> <span class="fu">labs</span>(<span class="at">caption =</span> caption) <span class="sc">+</span> </span>
|
||
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_ipsum_pub</span>() <span class="sc">+</span></span>
|
||
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_pander</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>We’re going to come back to this chart, but let’s set it to one side for a moment and build up a visualisation of the spirituality scale data. The spirituality scale questions come from research by ___ and ___. These researchers developed a series of questions which they asked respondents in a survey. The advantage here is that you’re getting at the question of spirituality from a lot of different angles, and then you combine the scores from all the questions to get a mean “spirituality score”.</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><span class="do">### Spirituality scale --------------------------------------------------------------</span></span>
|
||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate overall mean spirituality score based on six questions:</span></span>
|
||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>climate_experience_data<span class="sc">$</span>Q52_score <span class="ot"><-</span> <span class="fu">rowMeans</span>(<span class="fu">select</span>(climate_experience_data, Q52a_1<span class="sc">:</span>Q52f_1))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>Like we did in chapter 1, let’s start by exploring the data and get a bit of a sense of the character of the responses overall. One good place to start is to find out the mean response for our two continum questions:</p>
|
||
<div class="cell">
|
||
<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><span class="co"># t_testing and means</span></span>
|
||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Spirituality scale</span></span>
|
||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="co"># JK note to self: need to fix stat_summary plot here</span></span>
|
||
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="co"># stat_summary(climate_experience_data$Q52_score)</span></span>
|
||
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q52_score)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>[1] 6.047454</code></pre>
|
||
</div>
|
||
<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><span class="co"># Q57 Regardless of whether you belong to a particular religion, how religious would you say you are?</span></span>
|
||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 0-10, Not religious at all => Very religious; mean=5.58</span></span>
|
||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q57_1) <span class="co"># religiosity</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>[1] 5.581349</code></pre>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Q58 Apart from weddings, funerals and other special occasions, how often do you attend religious services?</span></span>
|
||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="co"># coded at 1-5, lower value = stronger mean=3.439484</span></span>
|
||
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q58) <span class="co"># service attendance</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>[1] 3.439484</code></pre>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Q59 Apart from when you are at religious services, how often do you pray?</span></span>
|
||
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="co"># coded at 1-5, lower = stronger mean=2.50496</span></span>
|
||
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q59)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stdout">
|
||
<pre><code>[1] 2.50496</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Now let’s try out some visualisations:</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="do">## Q52 Spirituality data ------------------------</span></span>
|
||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>q52_data <span class="ot"><-</span> <span class="fu">select</span>(climate_experience_data, Q52a_1<span class="sc">:</span>Q52f_1)</span>
|
||
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Data is at wide format, we need to make it 'tidy' or 'long'</span></span>
|
||
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>q52_data <span class="ot"><-</span> q52_data <span class="sc">%>%</span> </span>
|
||
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(<span class="at">key=</span><span class="st">"text"</span>, <span class="at">value=</span><span class="st">"value"</span>) <span class="sc">%>%</span></span>
|
||
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># rename columns</span></span>
|
||
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52_"</span>, <span class="st">""</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">value =</span> <span class="fu">round</span>(<span class="fu">as.numeric</span>(value),<span class="dv">0</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>Warning: attributes are not identical across measure variables; they will be
|
||
dropped</code></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><span class="co"># Change names of rows to question text</span></span>
|
||
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>q52_data <span class="ot"><-</span> q52_data <span class="sc">%>%</span> </span>
|
||
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(<span class="at">key=</span><span class="st">"text"</span>, <span class="at">value=</span><span class="st">"value"</span>) <span class="sc">%>%</span></span>
|
||
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># rename columns</span></span>
|
||
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52a_1"</span>, <span class="st">"In terms of questions I have about my life, my spirituality answers..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52b_1"</span>, <span class="st">"Growing spiritually is important..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52c_1"</span>, <span class="st">"When I<e2><80><99>m faced with an important decision, spirituality plays a role..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52d_1"</span>, <span class="st">"Spirituality is part of my life..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52e_1"</span>, <span class="st">"When I think of things that help me grow and mature as a person, spirituality has an effect on my personal growth..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52f_1"</span>, <span class="st">"My spiritual beliefs affect aspects of my life..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>))</span>
|
||
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot</span></span>
|
||
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Used for gradient colour schemes, as with violin plots</span></span>
|
||
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(viridis) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>Loading required package: viridisLite</code></pre>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>q52_plot <span class="ot"><-</span> q52_data <span class="sc">%>%</span></span>
|
||
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%>%</span> <span class="co"># Reorder data</span></span>
|
||
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>text, <span class="at">y=</span>value, <span class="at">fill=</span>text, <span class="at">color=</span>text)) <span class="sc">+</span></span>
|
||
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
|
||
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>, <span class="at">alpha=</span><span class="fl">0.8</span>) <span class="sc">+</span></span>
|
||
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_jitter</span>(<span class="at">color=</span><span class="st">"black"</span>, <span class="at">size=</span><span class="fl">0.2</span>, <span class="at">alpha=</span><span class="fl">0.2</span>) <span class="sc">+</span></span>
|
||
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
|
||
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position=</span><span class="st">"none"</span>, <span class="at">axis.text.y =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)) <span class="sc">+</span></span>
|
||
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="co"># This switch X and Y axis and allows to get the horizontal version</span></span>
|
||
<span id="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span></span>
|
||
<span id="cb22-11"><a href="#cb22-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Spirituality scales"</span>) <span class="sc">+</span></span>
|
||
<span id="cb22-12"><a href="#cb22-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> <span class="cf">function</span>(x) <span class="fu">str_wrap</span>(x, <span class="at">width =</span> <span class="dv">45</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><span class="co"># using gridExtra to specify explicit dimensions for printing</span></span>
|
||
<span id="cb22-15"><a href="#cb22-15" aria-hidden="true" tabindex="-1"></a>q52_plot</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid" width="672"></p>
|
||
</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><span class="fu">ggsave</span>(<span class="st">"figures/q52_boxplot.png"</span>, <span class="at">width =</span> <span class="dv">20</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units =</span> <span class="st">"cm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>There’s an enhanced version of this plot we can use, called <code>ggstatsplot()</code> to get a different view:</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><span class="co"># As an alternative trying ggstatsplot:</span></span>
|
||
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rstantools)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>This is rstantools version 2.3.1.1</code></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><span class="fu">library</span>(ggstatsplot)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>You can cite this package as:
|
||
Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
|
||
Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167</code></pre>
|
||
</div>
|
||
<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>q52_plot_alt <span class="ot"><-</span> <span class="fu">ggbetweenstats</span>(</span>
|
||
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> q52_data,</span>
|
||
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> text,</span>
|
||
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> value,</span>
|
||
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> <span class="at">outlier.tagging =</span> <span class="cn">TRUE</span>,</span>
|
||
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Intrinsic Spirituality Scale Responses"</span></span>
|
||
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a>) <span class="sc">+</span></span>
|
||
<span id="cb28-8"><a href="#cb28-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> <span class="cf">function</span>(x) <span class="fu">str_wrap</span>(x, <span class="at">width =</span> <span class="dv">30</span>)) <span class="sc">+</span></span>
|
||
<span id="cb28-9"><a href="#cb28-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Customizations</span></span>
|
||
<span id="cb28-10"><a href="#cb28-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
|
||
<span id="cb28-11"><a href="#cb28-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># Change fonts in the plot</span></span>
|
||
<span id="cb28-12"><a href="#cb28-12" aria-hidden="true" tabindex="-1"></a> <span class="at">text =</span> <span class="fu">element_text</span>(<span class="at">family =</span> <span class="st">"Helvetica"</span>, <span class="at">size =</span> <span class="dv">8</span>, <span class="at">color =</span> <span class="st">"black"</span>),</span>
|
||
<span id="cb28-13"><a href="#cb28-13" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.title =</span> <span class="fu">element_text</span>(</span>
|
||
<span id="cb28-14"><a href="#cb28-14" aria-hidden="true" tabindex="-1"></a> <span class="at">family =</span> <span class="st">"Abril Fatface"</span>, </span>
|
||
<span id="cb28-15"><a href="#cb28-15" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="dv">20</span>,</span>
|
||
<span id="cb28-16"><a href="#cb28-16" aria-hidden="true" tabindex="-1"></a> <span class="at">face =</span> <span class="st">"bold"</span>,</span>
|
||
<span id="cb28-17"><a href="#cb28-17" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"#2a475e"</span></span>
|
||
<span id="cb28-18"><a href="#cb28-18" aria-hidden="true" tabindex="-1"></a> ),</span>
|
||
<span id="cb28-19"><a href="#cb28-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># Statistical annotations below the main title</span></span>
|
||
<span id="cb28-20"><a href="#cb28-20" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.subtitle =</span> <span class="fu">element_text</span>(</span>
|
||
<span id="cb28-21"><a href="#cb28-21" aria-hidden="true" tabindex="-1"></a> <span class="at">family =</span> <span class="st">"Helvetica"</span>, </span>
|
||
<span id="cb28-22"><a href="#cb28-22" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="dv">12</span>, </span>
|
||
<span id="cb28-23"><a href="#cb28-23" aria-hidden="true" tabindex="-1"></a> <span class="at">face =</span> <span class="st">"bold"</span>,</span>
|
||
<span id="cb28-24"><a href="#cb28-24" aria-hidden="true" tabindex="-1"></a> <span class="at">color=</span><span class="st">"#1b2838"</span></span>
|
||
<span id="cb28-25"><a href="#cb28-25" aria-hidden="true" tabindex="-1"></a> ),</span>
|
||
<span id="cb28-26"><a href="#cb28-26" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.title.position =</span> <span class="st">"plot"</span>, <span class="co"># slightly different from default</span></span>
|
||
<span id="cb28-27"><a href="#cb28-27" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.text =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">10</span>, <span class="at">color =</span> <span class="st">"black"</span>),</span>
|
||
<span id="cb28-28"><a href="#cb28-28" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">7</span>),</span>
|
||
<span id="cb28-29"><a href="#cb28-29" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">12</span>),</span>
|
||
<span id="cb28-30"><a href="#cb28-30" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.line =</span> <span class="fu">element_line</span>(<span class="at">colour =</span> <span class="st">"grey50"</span>),</span>
|
||
<span id="cb28-31"><a href="#cb28-31" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid.minor =</span> <span class="fu">element_blank</span>(),</span>
|
||
<span id="cb28-32"><a href="#cb28-32" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid.major.x =</span> <span class="fu">element_blank</span>(),</span>
|
||
<span id="cb28-33"><a href="#cb28-33" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_line</span>(<span class="at">color =</span> <span class="st">"#b4aea9"</span>),</span>
|
||
<span id="cb28-34"><a href="#cb28-34" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid.major.y =</span> <span class="fu">element_line</span>(<span class="at">linetype =</span> <span class="st">"dashed"</span>),</span>
|
||
<span id="cb28-35"><a href="#cb28-35" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">"#fbf9f4"</span>, <span class="at">color =</span> <span class="st">"#fbf9f4"</span>),</span>
|
||
<span id="cb28-36"><a href="#cb28-36" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">"#fbf9f4"</span>, <span class="at">color =</span> <span class="st">"#fbf9f4"</span>)</span>
|
||
<span id="cb28-37"><a href="#cb28-37" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>Scale for x is already present.
|
||
Adding another scale for x, which will replace the existing scale.</code></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>q52_plot_alt</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/q52_plot_alt.png"</span>, <span class="at">width =</span> <span class="dv">20</span>, <span class="at">height =</span> <span class="dv">12</span>, <span class="at">units =</span> <span class="st">"cm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>One thing that might be interesting to test here is whether spirituality and religiosity are similar for our respondents.</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x=</span>Q52_score, <span class="at">y=</span>Q57_1)) <span class="sc">+</span> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Spirituality Scale Score"</span>, <span class="at">y =</span> <span class="st">"How Religious?"</span>) <span class="sc">+</span></span>
|
||
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size=</span><span class="dv">1</span>, <span class="at">alpha=</span><span class="fl">0.3</span>) <span class="sc">+</span> <span class="fu">geom_smooth</span>(<span class="at">method=</span><span class="st">"auto"</span>, <span class="at">se=</span><span class="cn">TRUE</span>, <span class="at">fullrange=</span><span class="cn">FALSE</span>, <span class="at">level=</span><span class="fl">0.95</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'</code></pre>
|
||
</div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co"># using http://sthda.com/english/wiki/ggplot2-scatter-plots-quick-start-guide-r-software-and-data-visualization</span></span>
|
||
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x=</span>Q52_score, <span class="at">y=</span>Q57_1)) <span class="sc">+</span></span>
|
||
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Spirituality Scale Score"</span>, <span class="at">y =</span> <span class="st">"How Religious?"</span>) <span class="sc">+</span></span>
|
||
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size=</span><span class="dv">1</span>, <span class="at">alpha=</span><span class="fl">0.3</span>) <span class="sc">+</span> <span class="fu">stat_density_2d</span>(<span class="fu">aes</span>(<span class="at">fill =</span> ..level..), <span class="at">geom=</span><span class="st">"polygon"</span>, <span class="at">alpha=</span><span class="fl">0.3</span>)<span class="sc">+</span></span>
|
||
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradient</span>(<span class="at">low=</span><span class="st">"blue"</span>, <span class="at">high=</span><span class="st">"red"</span>) <span class="sc">+</span></span>
|
||
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-stderr">
|
||
<pre><code>Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
|
||
i Please use `after_stat(level)` instead.</code></pre>
|
||
</div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-16-2.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
</div>
|
||
<p>Because the responses to these two questions, about spirituality and religiosity are on a continuum, we can also use them, like we did in previous charts, to subset other datasets. A simple way of doing this is to separate our respondents into “high,” “medium,” and “low” bins for the two questions. Rather than working with hard values, like assigning 0-3, 4-6 and 7-10 for low medium and high, we’ll work with the range of values that respondents actually chose. This is particularly appropriate as the median answer to these questions was not “5”. So we’ll use the statistical concept of standard deviation, which R can calculate almost magically for us, in the following way:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Create low/med/high bins based on Mean and +1/-1 Standard Deviation</span></span>
|
||
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> climate_experience_data <span class="sc">%>%</span></span>
|
||
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a> <span class="at">Q52_bin =</span> <span class="fu">case_when</span>(</span>
|
||
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a> Q52_score <span class="sc">></span> <span class="fu">mean</span>(Q52_score) <span class="sc">+</span> <span class="fu">sd</span>(Q52_score) <span class="sc">~</span> <span class="st">"high"</span>,</span>
|
||
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a> Q52_score <span class="sc"><</span> <span class="fu">mean</span>(Q52_score) <span class="sc">-</span> <span class="fu">sd</span>(Q52_score) <span class="sc">~</span> <span class="st">"low"</span>,</span>
|
||
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
|
||
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">factor</span>(<span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"low"</span>, <span class="st">"medium"</span>, <span class="st">"high"</span>))</span>
|
||
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a> )</span>
|
||
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a></span>
|
||
<span id="cb36-12"><a href="#cb36-12" aria-hidden="true" tabindex="-1"></a><span class="do">## Q57 subsetting based on Religiosity --------------------------------------------------------------</span></span>
|
||
<span id="cb36-13"><a href="#cb36-13" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> climate_experience_data <span class="sc">%>%</span></span>
|
||
<span id="cb36-14"><a href="#cb36-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||
<span id="cb36-15"><a href="#cb36-15" aria-hidden="true" tabindex="-1"></a> <span class="at">Q57_bin =</span> <span class="fu">case_when</span>(</span>
|
||
<span id="cb36-16"><a href="#cb36-16" aria-hidden="true" tabindex="-1"></a> Q57_1 <span class="sc">></span> <span class="fu">mean</span>(Q57_1) <span class="sc">+</span> <span class="fu">sd</span>(Q57_1) <span class="sc">~</span> <span class="st">"high"</span>,</span>
|
||
<span id="cb36-17"><a href="#cb36-17" aria-hidden="true" tabindex="-1"></a> Q57_1 <span class="sc"><</span> <span class="fu">mean</span>(Q57_1) <span class="sc">-</span> <span class="fu">sd</span>(Q57_1) <span class="sc">~</span> <span class="st">"low"</span>,</span>
|
||
<span id="cb36-18"><a href="#cb36-18" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
|
||
<span id="cb36-19"><a href="#cb36-19" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">factor</span>(<span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"low"</span>, <span class="st">"medium"</span>, <span class="st">"high"</span>))</span>
|
||
<span id="cb36-20"><a href="#cb36-20" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>As in the previous chapter, it’s useful to explore multiple factors when possible. So I’d like us to take the data about political affiliation to visualise alongside our religion and spirituality data. this will help us to see where effects are more or less significant and give us a point of comparison.</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="do">## Q53 subsetting based on Political LR orientation --------------------------------------------------------------</span></span>
|
||
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate low/med/high bins based on Mean and SD</span></span>
|
||
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> climate_experience_data <span class="sc">%>%</span></span>
|
||
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a> <span class="at">Q53_bin =</span> <span class="fu">case_when</span>(</span>
|
||
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a> Q53_1 <span class="sc">></span> <span class="fu">mean</span>(Q53_1) <span class="sc">+</span> <span class="fu">sd</span>(Q53_1) <span class="sc">~</span> <span class="st">"high"</span>,</span>
|
||
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a> Q53_1 <span class="sc"><</span> <span class="fu">mean</span>(Q53_1) <span class="sc">-</span> <span class="fu">sd</span>(Q53_1) <span class="sc">~</span> <span class="st">"low"</span>,</span>
|
||
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
|
||
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">factor</span>(<span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"low"</span>, <span class="st">"medium"</span>, <span class="st">"high"</span>))</span>
|
||
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
<p>Now let’s use those bins to explore some of the responses about attitudes towards climate change:</p>
|
||
<div class="cell">
|
||
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Faceted plot working with 3x3 grid</span></span>
|
||
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">select</span>(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q58)</span>
|
||
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(df) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Q52_bin"</span>, <span class="st">"Q53_bin"</span>, <span class="st">"Q57_bin"</span>, <span class="st">"response"</span>)</span>
|
||
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a>facet_names <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">`</span><span class="at">Q52_bin</span><span class="st">`</span> <span class="ot">=</span> <span class="st">"Spirituality"</span>, <span class="st">`</span><span class="at">Q53_bin</span><span class="st">`</span> <span class="ot">=</span> <span class="st">"Politics L/R"</span>, <span class="st">`</span><span class="at">Q57_bin</span><span class="st">`</span> <span class="ot">=</span> <span class="st">"Religiosity"</span>, <span class="st">`</span><span class="at">low</span><span class="st">`</span><span class="ot">=</span><span class="st">"low"</span>, <span class="st">`</span><span class="at">medium</span><span class="st">`</span><span class="ot">=</span><span class="st">"medium"</span>, <span class="st">`</span><span class="at">high</span><span class="st">`</span><span class="ot">=</span><span class="st">"high"</span>)</span>
|
||
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a>facet_labeller <span class="ot"><-</span> <span class="cf">function</span>(variable,value){<span class="fu">return</span>(facet_names[value])}</span>
|
||
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot"><-</span> <span class="fu">factor</span>(df<span class="sc">$</span>response, <span class="at">ordered =</span> <span class="cn">TRUE</span>, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"1"</span>, <span class="st">"2"</span>, <span class="st">"3"</span>, <span class="st">"4"</span>, <span class="st">"5"</span>))</span>
|
||
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot"><-</span> <span class="fu">fct_recode</span>(df<span class="sc">$</span>response, <span class="st">"More than once a week"</span> <span class="ot">=</span> <span class="st">"1"</span>, <span class="st">"Once a week"</span> <span class="ot">=</span> <span class="st">"2"</span>, <span class="st">"At least once a month"</span> <span class="ot">=</span> <span class="st">"3"</span>, <span class="st">"Only on special holy days"</span> <span class="ot">=</span> <span class="st">"4"</span>, <span class="st">"Never"</span> <span class="ot">=</span> <span class="st">"5"</span>)</span>
|
||
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
|
||
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># we need to get the data including facet info in long format, so we use pivot_longer()</span></span>
|
||
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="sc">!</span>response, <span class="at">names_to =</span> <span class="st">"bin_name"</span>, <span class="at">values_to =</span> <span class="st">"b"</span>) <span class="sc">%>%</span> </span>
|
||
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># add counts for plot below</span></span>
|
||
<span id="cb38-12"><a href="#cb38-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(response, bin_name, b) <span class="sc">%>%</span></span>
|
||
<span id="cb38-13"><a href="#cb38-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(bin_name,b) <span class="sc">%>%</span></span>
|
||
<span id="cb38-14"><a href="#cb38-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">perc=</span><span class="fu">paste0</span>(<span class="fu">round</span>(n<span class="sc">*</span><span class="dv">100</span><span class="sc">/</span><span class="fu">sum</span>(n),<span class="dv">1</span>),<span class="st">"%"</span>)) <span class="sc">%>%</span> </span>
|
||
<span id="cb38-15"><a href="#cb38-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># run ggplot</span></span>
|
||
<span id="cb38-16"><a href="#cb38-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> n, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> response)) <span class="sc">+</span></span>
|
||
<span id="cb38-17"><a href="#cb38-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">position=</span><span class="fu">position_fill</span>(), <span class="fu">aes</span>(<span class="at">fill=</span>response)) <span class="sc">+</span></span>
|
||
<span id="cb38-18"><a href="#cb38-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> perc), <span class="at">position =</span> <span class="fu">position_fill</span>(<span class="at">vjust=</span>.<span class="dv">5</span>), <span class="at">size=</span><span class="dv">2</span>) <span class="sc">+</span></span>
|
||
<span id="cb38-19"><a href="#cb38-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Dark2"</span>, <span class="at">type =</span> <span class="st">"qual"</span>) <span class="sc">+</span></span>
|
||
<span id="cb38-20"><a href="#cb38-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">percent_format</span>()) <span class="sc">+</span></span>
|
||
<span id="cb38-21"><a href="#cb38-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="fu">vars</span>(b), <span class="fu">vars</span>(bin_name), <span class="at">labeller=</span><span class="fu">as_labeller</span>(facet_names)) <span class="sc">+</span> </span>
|
||
<span id="cb38-22"><a href="#cb38-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption, <span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>) <span class="sc">+</span> </span>
|
||
<span id="cb38-23"><a href="#cb38-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">title =</span> <span class="cn">NULL</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output-display">
|
||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
|
||
</div>
|
||
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/q58_faceted.png"</span>, <span class="at">width =</span> <span class="dv">30</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units =</span> <span class="st">"cm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</div>
|
||
|
||
<!--
|
||
Use mutate to put "prefer not to say" at the bottom
|
||
# Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels
|
||
|
||
|
||
# Q56 follow-ups
|
||
caption <- "Christian Denomination"
|
||
# TODO: copy plot above for Q56 to add two additional plots using climate_experience_data_named$Q56b and climate_experience_data_named$Q56c
|
||
# Religious Affiliation b - Christian Denomination Subquestion
|
||
christian_denomination <- qualtrics_process_single_multiple_choice(climate_experience_data_named$Q56b)
|
||
christian_denomination_table <- chart_single_result_flextable(climate_experience_data_named$Q56b, desc(Count))
|
||
christian_denomination_table
|
||
save_as_docx(christian_denomination_table, path = "./figures/q56_religious_affiliation_xn_denomination.docx")
|
||
|
||
christian_denomination_hi <- filter(climate_experience_data_named, Q56 == "Christian", Q57_bin == "high")
|
||
christian_denomination_hi <- qualtrics_process_single_multiple_choice(christian_denomination_hi$Q56b)
|
||
christian_denomination_hi
|
||
|
||
# Religious Affiliation c - Muslim Denomination Subquestion
|
||
caption <- "Islamic Identity"
|
||
# Should the label be different than income since the data examined is the Affiliation?
|
||
# TODO: adjust plot to factor using numbered responses on this question (perhaps also above)
|
||
religious_affiliationc <- qualtrics_process_single_multiple_choice(climate_experience_data_named$Q56c)
|
||
religious_affiliationc_plot <- plot_horizontal_bar(religious_affiliationc)
|
||
religious_affiliationc_plot <- religious_affiliationc_plot + labs(caption = caption, x = "", y = "")
|
||
religious_affiliationc_plot
|
||
ggsave("figures/q56c_religious_affiliation.png", width = 20, height = 10, units = "cm")
|
||
religious_affiliationc_table <- chart_single_result_flextable(climate_experience_data_named$Q56c, Count)
|
||
religious_affiliationc_table
|
||
save_as_docx(religious_affiliationc_table, path = "./figures/q56_religious_affiliation_islam.docx")
|
||
|
||
|
||
# Q58
|
||
|
||
caption <- "Respondent Attendance of Religious Services"
|
||
religious_service_attend <- qualtrics_process_single_multiple_choice(climate_experience_data_named$Q58)
|
||
religious_service_attend_plot <- plot_horizontal_bar(religious_service_attend)
|
||
religious_service_attend_plot <- religious_service_attend_plot + labs(title = caption, x = "", y = "")
|
||
religious_service_attend_plot
|
||
ggsave("figures/q58_religious_service_attend.png", width = 20, height = 10, units = "cm")
|
||
religious_service_attend_table <- chart_single_result_flextable(climate_experience_data_named$Q58, Count)
|
||
religious_service_attend_table
|
||
save_as_docx(religious_service_attend_table, path = "./figures/q58_religious_service_attend.docx")
|
||
|
||
# Faceted plot working with 3x3 grid
|
||
df <- select(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q58)
|
||
names(df) <- c("Q52_bin", "Q53_bin", "Q57_bin", "response")
|
||
facet_names <- c(`Q52_bin` = "Spirituality", `Q53_bin` = "Politics L/R", `Q57_bin` = "Religiosity", `low`="low", `medium`="medium", `high`="high")
|
||
facet_labeller <- function(variable,value){return(facet_names[value])}
|
||
df$response <- factor(df$response, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
|
||
df$response <- fct_recode(df$response, "More than once a week" = "1", "Once a week" = "2", "At least once a month" = "3", "Only on special holy days" = "4", "Never" = "5")
|
||
df %>%
|
||
# we need to get the data including facet info in long format, so we use pivot_longer()
|
||
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
|
||
# add counts for plot below
|
||
count(response, bin_name, b) %>%
|
||
group_by(bin_name,b) %>%
|
||
mutate(perc=paste0(round(n*100/sum(n),1),"%")) %>%
|
||
# run ggplot
|
||
ggplot(aes(x = n, y = "", fill = response)) +
|
||
geom_col(position=position_fill(), aes(fill=response)) +
|
||
geom_text(aes(label = perc), position = position_fill(vjust=.5), size=2) +
|
||
scale_fill_brewer(palette = "Dark2", type = "qual") +
|
||
scale_x_continuous(labels = scales::percent_format()) +
|
||
facet_grid(vars(b), vars(bin_name), labeller=as_labeller(facet_names)) +
|
||
labs(caption = caption, x = "", y = "") +
|
||
guides(fill = guide_legend(title = NULL))
|
||
ggsave("figures/q58_faceted.png", width = 30, height = 10, units = "cm")
|
||
|
||
# Q59
|
||
|
||
caption <- "Respondent Prayer Outside of Religious Services"
|
||
prayer <- qualtrics_process_single_multiple_choice(climate_experience_data_named$Q59)
|
||
prayer_plot <- plot_horizontal_bar(prayer)
|
||
prayer_plot <- prayer_plot + labs(caption = caption, x = "", y = "")
|
||
prayer_plot
|
||
ggsave("figures/q59_prayer.png", width = 20, height = 10, units = "cm")
|
||
prayer_table <- chart_single_result_flextable(climate_experience_data_named$Q59, Count)
|
||
prayer_table
|
||
save_as_docx(prayer_table, path = "./figures/q59_prayer.docx")
|
||
|
||
# Faceted plot working with 3x3 grid
|
||
df <- select(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q59)
|
||
names(df) <- c("Q52_bin", "Q53_bin", "Q57_bin", "response")
|
||
facet_names <- c(`Q52_bin` = "Spirituality", `Q53_bin` = "Politics L/R", `Q57_bin` = "Religiosity", `low`="low", `medium`="medium", `high`="high")
|
||
facet_labeller <- function(variable,value){return(facet_names[value])}
|
||
df$response <- factor(df$response, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
|
||
df$response <- fct_recode(df$response, "More than once a week" = "1", "Once a week" = "2", "At least once a month" = "3", "Only on special holy days" = "4", "Never" = "5")
|
||
df %>%
|
||
# we need to get the data including facet info in long format, so we use pivot_longer()
|
||
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
|
||
# add counts for plot below
|
||
count(response, bin_name, b) %>%
|
||
group_by(bin_name,b) %>%
|
||
mutate(perc=paste0(round(n*100/sum(n),1),"%")) %>%
|
||
# run ggplot
|
||
ggplot(aes(x = n, y = "", fill = response)) +
|
||
geom_col(position=position_fill(), aes(fill=response)) +
|
||
geom_text(aes(label = perc), position = position_fill(vjust=.5), size=2) +
|
||
scale_fill_brewer(palette = "Dark2", type = "qual") +
|
||
scale_x_continuous(labels = scales::percent_format()) +
|
||
facet_grid(vars(b), vars(bin_name), labeller=as_labeller(facet_names)) +
|
||
labs(caption = caption, x = "", y = "") +
|
||
guides(fill = guide_legend(title = NULL))
|
||
ggsave("figures/q59_faceted.png", width = 30, height = 10, units = "cm")
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# Comparing with attitudes surrounding climate change
|
||
|
||
|
||
# Q6
|
||
|
||
q6_data <- qualtrics_process_single_multiple_choice_unsorted_streamlined(climate_experience_data$Q6)
|
||
|
||
title <- "Do you think the climate is changing?"
|
||
|
||
level_order <- c("Don<e2><80><99>t know",
|
||
"Definitely not changing",
|
||
"Probably not changing",
|
||
"Probably changing",
|
||
"Definitely changing")
|
||
## code if a specific palette is needed for matching
|
||
fill = wheel(ochre, num = as.integer(count(q6_data[1])))
|
||
# make plot
|
||
q6_data_plot <- ggplot(q6_data, aes(x = n, y = response, fill = fill)) +
|
||
geom_col(colour = "white") +
|
||
## add percentage labels
|
||
geom_text(aes(label = perc),
|
||
## make labels left-aligned and white
|
||
hjust = 1, colour = "black", size=4) + # use nudge_x = 30, to shift position
|
||
## reduce spacing between labels and bars
|
||
scale_fill_identity(guide = "none") +
|
||
## get rid of all elements except y axis labels + adjust plot margin
|
||
theme_ipsum_rc() +
|
||
theme(plot.margin = margin(rep(15, 4))) +
|
||
easy_center_title() +
|
||
# with thanks for helpful info on doing wrap here: https://stackoverflow.com/questions/21878974/wrap-long-axis-labels-via-labeller-label-wrap-in-ggplot2
|
||
scale_y_discrete(labels = wrap_format(30), limits = level_order) +
|
||
theme(plot.title = element_text(size =18, hjust = 0.5), axis.text.y = element_text(size =16)) +
|
||
labs(title = title, x = "", y = "")
|
||
|
||
q6_data_plot
|
||
|
||
ggsave("figures/q6.png", width = 18, height = 12, units = "cm")
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# Subsetting
|
||
|
||
## Q57 subsetting based on Religiosity --------------------------------------------------------------
|
||
climate_experience_data <- climate_experience_data %>%
|
||
mutate(
|
||
Q57_bin = case_when(
|
||
Q57_1 > mean(Q57_1) + sd(Q57_1) ~ "high",
|
||
Q57_1 < mean(Q57_1) - sd(Q57_1) ~ "low",
|
||
TRUE ~ "medium"
|
||
) %>% factor(levels = c("low", "medium", "high"))
|
||
)
|
||
|
||
## Subsetting based on Spirituality --------------------------------------------------------------
|
||
|
||
### Nature relatedness --------------------------------------------------------------
|
||
# Calculate overall mean nature-relatedness score based on six questions:
|
||
climate_experience_data$Q51_score <- rowMeans(select(climate_experience_data, Q51_remote_vacation:Q51_heritage))
|
||
|
||
# Create low/med/high bins based on Mean and +1/-1 Standard Deviation
|
||
climate_experience_data <- climate_experience_data %>%
|
||
mutate(
|
||
Q51_bin = case_when(
|
||
Q51_score > mean(Q51_score) + sd(Q51_score) ~ "high",
|
||
Q51_score < mean(Q51_score) - sd(Q51_score) ~ "low",
|
||
TRUE ~ "medium"
|
||
) %>% factor(levels = c("low", "medium", "high"))
|
||
)
|
||
|
||
|
||
-->
|
||
<div class="callout callout-style-default callout-tip callout-titled">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon"></i>
|
||
</div>
|
||
<div class="callout-title-container flex-fill">
|
||
What is Religion?
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>Content tbd</p>
|
||
</div>
|
||
</div>
|
||
<div class="callout callout-style-default callout-tip callout-titled">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon"></i>
|
||
</div>
|
||
<div class="callout-title-container flex-fill">
|
||
Hybrid Religious Identity
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>Content tbd</p>
|
||
</div>
|
||
</div>
|
||
<div class="callout callout-style-default callout-tip callout-titled">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon"></i>
|
||
</div>
|
||
<div class="callout-title-container flex-fill">
|
||
What is Secularisation?
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>Content tbd</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="references" class="level2 unnumbered">
|
||
<h2 class="unnumbered anchored" data-anchor-id="references">References</h2>
|
||
<div id="refs" role="list" style="display: none">
|
||
|
||
</div>
|
||
|
||
|
||
</section>
|
||
|
||
</main> <!-- /main -->
|
||
<script id="quarto-html-after-body" type="application/javascript">
|
||
window.document.addEventListener("DOMContentLoaded", function (event) {
|
||
const toggleBodyColorMode = (bsSheetEl) => {
|
||
const mode = bsSheetEl.getAttribute("data-mode");
|
||
const bodyEl = window.document.querySelector("body");
|
||
if (mode === "dark") {
|
||
bodyEl.classList.add("quarto-dark");
|
||
bodyEl.classList.remove("quarto-light");
|
||
} else {
|
||
bodyEl.classList.add("quarto-light");
|
||
bodyEl.classList.remove("quarto-dark");
|
||
}
|
||
}
|
||
const toggleBodyColorPrimary = () => {
|
||
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
|
||
if (bsSheetEl) {
|
||
toggleBodyColorMode(bsSheetEl);
|
||
}
|
||
}
|
||
toggleBodyColorPrimary();
|
||
const icon = "";
|
||
const anchorJS = new window.AnchorJS();
|
||
anchorJS.options = {
|
||
placement: 'right',
|
||
icon: icon
|
||
};
|
||
anchorJS.add('.anchored');
|
||
const isCodeAnnotation = (el) => {
|
||
for (const clz of el.classList) {
|
||
if (clz.startsWith('code-annotation-')) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
const clipboard = new window.ClipboardJS('.code-copy-button', {
|
||
text: function(trigger) {
|
||
const codeEl = trigger.previousElementSibling.cloneNode(true);
|
||
for (const childEl of codeEl.children) {
|
||
if (isCodeAnnotation(childEl)) {
|
||
childEl.remove();
|
||
}
|
||
}
|
||
return codeEl.innerText;
|
||
}
|
||
});
|
||
clipboard.on('success', function(e) {
|
||
// button target
|
||
const button = e.trigger;
|
||
// don't keep focus
|
||
button.blur();
|
||
// flash "checked"
|
||
button.classList.add('code-copy-button-checked');
|
||
var currentTitle = button.getAttribute("title");
|
||
button.setAttribute("title", "Copied!");
|
||
let tooltip;
|
||
if (window.bootstrap) {
|
||
button.setAttribute("data-bs-toggle", "tooltip");
|
||
button.setAttribute("data-bs-placement", "left");
|
||
button.setAttribute("data-bs-title", "Copied!");
|
||
tooltip = new bootstrap.Tooltip(button,
|
||
{ trigger: "manual",
|
||
customClass: "code-copy-button-tooltip",
|
||
offset: [0, -8]});
|
||
tooltip.show();
|
||
}
|
||
setTimeout(function() {
|
||
if (tooltip) {
|
||
tooltip.hide();
|
||
button.removeAttribute("data-bs-title");
|
||
button.removeAttribute("data-bs-toggle");
|
||
button.removeAttribute("data-bs-placement");
|
||
}
|
||
button.setAttribute("title", currentTitle);
|
||
button.classList.remove('code-copy-button-checked');
|
||
}, 1000);
|
||
// clear code selection
|
||
e.clearSelection();
|
||
});
|
||
function tippyHover(el, contentFn) {
|
||
const config = {
|
||
allowHTML: true,
|
||
content: contentFn,
|
||
maxWidth: 500,
|
||
delay: 100,
|
||
arrow: false,
|
||
appendTo: function(el) {
|
||
return el.parentElement;
|
||
},
|
||
interactive: true,
|
||
interactiveBorder: 10,
|
||
theme: 'quarto',
|
||
placement: 'bottom-start'
|
||
};
|
||
window.tippy(el, config);
|
||
}
|
||
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
|
||
for (var i=0; i<noterefs.length; i++) {
|
||
const ref = noterefs[i];
|
||
tippyHover(ref, function() {
|
||
// use id or data attribute instead here
|
||
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
|
||
try { href = new URL(href).hash; } catch {}
|
||
const id = href.replace(/^#\/?/, "");
|
||
const note = window.document.getElementById(id);
|
||
return note.innerHTML;
|
||
});
|
||
}
|
||
let selectedAnnoteEl;
|
||
const selectorForAnnotation = ( cell, annotation) => {
|
||
let cellAttr = 'data-code-cell="' + cell + '"';
|
||
let lineAttr = 'data-code-annotation="' + annotation + '"';
|
||
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
|
||
return selector;
|
||
}
|
||
const selectCodeLines = (annoteEl) => {
|
||
const doc = window.document;
|
||
const targetCell = annoteEl.getAttribute("data-target-cell");
|
||
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
|
||
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
|
||
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
|
||
const lineIds = lines.map((line) => {
|
||
return targetCell + "-" + line;
|
||
})
|
||
let top = null;
|
||
let height = null;
|
||
let parent = null;
|
||
if (lineIds.length > 0) {
|
||
//compute the position of the single el (top and bottom and make a div)
|
||
const el = window.document.getElementById(lineIds[0]);
|
||
top = el.offsetTop;
|
||
height = el.offsetHeight;
|
||
parent = el.parentElement.parentElement;
|
||
if (lineIds.length > 1) {
|
||
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
|
||
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
|
||
height = bottom - top;
|
||
}
|
||
if (top !== null && height !== null && parent !== null) {
|
||
// cook up a div (if necessary) and position it
|
||
let div = window.document.getElementById("code-annotation-line-highlight");
|
||
if (div === null) {
|
||
div = window.document.createElement("div");
|
||
div.setAttribute("id", "code-annotation-line-highlight");
|
||
div.style.position = 'absolute';
|
||
parent.appendChild(div);
|
||
}
|
||
div.style.top = top - 2 + "px";
|
||
div.style.height = height + 4 + "px";
|
||
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
|
||
if (gutterDiv === null) {
|
||
gutterDiv = window.document.createElement("div");
|
||
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
|
||
gutterDiv.style.position = 'absolute';
|
||
const codeCell = window.document.getElementById(targetCell);
|
||
const gutter = codeCell.querySelector('.code-annotation-gutter');
|
||
gutter.appendChild(gutterDiv);
|
||
}
|
||
gutterDiv.style.top = top - 2 + "px";
|
||
gutterDiv.style.height = height + 4 + "px";
|
||
}
|
||
selectedAnnoteEl = annoteEl;
|
||
}
|
||
};
|
||
const unselectCodeLines = () => {
|
||
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
|
||
elementsIds.forEach((elId) => {
|
||
const div = window.document.getElementById(elId);
|
||
if (div) {
|
||
div.remove();
|
||
}
|
||
});
|
||
selectedAnnoteEl = undefined;
|
||
};
|
||
// Attach click handler to the DT
|
||
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
|
||
for (const annoteDlNode of annoteDls) {
|
||
annoteDlNode.addEventListener('click', (event) => {
|
||
const clickedEl = event.target;
|
||
if (clickedEl !== selectedAnnoteEl) {
|
||
unselectCodeLines();
|
||
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
|
||
if (activeEl) {
|
||
activeEl.classList.remove('code-annotation-active');
|
||
}
|
||
selectCodeLines(clickedEl);
|
||
clickedEl.classList.add('code-annotation-active');
|
||
} else {
|
||
// Unselect the line
|
||
unselectCodeLines();
|
||
clickedEl.classList.remove('code-annotation-active');
|
||
}
|
||
});
|
||
}
|
||
const findCites = (el) => {
|
||
const parentEl = el.parentElement;
|
||
if (parentEl) {
|
||
const cites = parentEl.dataset.cites;
|
||
if (cites) {
|
||
return {
|
||
el,
|
||
cites: cites.split(' ')
|
||
};
|
||
} else {
|
||
return findCites(el.parentElement)
|
||
}
|
||
} else {
|
||
return undefined;
|
||
}
|
||
};
|
||
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
|
||
for (var i=0; i<bibliorefs.length; i++) {
|
||
const ref = bibliorefs[i];
|
||
const citeInfo = findCites(ref);
|
||
if (citeInfo) {
|
||
tippyHover(citeInfo.el, function() {
|
||
var popup = window.document.createElement('div');
|
||
citeInfo.cites.forEach(function(cite) {
|
||
var citeDiv = window.document.createElement('div');
|
||
citeDiv.classList.add('hanging-indent');
|
||
citeDiv.classList.add('csl-entry');
|
||
var biblioDiv = window.document.getElementById('ref-' + cite);
|
||
if (biblioDiv) {
|
||
citeDiv.innerHTML = biblioDiv.innerHTML;
|
||
}
|
||
popup.appendChild(citeDiv);
|
||
});
|
||
return popup.innerHTML;
|
||
});
|
||
}
|
||
}
|
||
});
|
||
</script>
|
||
<nav class="page-navigation">
|
||
<div class="nav-page nav-page-previous">
|
||
<a href="./chapter_1.html" class="pagination-link">
|
||
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">1</span> <span class="chapter-title">The 2021 UK Census</span></span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-page nav-page-next">
|
||
<a href="./chapter_3.html" class="pagination-link">
|
||
<span class="nav-page-text"><span class="chapter-number">3</span> <span class="chapter-title">Mapping churches: geospatial data science</span></span> <i class="bi bi-arrow-right-short"></i>
|
||
</a>
|
||
</div>
|
||
</nav>
|
||
</div> <!-- /content -->
|
||
|
||
|
||
|
||
</body></html> |