hacking_religion_textbook/docs/chapter_2.html

1458 lines
151 KiB
HTML
Raw Normal View History

2023-10-02 10:52:45 +00:00
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
2024-02-15 12:30:17 +00:00
<meta name="generator" content="quarto-1.4.549">
2023-10-02 10:52:45 +00:00
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Hacking Religion: TRS &amp; Data Science in Action - 2&nbsp; Getting into the nitty-gritty details</title>
2023-10-02 10:52:45 +00:00
<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;
}
2023-10-03 18:18:50 +00:00
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
2024-02-15 12:30:17 +00:00
pre > code.sourceCode > span { line-height: 1.25; }
2023-10-03 18:18:50 +00:00
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; }
}
</style>
2023-10-02 10:52:45 +00:00
<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",
2024-02-15 12:30:17 +00:00
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
2023-10-02 10:52:45 +00:00
"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",
2024-02-15 12:30:17 +00:00
"search-text-placeholder": "",
2023-10-02 10:52:45 +00:00
"search-detached-cancel-button-title": "Cancel",
2024-02-15 12:30:17 +00:00
"search-submit-button-title": "Submit",
"search-label": "Search"
2023-10-02 10:52:45 +00:00
}
}</script>
</head>
2023-10-12 19:41:06 +00:00
<body class="nav-sidebar floating slimcontent">
2023-10-02 10:52:45 +00:00
<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">
2024-02-15 12:30:17 +00:00
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
2023-10-02 10:52:45 +00:00
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
2024-02-15 12:30:17 +00:00
<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>&nbsp; <span class="chapter-title">Getting into the nitty-gritty details</span></a></li></ol></nav>
<a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" 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();">
2023-10-02 10:52:45 +00:00
<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 -->
2024-02-15 12:30:17 +00:00
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto">
2023-10-02 10:52:45 +00:00
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Hacking Religion: TRS &amp; 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">
2023-10-12 16:08:37 +00:00
<span class="menu-text">Introduction: Hacking Religion</span></a>
2023-10-02 10:52:45 +00:00
</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>&nbsp; <span class="chapter-title">Preamble</span></span></a>
2023-10-02 10:52:45 +00:00
</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>&nbsp; <span class="chapter-title">Getting into the nitty-gritty details</span></span></a>
2023-10-02 10:52:45 +00:00
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./chapter_3.html" class="sidebar-item-text sidebar-link">
2023-10-12 16:08:37 +00:00
<span class="menu-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Mapping churches: geospatial data science</span></span></a>
2023-10-02 10:52:45 +00:00
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./chapter_4.html" class="sidebar-item-text sidebar-link">
2023-10-12 16:08:37 +00:00
<span class="menu-text"><span class="chapter-number">4</span>&nbsp; <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>&nbsp; <span class="chapter-title">Whats next?</span></span></a>
2023-10-02 10:52:45 +00:00
</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>&nbsp; <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>
2024-02-15 12:30:17 +00:00
<div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
2023-10-02 10:52:45 +00:00
<!-- 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="#survey-data-spotlight-project" id="toc-survey-data-spotlight-project" class="nav-link active" data-scroll-target="#survey-data-spotlight-project"><span class="header-section-number">3</span> Survey Data: Spotlight Project</a></li>
<li><a href="#loading-in-some-data" id="toc-loading-in-some-data" class="nav-link" data-scroll-target="#loading-in-some-data"><span class="header-section-number">4</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">5</span> How can you ask about religion?</a>
2023-10-12 19:41:06 +00:00
<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">5.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">5.2</span> “How religious would you say you are?”</a></li>
<li><a href="#social-and-personal-participation-in-activity" id="toc-social-and-personal-participation-in-activity" class="nav-link" data-scroll-target="#social-and-personal-participation-in-activity"><span class="header-section-number">5.3</span> Social and personal participation in activity</a></li>
<li><a href="#spirituality" id="toc-spirituality" class="nav-link" data-scroll-target="#spirituality"><span class="header-section-number">5.4</span> Spirituality</a></li>
2023-10-12 19:41:06 +00:00
</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">6</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">7</span> Working With a Continum: Religiosity and Spirituality</a>
<ul class="collapse">
<li><a href="#quick-excursus-making-things-pretty-with-themes" id="toc-quick-excursus-making-things-pretty-with-themes" class="nav-link" data-scroll-target="#quick-excursus-making-things-pretty-with-themes"><span class="header-section-number">7.1</span> Quick excursus: making things pretty with themes</a></li>
</ul></li>
<li><a href="#spirituality-1" id="toc-spirituality-1" class="nav-link" data-scroll-target="#spirituality-1"><span class="header-section-number">8</span> Spirituality</a></li>
<li><a href="#correlation-testing-and-means" id="toc-correlation-testing-and-means" class="nav-link" data-scroll-target="#correlation-testing-and-means"><span class="header-section-number">9</span> Correlation testing and means</a></li>
<li><a href="#using-scale-values-for-subsetting" id="toc-using-scale-values-for-subsetting" class="nav-link" data-scroll-target="#using-scale-values-for-subsetting"><span class="header-section-number">10</span> Using scale values for subsetting</a></li>
2023-10-02 10:52:45 +00:00
</ul>
</nav>
</div>
<!-- main -->
2023-10-12 19:41:06 +00:00
<main class="content page-columns page-full" id="quarto-document-content">
2023-10-02 10:52:45 +00:00
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Getting into the nitty-gritty details</span></h1>
2023-10-02 10:52:45 +00:00
</div>
<div class="quarto-title-meta">
</div>
2024-02-15 12:30:17 +00:00
2023-10-02 10:52:45 +00:00
</header>
2024-02-15 12:30:17 +00:00
<p>In this chapter, well explore the diverse variety of ways you can frame collecting data around religion. Before we dive into that all, however, you might be wondering, why does it all really matter? Cant we just use the census data and assume thats a reasonably accurate approximation? Ill explore the importance of getting the framing right, or better yet, working with data that seeks to unpack religious belonging, identity, and beliefs (or unbelief) in a variety of ways, but an example might serve to explain why this is important.</p>
<p>The 2016 presidential election result in the USA came as a surprise to many data analysts and pollsters. As the dust settled, a number of analysis scrambled to make sense of things and identify some hidden factor that might have tipped the balance away from the expected winner Hilary Clinton. One of the most widely circulated data points was the role of white evangelical Christians in supporting Trump. Exit polls reported that 81% of this constituency voted for Trump and many major media outlets reported this figure prominently, with public commentary from many religious leaders on the meaning this figure had the social direction of evangelical Christianity.</p>
<p>Far too few observers paused to ask what those exit polls were measuring and a closer look at that information reveals some interesting nuances. There is only a single firm that runs exit polling in the USA, Edison Research, who is contracted to do this work by a consortium of major media news outlets (“the National Election Pool”), which represents ABC News, Associated Press, CBS News, CNN, Fox News, and NBC News. Its not a process driven by slow, nuanced, scholarly researchers strapped for funding, its a rapid high-stakes data collection exercise meant to provide data which can feed into the election week news cycle. The poll doesnt ask respondents simply if they are “evangelical” it uses a broader proxy question to do this: “Would you describe yourself as a born-again or evangelical Christian?” This term “born-again” can be a useful proxy, but it can also prove misleading. When asked if they are “born again” people who identify with a number of non-Christian religions, and people who might describe themselves as non-religious will also often answer “yes”. This is particularly salient given the 2016 exit survey asked this question before asking specifically what a persons religion was, so as Pew Research reported, “everyone who takes the exit poll (including religious “nones” and adherents of non-Christian faiths) has the opportunity to identify as a born-again or evangelical Christian.”</p>
<p>While the “born-again” Christian category tends to correlate to high levels of attendance at worship services, in this case some researchers found that white protestant Christian voters for Trump tended to have low levels of participation in activities. We dont have access to the underlying data, and ultimately the exit polling was quite limited in scope (in some instances respondents werent even asked about religion), so well never really have a proper understanding of what happened demographically in that election. But its an interesting puzzle to consider how different ways to record participation in religion might fit together, or even be in tension with one another. For this chapter, were going to take a look at another dataset which gives us exactly this kind of opportunity, to see how different kinds of measurement might reinforce or relate with one another.</p>
<section id="survey-data-spotlight-project" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Survey Data: Spotlight Project</h1>
<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, its 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 theyre quite rare in practice.</p>
<p>For this chapter, Im 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 regions in the UK. And if we bear in mind that non-white respondents are (of course!) a highly heterogenous group, were even more behind in terms of collecting data that can improve our knowledge. Up until recently researchers just havent been paying close enough attention to catch the significant neglect of the empirical field that this represents.</p>
2023-10-02 10:52:45 +00:00
<p>While Ive 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>Weve decided to open up access to our data and Im highlighting it in this book because its 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>
<section id="loading-in-some-data" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Loading in some data</h1>
<p>The first thing to note here is that weve drawn in a different type of data file, this time from an <code>.sav</code> file, which is usually produced by the statistics software package SPSS. This uses a different R Library (I use <code>haven</code> for this, which is now included as part of the tidyverse). The upside is that in some cases where you have survey data which includes a code like “very much agree” which corresponds to a value like “1” this package will preserve both the value and the text in the R dataframe that is created. This can be useful as there will be cases where, for the sake of analysis, you want the numeric values, and in other cases, for the sake of visualisation, you want the tidy names. Its a sort of “have your cake and eat it too” situation!</p>
<p>Heres how you can load in the sample data Ive provided for this chapter:</p>
2023-10-03 18:18:50 +00:00
<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>
2023-10-12 13:16:02 +00:00
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(here) <span class="sc">|&gt;</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">|&gt;</span> <span class="fu">suppressPackageStartupMessages</span>()</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(haven)</span>
<span id="cb1-6"><a href="#cb1-6" 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>
2023-10-03 18:18:50 +00:00
<div class="cell-output cell-output-stderr">
<pre><code>here() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion</code></pre>
</div>
2023-10-12 13:16:02 +00:00
<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">&lt;-</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>
2023-10-03 18:18:50 +00:00
</div>
<p>Now that youve 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>
2023-10-03 18:18:50 +00:00
</section>
<section id="how-can-you-ask-about-religion" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> How can you ask about religion?</h1>
<p>One of the challenges we faced when running this study is how to gather responsible data from surveys regarding religious identity. As we already hinted in the last chapter, this is one of the key problems we explore in this chapter. Well 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, for the project spotlight dataset we used the following kinds of question to ask about religion and spirituality:</p>
<section id="what-is-your-religion" class="level2" data-number="5.1">
<h2 data-number="5.1" class="anchored" data-anchor-id="what-is-your-religion"><span class="header-section-number">5.1</span> “What is your religion?”</h2>
<p>The first, and perhaps most obvious question (Question 56 in the dataset) 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?” and 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.</p>
<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 limitations, but well get to that in a moment.</p>
2023-10-12 19:41:06 +00:00
</section>
<section id="how-religious-would-you-say-you-are" class="level2" data-number="5.2">
<h2 data-number="5.2" class="anchored" data-anchor-id="how-religious-would-you-say-you-are"><span class="header-section-number">5.2</span> “How religious would you say you are?”</h2>
<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 sliding scale from 0 (not religious at all) to 10 (very religious). Seen in this way, we had a tradition-neutral measurement of religious intensity.</p>
2023-10-12 19:41:06 +00:00
</section>
<section id="social-and-personal-participation-in-activity" class="level2" data-number="5.3">
<h2 data-number="5.3" class="anchored" data-anchor-id="social-and-personal-participation-in-activity"><span class="header-section-number">5.3</span> Social and personal participation in activity</h2>
<p>We included another classic indicator asking how often respondents go to worship (Q58): “Apart from weddings, funerals and other special occasions, how often do you attend religious services?”. The individual counterpart to this question about social participation came next in the form of (Q59): “Apart from when you are at religious services, how often do you pray?” As with the previous question, the answers here also came in an descending scale of intensity:</p>
2023-10-03 18:18:50 +00:00
<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>Do note the descending order here, which is different from the ascending scale for most other questions. This becomes relevant later when we explore correlations across questions. As well note later on, 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 (in the case of our survey on environmental issues), than there is between their affiliation (if they are Christian or Pagan). Well do some exploratory work shortly to see how this is the case in our sample.</p>
2023-10-12 19:41:06 +00:00
</section>
<section id="spirituality" class="level2" data-number="5.4">
<h2 data-number="5.4" class="anchored" data-anchor-id="spirituality"><span class="header-section-number">5.4</span> Spirituality</h2>
<p>We also included a series of questions about spirituality in Q52 and used a slightly overlapping nature relatedness scale Q51 which well unpack a bit further below. There are many other types of question you can ask. In fact, in my teaching, one of my favourite exercises is to ask a student group to brainstorm as many ways as possible to ask a person about their religion whilst using a different word for religion in each question. Weve managed to come up with dozens, possibly hundreds over the year, exploring faith, ritual, spirituality, transcendence, connection, belief, unbelief, sacredness and more. The key thing is that these questions are not directly interchangeable, but they will almost inevitably overlap. If you want to make constructive claims about how religion relates to some aspect of daily life, you will need to carefully consider how you can relate to this plurality in framing everyday experience. In the best case scenario, I think, you should find ways to capture a variety of dimensions and then test for correlations and clusters among your data. Well do some exploration further below so you can see a bit of what I mean.</p>
<p>Youll 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. so its equally important to test for non-religion factors that may have a stronger bearing on someones actions or sentiments.</p>
2023-10-03 18:18:50 +00:00
<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">
2023-10-12 19:41:06 +00:00
So <em>Whos</em> Religious?
2023-10-03 18:18:50 +00:00
</div>
</div>
<div class="callout-body-container callout-body">
<p>As Ive already hinted in the previous chapter, measuring religiosity is complicated. I suspect some readers may be wondering something like, “whats 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.&nbsp;how frequently one attends services at a place of worship may be the most reliable proxy?</p>
<p>In the past scholars have worked with a minimalist definition of religion, e.g.&nbsp;measuring only those people who participate in worship services with a high level of frequency, or demonstrate a high level of commitment to a range of pre-determined doctrinal positions or beliefs. This relates to a suspicion which was popular in the 20th century, that with the advent of modernity, religion would naturally decline. This has not proven to be the case, with a range of resurgence and transformation of “old” religions and a similar multiplication of new religious and spiritual movements. Scholars tend to refer to this awareness as relating to a post-secular study of religion, and this kind of study tends to be more maximal in orientation, finding religion, belief, and spirituality in a variety of unexpected forms and places (like football, cooking, capitalism, and popular culture). Scholars here also emphasise the ways that religion can be hidden or “tacit,” and may also be non-exclusive, with individual persons adhering to a range of religious traditions in more creative forms of appropriation. We find Christian animists and spiritual atheists, and doctrinal positions which overlap and migrate. One place that scholars have found this to be widely the case is in contemporary belief in paranormal phenomena, which can transcend particular religious identities, and be quite widespread (over 80%) even in so-called advanced scientific societies.</p>
2023-10-03 18:18:50 +00:00
</div>
</div>
2023-10-12 19:41:06 +00:00
</section>
</section>
<section id="exploring-data-around-religious-affiliation" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Exploring data around religious affiliation:</h1>
<p>Lets dive into the data and do some initial exploration to map out what we have in this survey. Well start with the question 56 data, around religious affiliation. As usual, well begin by loading in some data:</p>
2023-10-03 18:18:50 +00:00
<div class="cell">
2023-10-12 13:16:02 +00:00
<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">&lt;-</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">&lt;-</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">&lt;-</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>
2023-10-03 18:18:50 +00:00
</div>
<p>There are few things we need to do here to get the data into initial proper shape. This is often referred to as “cleaning” the data:</p>
2023-10-03 18:18:50 +00:00
<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 <code>ggplot</code> can handle (a dataframe).</li>
2023-10-03 18:18:50 +00:00
<li>Next well rename the columns so these names are a bit more useful.</li>
<li>We need to omit non-responses so these dont mess with the counting (these are <code>NA</code> in R)</li>
</ol>
<p>As in the previous chapter, Ive provided sample data here that needs a bit of work. This gives you a chance to see what this all looks like in practice, and offers some examples you can apply later to your own datasets.</p>
<p>If we pause at this point to view the data, youll see its basically just a long list of survey responses. What we need is a count of each unique response (these are sometimes called a <code>factor</code>). This will take a few more steps:</p>
2023-10-03 18:18:50 +00:00
<div class="cell">
2023-10-12 13:16:02 +00:00
<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">&lt;-</span> religious_affiliation <span class="sc">%&gt;%</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">%&gt;%</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">&lt;-</span> religious_affiliation_sums <span class="sc">%&gt;%</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>
2023-10-03 18:18:50 +00:00
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
2023-10-12 13:16:02 +00:00
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
2023-10-03 18:18:50 +00:00
<dd>
2024-02-15 12:30:17 +00:00
<span data-code-cell="annotated-cell-4" data-code-lines="2" data-code-annotation="1">First we generate new a dataframe with sums per category and</span>
2023-10-03 18:18:50 +00:00
</dd>
2023-10-12 13:16:02 +00:00
<dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt>
2023-10-03 18:18:50 +00:00
<dd>
2024-02-15 12:30:17 +00:00
<span data-code-cell="annotated-cell-4" data-code-lines="3" data-code-annotation="2">…sort in descending order</span>
2023-10-03 18:18:50 +00:00
</dd>
2023-10-12 13:16:02 +00:00
<dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt>
2023-10-03 18:18:50 +00:00
<dd>
2024-02-15 12:30:17 +00:00
<span data-code-cell="annotated-cell-4" data-code-lines="5" data-code-annotation="3">Then we add new column with percentages based on the sums youve just generated</span>
2023-10-03 18:18:50 +00:00
</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">
2023-10-12 13:16:02 +00:00
<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>
2023-10-03 18:18:50 +00:00
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 3
2023-10-03 18:18:50 +00:00
response n perc
&lt;fct&gt; &lt;int&gt; &lt;chr&gt;
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>
<p>We can view this as a bar chart using <code>ggplot</code> in ways that are similar to the exercises in the last chapter:</p>
2023-10-03 18:18:50 +00:00
<div class="cell">
2023-10-12 13:16:02 +00:00
<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>
2023-10-03 18:18:50 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-03 18:18:50 +00:00
</div>
</div>
<p>You may notice that Ive added one feature to our chart that wasnt in the bar charts in chapter 1, text labels with the actual value on each bar using <code>geom_text</code>.</p>
2023-10-07 18:01:40 +00:00
<p>You may be thinking about the plots weve just finished in chapter 1 and wondering how they compare. Lets use the same facet approach that weve just used to render this data in a subsetted way.</p>
<div class="cell">
2023-10-12 13:16:02 +00:00
<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">&lt;-</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">&lt;-</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">&lt;-</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">&lt;-</span> religious_affiliation_ethnicity <span class="sc">%&gt;%</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">%&gt;%</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">%&gt;%</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>religious_affiliation_ethnicity_plot <span class="ot">&lt;-</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 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 class="at">labeller =</span> <span class="fu">label_wrap_gen</span>(<span class="at">width =</span> <span class="dv">24</span>)) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">strip.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">strip.text.y =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">6</span>))</span>
<span id="annotated-cell-7-12"><a href="#annotated-cell-7-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="annotated-cell-7-13"><a href="#annotated-cell-7-13" aria-hidden="true" tabindex="-1"></a>religious_affiliation_ethnicity_plot</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<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">ggsave</span>(<span class="st">"figures/spotlight_religious_affiliation_ethnicity.png"</span>, <span class="at">plot=</span>religious_affiliation_ethnicity_plot, <span class="at">width =</span> <span class="dv">8</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units=</span><span class="fu">c</span>(<span class="st">"in"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-07 18:01:40 +00:00
</div>
<p>Youll notice that Ive tweaked the display of facet titles a bit here so that the text wraps using <code>labeller = label_wrap_gen(width = 24)</code>, since there are a lot of facets here, which are all interesting, Ive also reduced the size of text for x- and y- axes using <code>theme(strip.text.x = element_text()</code>.</p>
2023-10-12 19:41:06 +00:00
</section>
<section id="working-with-a-continum-religiosity-and-spirituality" class="level1 page-columns page-full" data-number="7">
<h1 data-number="7"><span class="header-section-number">7</span> Working With a Continum: Religiosity and Spirituality</h1>
2023-10-12 19:41:06 +00:00
<p>So far weve just worked with bar plots, but there are a lot of other possible visualisations and types of data which demand them.</p>
<p>As Ive 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>Well 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-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>religiosity <span class="ot">&lt;-</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-9-2"><a href="#annotated-cell-9-2" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(religiosity) <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"response"</span>)</span>
<span id="annotated-cell-9-3"><a href="#annotated-cell-9-3" aria-hidden="true" tabindex="-1"></a>religiosity <span class="ot">&lt;-</span> <span class="fu">filter</span>(religiosity, <span class="sc">!</span><span class="fu">is.na</span>(response))</span>
<span id="annotated-cell-9-4"><a href="#annotated-cell-9-4" aria-hidden="true" tabindex="-1"></a>religiosity_sums <span class="ot">&lt;-</span> religiosity <span class="sc">%&gt;%</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-5" class="code-annotation-target"><a href="#annotated-cell-9-5" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">count</span>(response) <span class="sc">%&gt;%</span></span>
<span id="annotated-cell-9-6"><a href="#annotated-cell-9-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-9-7"><a href="#annotated-cell-9-7" aria-hidden="true" tabindex="-1"></a>religiosity_sums <span class="ot">&lt;-</span> religiosity_sums <span class="sc">%&gt;%</span> </span>
<span id="annotated-cell-9-8"><a href="#annotated-cell-9-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>
2023-10-12 19:41:06 +00:00
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-9" data-target-annotation="1">1</dt>
2023-10-12 19:41:06 +00:00
<dd>
2024-02-15 12:30:17 +00:00
<span data-code-cell="annotated-cell-9" data-code-lines="5" 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 wouldnt really make sense to plot this chart in the order of response.</span>
2023-10-12 19:41:06 +00:00
</dd>
</dl>
</div>
</div>
<p>Now, lets plot that data:</p>
<div class="cell">
<div class="sourceCode cell-code" id="annotated-cell-10"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-10-1"><a href="#annotated-cell-10-1" aria-hidden="true" tabindex="-1"></a>caption <span class="ot">&lt;-</span> <span class="st">"Respondent Religiosity"</span></span>
<span id="annotated-cell-10-2"><a href="#annotated-cell-10-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-10" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-10-3" class="code-annotation-target"><a href="#annotated-cell-10-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-10-4"><a href="#annotated-cell-10-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-10" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-10-5" class="code-annotation-target"><a href="#annotated-cell-10-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</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">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-7"><a href="#annotated-cell-10-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>
2023-10-13 06:04:52 +00:00
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-10" data-target-annotation="1">1</dt>
2023-10-13 06:04:52 +00:00
<dd>
2024-02-15 12:30:17 +00:00
<span data-code-cell="annotated-cell-10" data-code-lines="3" data-code-annotation="1">Weve added colors, because colours are fun.</span>
2023-10-13 06:04:52 +00:00
</dd>
<dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt>
2023-10-13 06:04:52 +00:00
<dd>
2024-02-15 12:30:17 +00:00
<span data-code-cell="annotated-cell-10" data-code-lines="5" data-code-annotation="2">Also new here is <code>coord_flip</code> to rotate the chart so we have bars going horizontally</span>
2023-10-13 06:04:52 +00:00
</dd>
</dl>
</div>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<section id="quick-excursus-making-things-pretty-with-themes" class="level2 page-columns page-full" data-number="7.1">
<h2 data-number="7.1" class="anchored" data-anchor-id="quick-excursus-making-things-pretty-with-themes"><span class="header-section-number">7.1</span> Quick excursus: making things pretty with themes</h2>
2023-10-12 19:41:06 +00:00
<p>Since were thinking about how things look just now, lets 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>
2024-02-15 12:30:17 +00:00
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="margin-aside">If youd 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>
2023-10-12 19:41:06 +00:00
<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-11"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-11-1"><a href="#annotated-cell-11-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-11-2"><a href="#annotated-cell-11-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-11-3"><a href="#annotated-cell-11-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-11-4"><a href="#annotated-cell-11-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="annotated-cell-11-5"><a href="#annotated-cell-11-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-11-6"><a href="#annotated-cell-11-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-11-7"><a href="#annotated-cell-11-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>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</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 its own special (and very cheerful) colour palette.</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>(ggthemes) <span class="sc">|&gt;</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_pander</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>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<p>Or, you might try the well-crafted typgraphy from <code>hbrthemes</code> in the <code>theme_ipsum_pub</code> theme:</p>
<p>Note: this library will expect your system to have certain fonts installed and available for RStudio to use. You may want to run the following command to import your system fonts to R so they are available: <code>extrafont::font_import()</code>. This will take a bit of time to run. The package will also save fonts to a folder on your PC so you can install them if you dont already have them, you can run <code>import_public_sans()</code> to get the path for these files and install in R.</p>
2023-10-12 19:41:06 +00:00
<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="fu">library</span>(hrbrthemes) <span class="sc">|&gt;</span> <span class="fu">suppressPackageStartupMessages</span>()</span>
<span id="cb9-2"><a href="#cb9-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="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_ipsum_pub</span>() <span class="sc">+</span></span>
<span id="cb9-4"><a href="#cb9-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>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
</section>
</section>
2024-02-15 12:30:17 +00:00
<section id="spirituality-1" class="level1 page-columns page-full" data-number="8">
<h1 data-number="8"><span class="header-section-number">8</span> Spirituality</h1>
<p>Were going to come back to this data around religiosity, but lets set it to one side for a moment and build up a visualisation of an adjacent measure we used in this study which focussed on spirituality.</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">
What is the difference between Spirituality and Religion?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Though the terms can tend to be used interchangeable in many cases, some scholars in religious studies and psychology have sought to develop the concept (and measurement of) spirituality as a counterpoint to religion. In some cases, scholars argue that religion is extrinsic (something outside us that we participate in) and spirituality is intrinsic (something inside ourselves that we engage with). Another way of contrasting the two concepts is to suggest that religion is social whereas spirituality is personal. As Hodge puts it, “spirituality refers to an individuals relationship with God (or perceived Transcendence), while religion is defined as a particular set of beliefs, practices, and rituals that have been developed in community by people who share similar exis- tential experiences of transcendent reality.” Of course, as youll have noticed, there are many people who think of themselves as religious, but are opposed to participation in a formal religious tradition, or a social institution like a church, mosque, or denomination. So these differentiations cant be sharply made in a conclusive way. And its likely that many respondents will have their own way to relate to these terms, whether it is affection or aversion.</p>
</div>
</div>
<p>For our study, we made use of a six-item intrinsic spirituality scale that was developed by David R. Hodge which is based on another instrument intended to measure “intrinsic religion” by Allport and Ross (1967). These researchers developed a series of questions which they asked respondents in a survey. The advantage here is that youre 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”. There are many other ways that psychologists have developed to measure intrinsic religion or spirituality, and wed encourage you to try them out (there are some references to get you started in Appendix B).</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Statistics 101: Statistical Mean
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Content TBD.</p>
</div>
</div>
2023-10-12 19:41:06 +00:00
</div>
<div class="cell">
2024-02-15 12:30:17 +00:00
<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"># Calculate overall mean spirituality score based on six questions:</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>climate_experience_data<span class="sc">$</span>spirituality_score <span class="ot">&lt;-</span> <span class="fu">rowMeans</span>(<span class="fu">select</span>(climate_experience_data, Q52a_1<span class="sc">:</span>Q52f_1))</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate overall mean nature relatedness score based on six questions:</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>climate_experience_data<span class="sc">$</span>Q51_score <span class="ot">&lt;-</span> <span class="fu">rowMeans</span>(<span class="fu">select</span>(climate_experience_data, Q51_heritage<span class="sc">:</span>Q51_remote_vacation))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
</div>
<p>Like we did in chapter 1, lets 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. We can start with religiosity:</p>
<div class="cell">
2024-02-15 12:30:17 +00:00
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q57_1)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.581349</code></pre>
</div>
</div>
<p>Now lets compare this with the overall mean score for our whole survey pool around spirituality:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>spirituality_score)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output cell-output-stdout">
<pre><code>[1] 6.047454</code></pre>
2023-10-12 19:41:06 +00:00
</div>
</div>
<p>So theyre pretty close, but theres a bit of a contrast between the responses these two measures, with our cohort measuring a bit higher on spirituality than religiosity.</p>
<p>This is quite a blunt measure, telling us how the whole average of all the responses compares in each case. But what is the relationship between these two measures for each individual? To find out more about this, we need to explore the correlation between points. Well talk about correlation analysis in a little bit, but I think it can be helpful to get ourselves back to thinking about our data as consisting of hundreds of tiny points all of which relate to a specific person who provided a range of responses.</p>
<p>Now lets try out some visualisations, staring with the religiosity data.</p>
2023-10-12 19:41:06 +00:00
<div class="cell">
2024-02-15 12:30:17 +00:00
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x =</span> <span class="dv">1</span>, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="cn">NULL</span>, <span class="at">y =</span> <span class="st">"Q57_1"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<p>This is pretty disappointing, as ggplot doesnt know what to do with the x-axis as our points are 1-dimensional, e.g.&nbsp;they only have one value. But its easy to fix! You can ask R to add random numbers for the x-axis so that we can see more of the dots and they arent overlapping. This is called jitter:</p>
<div class="cell">
<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="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x =</span> <span class="dv">1</span>, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">position =</span> <span class="fu">position_jitter</span>(<span class="at">width =</span> <span class="fl">0.1</span>)) <span class="sc">+</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="cn">NULL</span>, <span class="at">y =</span> <span class="st">"Q57_1"</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_blank</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<p>Youll also notice that weve hidden the x-axis value labels as these are just random numbers and not really something we want to draw attention to. Weve also hidden the label for that axis.</p>
2024-02-15 12:30:17 +00:00
<p>This is visually pretty chaotic, but you can see probably see some places where the dots are thicker and get the sense that there are more in the top than the bottom.</p>
<p>Since this is quite a large plot, Id recommend going one step further and making the dots a bit smaller, and a bit transparent (this is called “alpha” in R). The advantage of this is that well be able to tell visually when dots are overlapping and register that there is a cluster. When theyre all the same black color, this is impossible to tell.</p>
2023-10-12 19:41:06 +00:00
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-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> <span class="dv">1</span>, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
2024-02-15 12:30:17 +00:00
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">position =</span> <span class="fu">position_jitter</span>(<span class="at">width =</span> <span class="dv">1</span>), <span class="at">color=</span><span class="st">"black"</span>, <span class="at">size=</span><span class="fl">1.5</span>, <span class="at">alpha=</span><span class="fl">0.3</span>) <span class="sc">+</span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="cn">NULL</span>, <span class="at">y =</span> <span class="st">"Q57_1"</span>) <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_blank</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<p>Thats a bit better. And we can start to see the weight of points hovering just over a value of 5, which aligns with our observation of the overall mean for this column of data a bit earlier in the exercise. But lets say wed like to be able to see this in an even more explicit way using a modification of the jitterplot with additional visual elements showing us where the mean is located. One example of this is called a boxplot:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x =</span> <span class="dv">1</span>, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">color =</span> <span class="st">"black"</span>, <span class="at">fill =</span> <span class="st">"lightblue"</span>, <span class="at">alpha =</span> <span class="fl">0.7</span>) <span class="sc">+</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="cn">NULL</span>, <span class="at">y =</span> <span class="st">"Q57_1"</span>) <span class="sc">+</span> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">axis.text.y =</span> <span class="fu">element_blank</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Ive flipped this chart on its side using <code>coord_flip()</code> because I just feel like these plot are easier to read from left to right. I also needed to adjust the concealment of labels to the y-axis.</p>
<p>The boxplot show us two things: the mean for the overall data using the black vertical line, and then the <a href="https://en.wikipedia.org/wiki/Interquartile_range">interquartile range</a> (the boxes extend to the minimum and maximum values within 1.5 times of the IQR). This is helpful for us to see because, while the mean of all the values is a bit further to the right, the points we have to the left of the mean are more widely distributed.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Statistics 101: Range and getting into Quartiles, Quintiles, Deciles etc.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Content TBD.</p>
2023-10-12 19:41:06 +00:00
</div>
</div>
</div>
<p>I think it would be nice if we could see all the points on our chart with the boxes as you can really see how this is the case, and thats not hard to do. We can also add a theme to make the points stand out a bit more:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x =</span> <span class="dv">1</span>, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">color =</span> <span class="st">"black"</span>, <span class="at">fill =</span> <span class="st">"lightgreen"</span>, <span class="at">alpha =</span> <span class="fl">0.7</span>) <span class="sc">+</span></span>
<span id="cb19-3"><a href="#cb19-3" 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">alpha =</span> <span class="fl">0.3</span>) <span class="sc">+</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="cn">NULL</span>, <span class="at">y =</span> <span class="st">"Q57_1"</span>) <span class="sc">+</span> <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">axis.text.y =</span> <span class="fu">element_blank</span>()) <span class="sc">+</span> <span class="fu">coord_flip</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Lets set the religiosity data to one side and look at the spirituality scale data. Ive mentioned before that this dataset takes a set of six questions and then averages them out. It might be useful to start out by visualising each of these six separately, sticking with our jittered points-on-boxplot format for the sake of exploration. Lets start by gathering our data:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>spirituality_combined <span class="ot">&lt;-</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>Here we hit an aspect of ggplot that is really important to appreciate. This library doesnt want to work with more than two columns, so if we are introducing a third layer of complexity (e.g.&nbsp;answers from different questions) we need to reformat the data for ggplot. The tools to do this are a core part of the <code>tidyverse()</code> library and the usual terminology here is to refer to “wide” data which needs to be converted to “tidy” (thus “tidyverse” for all these tools that love tidy data) or “long” data. This can be accomplished using a pretty quick operation using <code>gather()</code>. And well follow that with a range of more typical data cleaning operations:</p>
2023-10-12 19:41:06 +00:00
<div class="cell">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>spirituality_combined <span class="ot">&lt;-</span> spirituality_combined <span class="sc">%&gt;%</span> </span>
<span id="cb21-2"><a href="#cb21-2" 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">%&gt;%</span></span>
<span id="cb21-3"><a href="#cb21-3" 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">%&gt;%</span></span>
<span id="cb21-4"><a href="#cb21-4" 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 class="co"># [1]</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output cell-output-stderr">
<pre><code>Warning: attributes are not identical across measure variables; they will be
dropped</code></pre>
2023-10-12 19:41:06 +00:00
</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>spirituality_combined <span class="ot">&lt;-</span> spirituality_combined <span class="sc">%&gt;%</span> </span>
<span id="cb23-2"><a href="#cb23-2" 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">%&gt;%</span></span>
<span id="cb23-3"><a href="#cb23-3" 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">%&gt;%</span></span>
<span id="cb23-4"><a href="#cb23-4" 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 Im 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">%&gt;%</span></span>
<span id="cb23-5"><a href="#cb23-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">"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">%&gt;%</span></span>
<span id="cb23-6"><a href="#cb23-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">"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">%&gt;%</span></span>
<span id="cb23-7"><a href="#cb23-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">"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 class="co"># [2]</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol type="1">
<li>Gather text into long format</li>
<li>Change names of rows to question text</li>
</ol>
<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>spirituality_combined <span class="sc">%&gt;%</span></span>
<span id="cb24-2"><a href="#cb24-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">%&gt;%</span> <span class="co"># Reorder data</span></span>
<span id="cb24-3"><a href="#cb24-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="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
<span id="cb24-5"><a href="#cb24-5" 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="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb24-7"><a href="#cb24-7" 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="cb24-8"><a href="#cb24-8" 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="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span></span>
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Spirituality scales"</span>) <span class="sc">+</span></span>
<span id="cb24-11"><a href="#cb24-11" 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></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="co"># using gridExtra to specify explicit dimensions for printing</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/spirituality_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>Weve done a pretty reasonable exploration of these two questions. Now its time to visualise how they correlate to one another. Well work with the combined spirituality scale score for this example, but you could just as easily work with individual elements. What were wondering, in particular, is whether whether spirituality and religiosity are similar for our respondents. Youll see that in this chart, Ive handled the <code>geom_point</code> styling separately for each point so that we can tell them apart.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x =</span> spirituality_score, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">color =</span> <span class="st">"x"</span>), <span class="at">size =</span> <span class="dv">1</span>, <span class="at">alpha =</span> <span class="fl">0.2</span>, <span class="at">shape =</span> <span class="dv">15</span>) <span class="sc">+</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">color =</span> <span class="st">"y"</span>), <span class="at">size =</span> <span class="dv">1</span>, <span class="at">alpha =</span> <span class="fl">0.2</span>, <span class="at">shape =</span> <span class="dv">17</span>) <span class="sc">+</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <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 class="sc">+</span></span>
<span id="cb26-5"><a href="#cb26-5" 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">"Religiosity"</span>) <span class="sc">+</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"x"</span> <span class="ot">=</span> <span class="st">"red"</span>, <span class="st">"y"</span> <span class="ot">=</span> <span class="st">"blue"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
2023-10-12 19:41:06 +00:00
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'</code></pre>
2023-10-12 19:41:06 +00:00
</div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-24-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
<p>It may be helpful to add a few more visual elements to help someone understand this data. Lets try adding a density plot:</p>
2024-02-15 12:30:17 +00:00
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="margin-aside">If youd like to explore a wider range of correlation plots, you might want to check out Data Analysis and Visualization in R Using smplot2, <a href="https://smin95.github.io/dataviz/basics-of-ggplot2-and-correlation-plot.html">“data import”</a>.</span></div></div>
2023-10-12 19:41:06 +00:00
<div class="cell">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggExtra)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a>p <span class="ot">&lt;-</span> <span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x =</span> spirituality_score, <span class="at">y =</span> Q57_1)) <span class="sc">+</span></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">color =</span> <span class="st">"x"</span>), <span class="at">size =</span> <span class="dv">1</span>, <span class="at">alpha =</span> <span class="fl">0.2</span>) <span class="sc">+</span></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">color =</span> <span class="st">"y"</span>), <span class="at">size =</span> <span class="dv">1</span>, <span class="at">alpha =</span> <span class="fl">0.2</span>, <span class="at">shape =</span> <span class="dv">17</span>) <span class="sc">+</span></span>
<span id="cb28-5"><a href="#cb28-5" 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">"Religiosity"</span>) <span class="sc">+</span></span>
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"x"</span> <span class="ot">=</span> <span class="st">"red"</span>, <span class="st">"y"</span> <span class="ot">=</span> <span class="st">"blue"</span>))</span>
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a>p_with_density <span class="ot">&lt;-</span> <span class="fu">ggMarginal</span>(p, <span class="at">type =</span> <span class="st">"histogram"</span>)</span>
<span id="cb28-8"><a href="#cb28-8" aria-hidden="true" tabindex="-1"></a>p_with_density</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-25-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As an alternative we can view this as a heatmap:</p>
2023-10-12 19:41:06 +00:00
<div class="cell">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x=</span>spirituality_score, <span class="at">y=</span>Q57_1)) <span class="sc">+</span></span>
<span id="cb29-2"><a href="#cb29-2" 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="cb29-3"><a href="#cb29-3" 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="cb29-4"><a href="#cb29-4" 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="cb29-5"><a href="#cb29-5" 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.
Please use `after_stat(level)` instead.</code></pre>
</div>
2023-10-12 19:41:06 +00:00
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
2023-10-12 19:41:06 +00:00
</div>
</div>
</section>
<section id="correlation-testing-and-means" class="level1" data-number="9">
<h1 data-number="9"><span class="header-section-number">9</span> Correlation testing and means</h1>
<p>What you may be starting to see in the charts weve plotted so far is that there is a bit of a similar trend between the religiosity values and spirituality scores for our survey responses. This book isnt going to aim to provide an introduction to statistics, but we will highlight a few possibilities and the way they are handled in R to whet your appetite for further study. Weve already mentioned mean values a bit above, and most readers will likely be familiar with the calculation of basic statistical functions, like mean and range. Below, were going to explore two further concepts, of “correlation” and “standard deviation”.</p>
<p>Lets start by assessing the correlation between these two elements of the data that were featured in the previous section. Suffice it to say that there are different ways to measure correlation, depending on how the two data sources youre working with are related (or not). For our purposes here, were going to calculate the Pearson correlation coefficient. In essence this describes the relationship between the two datasets in the form of a number from -1 to 1. If the value is close to zero, there is simply non relationship between the two sets of data. The closer your value gets to +1, the stronger the indication that there is a positive linear relationship, in other words, if a value in set A is high, the corresponding value in set B is also going to be high. The closer your value gets to -1, the strong the indication that there is a negative linear relationship, so they are definitely related, but like magnets flipped in the wrong direction, so rather than attract each other, they move in opposing ways, so a high value in set A will likely correlate to a low value in set B.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
2023-10-03 18:18:50 +00:00
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Statistics 101: Correlation
2023-10-03 18:18:50 +00:00
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
2023-10-03 18:18:50 +00:00
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
2023-10-03 18:18:50 +00:00
<div class="callout-body-container callout-body">
<p>Content TBD.</p>
<p>Discuss Pearson correlation coefficient</p>
2023-10-03 18:18:50 +00:00
</div>
</div>
</div>
<p>To caluclate the correlation in R, you can use the function <code>cor()</code> like this:</p>
<div class="cell">
<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">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>spirituality_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] 0.6075501</code></pre>
</div>
</div>
<p>In this case, weve got a positive value, which is closer to 1 than 0. This indicates there is a positive correlation between these two values. How high must this number be before we call it a “strong” or “very strong” positive correlation? Well, this really depends on the kind of data youre working with. In some physcial sciences with very precise calculations, we might want to see something over 0.8 or even 0.9 before wed call it a strong correlation. But with surveys, that number is generally taken to be a bit lower. Id be tempted to call this a “strongly positive correlation” in our survey between spirituality scores and religiosity.</p>
<p>We can see the range of possibility by examining correlation between some other elements of our survey. We asked respondents to report on their “happiness” and “life satisfaction” - it would be interesting to see if theres a correlation here:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Religious intensity to happiness - minimal positive</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>Q49)</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] 0.1494995</code></pre>
</div>
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Religious intensity to life satisfaction - minimal positive</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>Q50)</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] 0.1340134</code></pre>
</div>
</div>
<p>As you can see if you run this analysis yourself, the correlation is mildly positive, but not particularly strong. Better to say these responses dont have a correlation.</p>
<p>Just to look at another example, lets take a quick look at the relationship in our survey between religiosity and how interested a given respondent said they were in politics:</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="fu">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>Q54)</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] 0.02748217</code></pre>
</div>
</div>
<p>Same situation - no meaningful correlation.</p>
<p>Returning to the adjacent data on religion in the survey, lets examine whether religiosity corresponds in our sample to participation in worship or more private expressions such as prayer:</p>
<div class="cell">
<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="co"># Religious intensity to participation in services - strong positive (because reverse in scales)</span></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>Q58)</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] -0.610272</code></pre>
</div>
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Religious intensity to participation in activity - even stronger positive (because reverse in scales)</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, 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] -0.7130233</code></pre>
</div>
</div>
<p>Here we have a quite different result, showing a strongly negative (even stronger than the correlation to spirituality) relationship. Its worth reminding readers of a feature of this data that I mentioned a while back. These two scales were represented numerically with a descending scale of intensity, while the religiosity data is an ascending scale. So actually, while the pearson test shows a negative correlation, the opposite is actually the case.</p>
<p>You can test for correlations in similar ways around the spirituality score</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>spirituality_score, climate_experience_data<span class="sc">$</span>Q49)</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] 0.123769</code></pre>
</div>
</div>
<p>As before, no correlation to happiness. What about politics?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>spirituality_score, climate_experience_data<span class="sc">$</span>Q53_1)</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] 0.296213</code></pre>
</div>
</div>
<p>We can see here that the value is on the low side, probably not a significannt correlation.</p>
<p>And looking at our two participation scales (social and personal) we can see that the results are a bit different from religiosity:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Religious intensity to participation in services - strong positive (because reverse in scales)</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>spirituality_score, climate_experience_data<span class="sc">$</span>Q58)</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] -0.4326788</code></pre>
</div>
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Religious intensity to participation in activity - even stronger positive (because reverse in scales)</span></span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(climate_experience_data<span class="sc">$</span>Q57_1, 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] -0.7130233</code></pre>
</div>
</div>
<p>This is just barely scratching the surface in terms of the kinds of analysis you can do in R around correlation, and very bare bones in terms of statistical concepts. You can, for example, run a more annnotated correlation test using <code>cor.test()</code>, <code>t.test()</code> and <code>anova()</code> in R which are better suited to other kinds of analysis and which may give a wider array of information such as the p_value. Im not going to dive into this material now, but Id encourage readers to explore some of the resources listed in the appendix, and continue to dive deeper into the world of correlation testing in R.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample car.test:</span></span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a>result <span class="ot">&lt;-</span> <span class="fu">cor.test</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>Q59)</span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Extract p_value:</span></span>
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a>p_value <span class="ot">&lt;-</span> result<span class="sc">$</span>p.value</span>
<span id="cb51-5"><a href="#cb51-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Format the p-value without scientific notation</span></span>
<span id="cb51-6"><a href="#cb51-6" aria-hidden="true" tabindex="-1"></a><span class="fu">format</span>(p_value, <span class="at">scientific =</span> <span class="cn">FALSE</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] "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002671895"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample t-test</span></span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a>result <span class="ot">&lt;-</span> <span class="fu">t.test</span>(climate_experience_data<span class="sc">$</span>Q57_1, climate_experience_data<span class="sc">$</span>Q58)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="using-scale-values-for-subsetting" class="level1" data-number="10">
<h1 data-number="10"><span class="header-section-number">10</span> Using scale values for subsetting</h1>
<p>Because the responses to these two questions about spirituality and religiosity are on a numeric continuum, we can also use them to subset other variables in this dataset. A simple way of doing this is to separate our respondents into “high,” “medium,” and “low” bins for the two questions.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
2023-10-03 18:18:50 +00:00
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Statistics 101: Subsetting
2023-10-03 18:18:50 +00:00
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
2023-10-03 18:18:50 +00:00
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
2023-10-03 18:18:50 +00:00
<div class="callout-body-container callout-body">
<p>Content TBD.</p>
2023-10-03 18:18:50 +00:00
</div>
</div>
</div>
<p>One way to do this would be to simply sort responses into bins based on their numeric value, like assigning 0-3, 4-6 and 7-10 for low medium and high. But this is a bit problematic in practice and can risk misrepresenting your data. Remember above that when we calculated the mean for each of these two datasets, it wasnt straight in the middle of the 0-10 range (e.g.&nbsp;5), but a bit above that. This means that if we divided the actual responses into proportional bins, the point at which we might divide them should be shifted a bit. What we want to do ultimately is work with the range of values that respondents actually chose.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="false" aria-label="Toggle callout">
2023-10-02 10:52:45 +00:00
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Statistics 101: Standard Deviation
2023-10-02 10:52:45 +00:00
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
2023-10-02 10:52:45 +00:00
</div>
<div id="callout-7" class="callout-7-contents callout-collapse collapse">
2023-10-02 10:52:45 +00:00
<div class="callout-body-container callout-body">
<p>Content TBD.</p>
2023-10-02 10:52:45 +00:00
</div>
</div>
</div>
<p>Luckily, this is easy to do in R using the statistical concept of standard deviation, which R can calculate almost magically for us, in the following way:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs" role="tablist"><li class="nav-item" role="presentation"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" role="tab" aria-controls="tabset-1-1" aria-selected="true">Spirituality bins</a></li><li class="nav-item" role="presentation"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" role="tab" aria-controls="tabset-1-2" aria-selected="false">Religiosity bins</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" role="tabpanel" aria-labelledby="tabset-1-1-tab">
<div class="cell">
<div class="sourceCode cell-code" id="cb54"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot">&lt;-</span> climate_experience_data <span class="sc">%&gt;%</span></span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a> <span class="at">spirituality_bin =</span> <span class="fu">case_when</span>(</span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a> spirituality_score <span class="sc">&gt;</span> <span class="fu">mean</span>(spirituality_score) <span class="sc">+</span> <span class="fu">sd</span>(spirituality_score) <span class="sc">~</span> <span class="st">"high"</span>,</span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> spirituality_score <span class="sc">&lt;</span> <span class="fu">mean</span>(spirituality_score) <span class="sc">-</span> <span class="fu">sd</span>(spirituality_score) <span class="sc">~</span> <span class="st">"low"</span>,</span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%&gt;%</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="cb54-8"><a href="#cb54-8" 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>
<ol type="1">
<li>We start by using <code>mutate</code> to add a new column, <code>spirituality_bin</code> to our existing dataframe</li>
<li>We use a case_when loop to test the data against a series of conditions, and then fill the new column with the text “high” or “low” depending on whether the spirituality_score value fits the evaluation. You can see weve used a bit of math here to evaluate the score three times: The first test evaluates whether a given row in that column is greater than the mean plus one standard deviation above the mean. Assuming our value doesnt match that first evaluation, we next test whether a value is a standard deviation above the mean (or more). Our final test, which results in a value for the “medium” category is easy as its just anything that hasnt already been put into one of the other two bins.</li>
<li>We finish by converting this data to a factor so its not just treated by R as a column with text that happens to repeat a lot.</li>
</ol>
</div>
<div id="tabset-1-2" class="tab-pane" role="tabpanel" aria-labelledby="tabset-1-2-tab">
<div class="cell">
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot">&lt;-</span> climate_experience_data <span class="sc">%&gt;%</span></span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="at">religiosity_bin =</span> <span class="fu">case_when</span>(</span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> Q57_1 <span class="sc">&gt;</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="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a> Q57_1 <span class="sc">&lt;</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="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%&gt;%</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="cb55-8"><a href="#cb55-8" 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>
<ol type="1">
<li>We start by using <code>mutate</code> to add a new column, <code>religiosity_bin</code> to our existing dataframe</li>
<li>We use a case_when loop to evaluate the value and fill in text “high”, “low”, or medium.</li>
<li>We finish by converting this data to a factor so its not just treated by R as a column with text that happens to repeat a lot.</li>
</ol>
</div>
</div>
</div>
<p>As Ive mentioned in the previous chapter, good analysis draws on multiple factors when possible and when were trying to hack religion carefully, it can be useful to assess how a given datapoint relates to non-religious categories as well. For our exercise here, Id 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 we are measuring are more or less significant and give us a point of comparison. This is particularly important for research into climate change as various studies have highlighted religious affiliation as an important factor correlating to climate change attitudes, only for later studies to highlight much larger correlations that had been missed by too myopic a research methodology.</p>
<p>Question 53 in the survey asked respondents to place themselves on a political spectrum from “Left” to “Right” so the low bin will represent Left here, high Right and medium a “centrist”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot">&lt;-</span> climate_experience_data <span class="sc">%&gt;%</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a> <span class="at">Q53_bin =</span> <span class="fu">case_when</span>(</span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a> Q53_1 <span class="sc">&gt;</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="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a> Q53_1 <span class="sc">&lt;</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="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%&gt;%</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="cb56-8"><a href="#cb56-8" 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 lets use those bins to explore some of the responses and see how responses may be different depending on spirituality, religiosity and political orientation. Well start with Question 58 data, which asked respondents about how often the attend weekly worship services. Using ggplot well also draws on the facet technique we used in the last chapter, this time to inflect our data with those bins as separate plots.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">select</span>(climate_experience_data, spirituality_bin, Q53_bin, religiosity_bin, Q58) <span class="co"># [1]</span></span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(df) <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"spirituality_bin"</span>, <span class="st">"Q53_bin"</span>, <span class="st">"religiosity_bin"</span>, <span class="st">"response"</span>) <span class="co"># [2]</span></span>
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a>facet_names <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">`</span><span class="at">spirituality_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">religiosity_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 class="co"># [2]</span></span>
<span id="cb57-5"><a href="#cb57-5" aria-hidden="true" tabindex="-1"></a>facet_labeller <span class="ot">&lt;-</span> <span class="cf">function</span>(variable,value){<span class="fu">return</span>(facet_names[value])} <span class="co"># [2]</span></span>
<span id="cb57-6"><a href="#cb57-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb57-7"><a href="#cb57-7" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot">&lt;-</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 class="co"># [3]</span></span>
<span id="cb57-8"><a href="#cb57-8" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot">&lt;-</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 class="co"># [3]</span></span>
<span id="cb57-9"><a href="#cb57-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb57-10"><a href="#cb57-10" aria-hidden="true" tabindex="-1"></a>caption <span class="ot">&lt;-</span> <span class="st">"Frequency of Attendance at Worship Services"</span></span>
<span id="cb57-11"><a href="#cb57-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb57-12"><a href="#cb57-12" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> </span>
<span id="cb57-13"><a href="#cb57-13" 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="cb57-14"><a href="#cb57-14" 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">%&gt;%</span> </span>
<span id="cb57-15"><a href="#cb57-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># add counts for plot below</span></span>
<span id="cb57-16"><a href="#cb57-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(response, bin_name, b) <span class="sc">%&gt;%</span></span>
<span id="cb57-17"><a href="#cb57-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(bin_name,b) <span class="sc">%&gt;%</span></span>
<span id="cb57-18"><a href="#cb57-18" 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">%&gt;%</span> </span>
<span id="cb57-19"><a href="#cb57-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># run ggplot</span></span>
<span id="cb57-20"><a href="#cb57-20" 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="cb57-21"><a href="#cb57-21" 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="cb57-22"><a href="#cb57-22" 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="cb57-23"><a href="#cb57-23" 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="cb57-24"><a href="#cb57-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">type =</span> <span class="st">"qual"</span>) <span class="sc">+</span></span>
<span id="cb57-25"><a href="#cb57-25" 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="cb57-26"><a href="#cb57-26" 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="cb57-27"><a href="#cb57-27" 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 class="sc">+</span></span>
<span id="cb57-28"><a href="#cb57-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="co"># [4]</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-38-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<ol type="1">
<li>First we need to draw in relevant data for the plot.</li>
<li>Now we need to add some formatting with names for columns and facets.</li>
<li>Next, well recode the response values so that theyre factors and tidy up the representation of those factors for our legend.</li>
<li>Finally, we convert this data from wide into long format and plot using ggplot.</li>
</ol>
<!--
Use mutate to put "prefer not to say" at the bottom
# Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels
-->
<p>Have a look over the columns and you can see how there are some clear differences across each of the different kinds of bins weve used and these shift in intensity. It seems like spirituality and religiosity are similar in profile here but political “right” also seems to correlate with a higher level of attendance at weekly worship.</p>
<p>We can run the same faceted plots on other questions and observe the results:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a>caption <span class="ot">&lt;-</span> <span class="st">"Respondent Prayer Outside of Religious Services"</span></span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">select</span>(climate_experience_data, spirituality_bin, Q53_bin, religiosity_bin, Q59)</span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(df) <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"spirituality_bin"</span>, <span class="st">"Q53_bin"</span>, <span class="st">"religiosity_bin"</span>, <span class="st">"response"</span>)</span>
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a>facet_names <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">`</span><span class="at">spirituality_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">religiosity_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="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a>facet_labeller <span class="ot">&lt;-</span> <span class="cf">function</span>(variable,value){<span class="fu">return</span>(facet_names[value])}</span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot">&lt;-</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="cb58-7"><a href="#cb58-7" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot">&lt;-</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="cb58-8"><a href="#cb58-8" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%&gt;%</span> </span>
<span id="cb58-9"><a href="#cb58-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="cb58-10"><a href="#cb58-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">%&gt;%</span> </span>
<span id="cb58-11"><a href="#cb58-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># add counts for plot below</span></span>
<span id="cb58-12"><a href="#cb58-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(response, bin_name, b) <span class="sc">%&gt;%</span></span>
<span id="cb58-13"><a href="#cb58-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(bin_name,b) <span class="sc">%&gt;%</span></span>
<span id="cb58-14"><a href="#cb58-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">%&gt;%</span> </span>
<span id="cb58-15"><a href="#cb58-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># run ggplot</span></span>
<span id="cb58-16"><a href="#cb58-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="cb58-17"><a href="#cb58-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="cb58-18"><a href="#cb58-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="cb58-19"><a href="#cb58-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">type =</span> <span class="st">"qual"</span>) <span class="sc">+</span></span>
<span id="cb58-20"><a href="#cb58-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="cb58-21"><a href="#cb58-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="cb58-22"><a href="#cb58-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="cb58-23"><a href="#cb58-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 class="sc">+</span></span>
<span id="cb58-24"><a href="#cb58-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
2024-02-15 12:30:17 +00:00
<div>
<figure class="figure">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-39-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
2023-10-02 10:52:45 +00:00
</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();
});
2024-02-15 12:30:17 +00:00
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
2023-10-02 10:52:45 +00:00
const config = {
allowHTML: true,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
2024-02-15 12:30:17 +00:00
placement: 'bottom-start',
2023-10-02 10:52:45 +00:00
};
2024-02-15 12:30:17 +00:00
if (contentFn) {
config.content = contentFn;
}
if (onTriggerFn) {
config.onTrigger = onTriggerFn;
}
if (onUntriggerFn) {
config.onUntrigger = onUntriggerFn;
}
2023-10-02 10:52:45 +00:00
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;
});
2024-02-15 12:30:17 +00:00
}
const xrefs = window.document.querySelectorAll('a.quarto-xref');
const processXRef = (id, note) => {
// Strip column container classes
const stripColumnClz = (el) => {
el.classList.remove("page-full", "page-columns");
if (el.children) {
for (const child of el.children) {
stripColumnClz(child);
}
}
}
stripColumnClz(note)
if (id === null || id.startsWith('sec-')) {
// Special case sections, only their first couple elements
const container = document.createElement("div");
if (note.children && note.children.length > 2) {
container.appendChild(note.children[0].cloneNode(true));
for (let i = 1; i < note.children.length; i++) {
const child = note.children[i];
if (child.tagName === "P" && child.innerText === "") {
continue;
} else {
container.appendChild(child.cloneNode(true));
break;
}
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(container);
}
return container.innerHTML
} else {
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(note);
}
return note.innerHTML;
}
} else {
// Remove any anchor links if they are present
const anchorLink = note.querySelector('a.anchorjs-link');
if (anchorLink) {
anchorLink.remove();
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(note);
}
// TODO in 1.5, we should make sure this works without a callout special case
if (note.classList.contains("callout")) {
return note.outerHTML;
} else {
return note.innerHTML;
}
}
}
for (var i=0; i<xrefs.length; i++) {
const xref = xrefs[i];
tippyHover(xref, undefined, function(instance) {
instance.disable();
let url = xref.getAttribute('href');
let hash = undefined;
if (url.startsWith('#')) {
hash = url;
} else {
try { hash = new URL(url).hash; } catch {}
}
if (hash) {
const id = hash.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
if (note !== null) {
try {
const html = processXRef(id, note.cloneNode(true));
instance.setContent(html);
} finally {
instance.enable();
instance.show();
}
} else {
// See if we can fetch this
fetch(url.split('#')[0])
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(html, "text/html");
const note = htmlDoc.getElementById(id);
if (note !== null) {
const html = processXRef(id, note);
instance.setContent(html);
}
}).finally(() => {
instance.enable();
instance.show();
});
}
} else {
// See if we can fetch a full url (with no hash to target)
// This is a special case and we should probably do some content thinning / targeting
fetch(url)
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(html, "text/html");
const note = htmlDoc.querySelector('main.content');
if (note !== null) {
// This should only happen for chapter cross references
// (since there is no id in the URL)
// remove the first header
if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
note.children[0].remove();
}
const html = processXRef(null, note);
instance.setContent(html);
}
}).finally(() => {
instance.enable();
instance.show();
});
}
}, function(instance) {
});
2023-10-02 10:52:45 +00:00
}
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";
2024-02-15 12:30:17 +00:00
div.style.left = 0;
2023-10-02 10:52:45 +00:00
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;
};
2024-02-15 12:30:17 +00:00
// Handle positioning of the toggle
window.addEventListener(
"resize",
throttle(() => {
elRect = undefined;
if (selectedAnnoteEl) {
selectCodeLines(selectedAnnoteEl);
}
}, 10)
);
function throttle(fn, ms) {
let throttle = false;
let timer;
return (...args) => {
if(!throttle) { // first call gets through
fn.apply(this, args);
throttle = true;
} else { // all the others get throttled
if(timer) clearTimeout(timer); // cancel #2
timer = setTimeout(() => {
fn.apply(this, args);
timer = throttle = false;
}, ms);
}
};
}
2023-10-02 10:52:45 +00:00
// 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>
2024-02-15 12:30:17 +00:00
<script src="https://utteranc.es/client.js" repo="kidwellj/hacking_religion_textbook" issue-term="pathname" theme="github-light" crossorigin="anonymous" async="">
</script>
2023-10-02 10:52:45 +00:00
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
2024-02-15 12:30:17 +00:00
<a href="./chapter_1.html" class="pagination-link aria-label=" &lt;span="">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">1</span>&nbsp; <span class="chapter-title">Preamble</span></span>
2023-10-02 10:52:45 +00:00
</a>
</div>
<div class="nav-page nav-page-next">
2024-02-15 12:30:17 +00:00
<a href="./chapter_3.html" class="pagination-link" aria-label="<span class='chapter-number'>3</span>&nbsp; <span class='chapter-title'>Mapping churches: geospatial data science</span>">
2023-10-12 16:08:37 +00:00
<span class="nav-page-text"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Mapping churches: geospatial data science</span></span> <i class="bi bi-arrow-right-short"></i>
2023-10-02 10:52:45 +00:00
</a>
</div>
</nav>
</div> <!-- /content -->
2024-02-15 12:30:17 +00:00
2023-10-02 10:52:45 +00:00
</body></html>