added to ch 2
|
@ -572,7 +572,7 @@ div.csl-indent {
|
|||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-lines="1" data-code-cell="annotated-cell-10" data-code-annotation="2">We’ll re-order the column by size.</span>
|
||||
<span data-code-cell="annotated-cell-10" data-code-lines="1" data-code-annotation="2">We’ll re-order the column by size.</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
@ -595,19 +595,19 @@ div.csl-indent {
|
|||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<span data-code-lines="1" data-code-cell="annotated-cell-11" data-code-annotation="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="1" data-code-annotation="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
|
||||
<dd>
|
||||
<span data-code-lines="3" data-code-cell="annotated-cell-11" data-code-annotation="2">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that you’ll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)</code></span>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="3" data-code-annotation="2">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that you’ll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)</code></span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="3">3</dt>
|
||||
<dd>
|
||||
<span data-code-lines="4" data-code-cell="annotated-cell-11" data-code-annotation="3">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="4" data-code-annotation="3">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
|
||||
</dd>
|
||||
<dt data-target-cell="annotated-cell-11" data-target-annotation="4">4</dt>
|
||||
<dd>
|
||||
<span data-code-lines="5" data-code-cell="annotated-cell-11" data-code-annotation="4">Now plot it out and have a look!</span>
|
||||
<span data-code-cell="annotated-cell-11" data-code-lines="5" data-code-annotation="4">Now plot it out and have a look!</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
|
|
@ -118,7 +118,7 @@ div.csl-indent {
|
|||
|
||||
</head>
|
||||
|
||||
<body class="nav-sidebar floating">
|
||||
<body class="nav-sidebar floating slimcontent">
|
||||
|
||||
<div id="quarto-search-results"></div>
|
||||
<header id="quarto-header" class="headroom fixed-top">
|
||||
|
@ -211,13 +211,21 @@ div.csl-indent {
|
|||
|
||||
<ul>
|
||||
<li><a href="#loading-in-some-data" id="toc-loading-in-some-data" class="nav-link active" data-scroll-target="#loading-in-some-data"><span class="header-section-number">2.1</span> Loading in some data</a></li>
|
||||
<li><a href="#how-can-you-ask-about-religion" id="toc-how-can-you-ask-about-religion" class="nav-link" data-scroll-target="#how-can-you-ask-about-religion"><span class="header-section-number">2.2</span> How can you ask about religion?</a></li>
|
||||
<li><a href="#how-can-you-ask-about-religion" id="toc-how-can-you-ask-about-religion" class="nav-link" data-scroll-target="#how-can-you-ask-about-religion"><span class="header-section-number">2.2</span> How can you ask about religion?</a>
|
||||
<ul class="collapse">
|
||||
<li><a href="#what-is-your-religion" id="toc-what-is-your-religion" class="nav-link" data-scroll-target="#what-is-your-religion"><span class="header-section-number">2.2.1</span> “What is your religion?”</a></li>
|
||||
<li><a href="#how-religious-would-you-say-you-are" id="toc-how-religious-would-you-say-you-are" class="nav-link" data-scroll-target="#how-religious-would-you-say-you-are"><span class="header-section-number">2.2.2</span> “How religious would you say you are?”</a></li>
|
||||
<li><a href="#participation-in-worship" id="toc-participation-in-worship" class="nav-link" data-scroll-target="#participation-in-worship"><span class="header-section-number">2.2.3</span> Participation in Worship</a></li>
|
||||
<li><a href="#spirituality" id="toc-spirituality" class="nav-link" data-scroll-target="#spirituality"><span class="header-section-number">2.2.4</span> Spirituality</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#exploring-data-around-religious-affiliation" id="toc-exploring-data-around-religious-affiliation" class="nav-link" data-scroll-target="#exploring-data-around-religious-affiliation"><span class="header-section-number">2.3</span> Exploring data around religious affiliation:</a></li>
|
||||
<li><a href="#working-with-a-continum-religiosity-and-spirituality" id="toc-working-with-a-continum-religiosity-and-spirituality" class="nav-link" data-scroll-target="#working-with-a-continum-religiosity-and-spirituality"><span class="header-section-number">2.4</span> Working With a Continum: Religiosity and Spirituality</a></li>
|
||||
<li><a href="#references" id="toc-references" class="nav-link" data-scroll-target="#references">References</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
<!-- main -->
|
||||
<main class="content" id="quarto-document-content">
|
||||
<main class="content page-columns page-full" id="quarto-document-content">
|
||||
|
||||
<header id="title-block-header" class="quarto-title-block default">
|
||||
<div class="quarto-title">
|
||||
|
@ -260,11 +268,19 @@ div.csl-indent {
|
|||
<section id="how-can-you-ask-about-religion" class="level2" data-number="2.2">
|
||||
<h2 data-number="2.2" class="anchored" data-anchor-id="how-can-you-ask-about-religion"><span class="header-section-number">2.2</span> How can you ask about religion?</h2>
|
||||
<p>One of the challenges we faced when running this study is how to gather responsible data from surveys regarding religious identity. We’ll dive into this in depth as we do analysis and look at some of the agreements and conflicts in terms of respondent attribution. Just to set the stage, we used the following kinds of question to ask about religion and spirituality:</p>
|
||||
<section id="what-is-your-religion" class="level3" data-number="2.2.1">
|
||||
<h3 data-number="2.2.1" class="anchored" data-anchor-id="what-is-your-religion"><span class="header-section-number">2.2.1</span> “What is your religion?”</h3>
|
||||
<ol type="1">
|
||||
<li>Question 56 asks respondents simply, “What is your religion?” and then provides a range of possible answers. We included follow-up questions regarding denomination for respondents who indicated they were “Christian” or “Muslim”. For respondents who ticked “Christian” we asked, “What is your denomination?” nad for respondents who ticked “Muslim” we asked “Which of the following would you identify with?” and then left a range of possible options which could be ticked such as “Sunni,” “Shia,” “Sufi” etc.</li>
|
||||
</ol>
|
||||
<p>This is one way of measuring religion, that is, to ask a person if they consider themselves formally affiliated with a particular group. This kind of question has some (serious) limitations, but we’ll get to that in a moment.</p>
|
||||
</section>
|
||||
<section id="how-religious-would-you-say-you-are" class="level3" data-number="2.2.2">
|
||||
<h3 data-number="2.2.2" class="anchored" data-anchor-id="how-religious-would-you-say-you-are"><span class="header-section-number">2.2.2</span> “How religious would you say you are?”</h3>
|
||||
<p>We also asked respondents (Q57): “Regardless of whether you belong to a particular religion, how religious would you say you are?” and then provided a slider from 0 (not religious at all) to 10 (very religious).</p>
|
||||
</section>
|
||||
<section id="participation-in-worship" class="level3" data-number="2.2.3">
|
||||
<h3 data-number="2.2.3" class="anchored" data-anchor-id="participation-in-worship"><span class="header-section-number">2.2.3</span> Participation in Worship</h3>
|
||||
<p>We included some classic indicators about how often respondents go to worship (Q58): Apart from weddings, funerals and other special occasions, how often do you attend religious services? and (Q59): “Q59 Apart from when you are at religious services, how often do you pray?”</p>
|
||||
<ul>
|
||||
<li>More than once a week (1)</li>
|
||||
|
@ -273,7 +289,11 @@ div.csl-indent {
|
|||
<li>Only on special holy days (4)</li>
|
||||
<li>Never (5)</li>
|
||||
</ul>
|
||||
<p>Each of these measures a particular kind of dimension, and it is interesting to note that sometimes there are stronger correlations between how often a person attends worship services (weekly versus once a year) and a particular view, than there is between their affiliation (if they are Christian or Pagan). We’ll do some exploratory work shortly to see how this is the case in our sample. We also included a series of questions about spirituality in Q52 and used a nature relatedness scale Q51.</p>
|
||||
<p>Each of these measures a particular kind of dimension, and it is interesting to note that sometimes there are stronger correlations between how often a person attends worship services (weekly versus once a year) and a particular view, than there is between their affiliation (if they are Christian or Pagan). We’ll do some exploratory work shortly to see how this is the case in our sample.</p>
|
||||
</section>
|
||||
<section id="spirituality" class="level3" data-number="2.2.4">
|
||||
<h3 data-number="2.2.4" class="anchored" data-anchor-id="spirituality"><span class="header-section-number">2.2.4</span> Spirituality</h3>
|
||||
<p>We also included a series of questions about spirituality in Q52 and used a slightly overlapping nature relatedness scale Q51.</p>
|
||||
<p>You’ll find that many surveys will only use one of these forms of question and ignore the rest. I think this is a really bad idea as religious belonging, identity, and spirituality are far too complex to work off a single form of response. We can also test out how these different attributions relate to other demographic features, like interest in politics, economic attainment, etc.</p>
|
||||
<div class="callout callout-style-default callout-tip callout-titled">
|
||||
<div class="callout-header d-flex align-content-center">
|
||||
|
@ -281,7 +301,7 @@ div.csl-indent {
|
|||
<i class="callout-icon"></i>
|
||||
</div>
|
||||
<div class="callout-title-container flex-fill">
|
||||
So <em>who’s</em> religious?
|
||||
So <em>Who’s</em> Religious?
|
||||
</div>
|
||||
</div>
|
||||
<div class="callout-body-container callout-body">
|
||||
|
@ -289,6 +309,10 @@ So <em>who’s</em> religious?
|
|||
<p>Highlight challenges of various approaches pointing to literature.</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="exploring-data-around-religious-affiliation" class="level2" data-number="2.3">
|
||||
<h2 data-number="2.3" class="anchored" data-anchor-id="exploring-data-around-religious-affiliation"><span class="header-section-number">2.3</span> Exploring data around religious affiliation:</h2>
|
||||
<p>Let’s dive into the data and see how this all works out. We’ll start with the question 56 data, around religious affiliation:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-3"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-3-1"><a href="#annotated-cell-3-1" aria-hidden="true" tabindex="-1"></a>religious_affiliation <span class="ot"><-</span> <span class="fu">as_tibble</span>(<span class="fu">as_factor</span>(climate_experience_data<span class="sc">$</span>Q56))</span>
|
||||
|
@ -370,6 +394,321 @@ So <em>who’s</em> religious?
|
|||
<span id="annotated-cell-7-13"><a href="#annotated-cell-7-13" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="annotated-cell-7-14"><a href="#annotated-cell-7-14" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"chart.png"</span>, <span class="at">plot=</span>plot1, <span class="at">width =</span> <span class="dv">8</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units=</span><span class="fu">c</span>(<span class="st">"in"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
</section>
|
||||
<section id="working-with-a-continum-religiosity-and-spirituality" class="level2 page-columns page-full" data-number="2.4">
|
||||
<h2 data-number="2.4" class="anchored" data-anchor-id="working-with-a-continum-religiosity-and-spirituality"><span class="header-section-number">2.4</span> Working With a Continum: Religiosity and Spirituality</h2>
|
||||
<p>So far we’ve just worked with bar plots, but there are a lot of other possible visualisations and types of data which demand them.</p>
|
||||
<p>As I’ve mentioned above, on this survey we also asked respondents to tell us on by rating themselves on a scale of 0-10 with 0 being “not religious at all” and 10 being “very religious” in response to the question, “Regardless of whether you belong to a particular religion, how religious would you say you are?”</p>
|
||||
<p>We’ll recycle some code from our previous import to bring in the Q57 data:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-8"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-8-1"><a href="#annotated-cell-8-1" aria-hidden="true" tabindex="-1"></a>religiosity <span class="ot"><-</span> <span class="fu">as_tibble</span>(<span class="fu">as_factor</span>(climate_experience_data<span class="sc">$</span>Q57_1))</span>
|
||||
<span id="annotated-cell-8-2"><a href="#annotated-cell-8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(religiosity) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"response"</span>)</span>
|
||||
<span id="annotated-cell-8-3"><a href="#annotated-cell-8-3" aria-hidden="true" tabindex="-1"></a>religiosity <span class="ot"><-</span> <span class="fu">filter</span>(religiosity, <span class="sc">!</span><span class="fu">is.na</span>(response))</span>
|
||||
<span id="annotated-cell-8-4"><a href="#annotated-cell-8-4" aria-hidden="true" tabindex="-1"></a>religiosity_sums <span class="ot"><-</span> religiosity <span class="sc">%>%</span> </span>
|
||||
<a class="code-annotation-anchor" data-target-cell="annotated-cell-8" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-8-5" class="code-annotation-target"><a href="#annotated-cell-8-5" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">count</span>(response) <span class="sc">%>%</span></span>
|
||||
<span id="annotated-cell-8-6"><a href="#annotated-cell-8-6" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">response =</span> forcats<span class="sc">::</span><span class="fu">fct_rev</span>(forcats<span class="sc">::</span><span class="fu">fct_inorder</span>(response)))</span>
|
||||
<span id="annotated-cell-8-7"><a href="#annotated-cell-8-7" aria-hidden="true" tabindex="-1"></a>religiosity_sums <span class="ot"><-</span> religiosity_sums <span class="sc">%>%</span> </span>
|
||||
<span id="annotated-cell-8-8"><a href="#annotated-cell-8-8" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">perc =</span> scales<span class="sc">::</span><span class="fu">percent</span>(n <span class="sc">/</span> <span class="fu">sum</span>(n), <span class="at">accuracy =</span> .<span class="dv">1</span>, <span class="at">trim =</span> <span class="cn">FALSE</span>))</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-annotation">
|
||||
<dl class="code-annotation-container-grid">
|
||||
<dt data-target-cell="annotated-cell-8" data-target-annotation="1">1</dt>
|
||||
<dd>
|
||||
<span data-code-annotation="1" data-code-cell="annotated-cell-8" data-code-lines="5">Note: we have removed <code>sort = TRUE</code> in the above statement as it will enforce sorting the data by quantities rather than the factor order. It wouldn’t really make sense to plot this chart in the order of response.</span>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now, let’s plot that data:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-9"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-9-1"><a href="#annotated-cell-9-1" aria-hidden="true" tabindex="-1"></a>caption <span class="ot"><-</span> <span class="st">"Respondent Religiosity"</span></span>
|
||||
<span id="annotated-cell-9-2"><a href="#annotated-cell-9-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-9-3"><a href="#annotated-cell-9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-9-4"><a href="#annotated-cell-9-4" aria-hidden="true" tabindex="-1"></a> <span class="do">## get rid of all elements except y axis labels + adjust plot margin</span></span>
|
||||
<span id="annotated-cell-9-5"><a href="#annotated-cell-9-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-9-6"><a href="#annotated-cell-9-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-9-7"><a href="#annotated-cell-9-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>We’ve added a few elements here: 1. Colors, because colours are fun. 2. <code>coord_flip</code> to rotate the chart so we have bars going horizontally</p>
|
||||
<p>Since we’re thinking about how things look just now, let’s play with themes for a minute. <code>ggplot</code> is a really powerful tool for visualising information, but it also has some quite nice features for making things look pretty.</p>
|
||||
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">If you’d like to take a proper deep dive on all this theme stuff, R-Charts has a great set of examples showing you how a number of different theme packages look in practice, <a href="https://r-charts.com/ggplot2/themes/">“R-Charts on Themes”</a>.</span></div></div>
|
||||
<p>R has a number of built-in themes, but these are mostly driven by functional concerns, such as whether you might want to print your chart or have a less heavy look overall. So for example you might use <code>theme_light()</code> in the following way:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="annotated-cell-10"><pre class="sourceCode r code-annotation-code code-with-copy"><code class="sourceCode r"><span id="annotated-cell-10-1"><a href="#annotated-cell-10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-10-2"><a href="#annotated-cell-10-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-10-3"><a href="#annotated-cell-10-3" aria-hidden="true" tabindex="-1"></a> <span class="do">## get rid of all elements except y axis labels + adjust plot margin</span></span>
|
||||
<span id="annotated-cell-10-4"><a href="#annotated-cell-10-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-10-5"><a href="#annotated-cell-10-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-10-6"><a href="#annotated-cell-10-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption) <span class="sc">+</span></span>
|
||||
<span id="annotated-cell-10-7"><a href="#annotated-cell-10-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_light</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>You can also use additional packages like <code>ggthemes()</code> or <code>hrbrthemes()</code> so for example we might want to try the <code>pander</code> theme which has it’s own special (and very cheerful) colour palette.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggthemes) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span> <span class="fu">labs</span>(<span class="at">caption =</span> caption) <span class="sc">+</span> </span>
|
||||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_pander</span>() <span class="sc">+</span></span>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_pander</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Or, you might try the well-crafted typgraphy from <code>hbrthemes</code> in the <code>theme_ipsum_pub</code> theme:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes) <span class="sc">|></span> <span class="fu">suppressPackageStartupMessages</span>()</span>
|
||||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(religiosity_sums, <span class="fu">aes</span>(<span class="at">x =</span> response, <span class="at">y =</span> n, <span class="at">color=</span>response)) <span class="sc">+</span> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span> <span class="fu">labs</span>(<span class="at">caption =</span> caption) <span class="sc">+</span> </span>
|
||||
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_ipsum_pub</span>() <span class="sc">+</span></span>
|
||||
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_pander</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>We’re going to come back to this chart, but let’s set it to one side for a moment and build up a visualisation of the spirituality scale data. The spirituality scale questions come from research by ___ and ___. These researchers developed a series of questions which they asked respondents in a survey. The advantage here is that you’re getting at the question of spirituality from a lot of different angles, and then you combine the scores from all the questions to get a mean “spirituality score”.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="do">### Spirituality scale --------------------------------------------------------------</span></span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate overall mean spirituality score based on six questions:</span></span>
|
||||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>climate_experience_data<span class="sc">$</span>Q52_score <span class="ot"><-</span> <span class="fu">rowMeans</span>(<span class="fu">select</span>(climate_experience_data, Q52a_1<span class="sc">:</span>Q52f_1))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Like we did in chapter 1, let’s start by exploring the data and get a bit of a sense of the character of the responses overall. One good place to start is to find out the mean response for our two continum questions:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># t_testing and means</span></span>
|
||||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Spirituality scale</span></span>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="co"># stat_summary(climate_experience_data$Q52_score)</span></span>
|
||||
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q52_score)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stdout">
|
||||
<pre><code>[1] 6.047454</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Q57 Regardless of whether you belong to a particular religion, how religious would you say you are?</span></span>
|
||||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 0-10, Not religious at all => Very religious; mean=5.58</span></span>
|
||||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q57_1) <span class="co"># religiosity</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stdout">
|
||||
<pre><code>[1] 5.581349</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Q58 Apart from weddings, funerals and other special occasions, how often do you attend religious services?</span></span>
|
||||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="co"># coded at 1-5, lower value = stronger mean=3.439484</span></span>
|
||||
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q58) <span class="co"># service attendance</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stdout">
|
||||
<pre><code>[1] 3.439484</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Q59 Apart from when you are at religious services, how often do you pray?</span></span>
|
||||
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="co"># coded at 1-5, lower = stronger mean=2.50496</span></span>
|
||||
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q59)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stdout">
|
||||
<pre><code>[1] 2.50496</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<p>Now let’s try out some visualisations:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="do">## Q52 Spirituality data ------------------------</span></span>
|
||||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>q52_data <span class="ot"><-</span> <span class="fu">select</span>(climate_experience_data, Q52a_1<span class="sc">:</span>Q52f_1)</span>
|
||||
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Data is at wide format, we need to make it 'tidy' or 'long'</span></span>
|
||||
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>q52_data <span class="ot"><-</span> q52_data <span class="sc">%>%</span> </span>
|
||||
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(<span class="at">key=</span><span class="st">"text"</span>, <span class="at">value=</span><span class="st">"value"</span>) <span class="sc">%>%</span></span>
|
||||
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># rename columns</span></span>
|
||||
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52_"</span>, <span class="st">""</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">value =</span> <span class="fu">round</span>(<span class="fu">as.numeric</span>(value),<span class="dv">0</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>Warning: attributes are not identical across measure variables; they will be
|
||||
dropped</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Change names of rows to question text</span></span>
|
||||
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>q52_data <span class="ot"><-</span> q52_data <span class="sc">%>%</span> </span>
|
||||
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(<span class="at">key=</span><span class="st">"text"</span>, <span class="at">value=</span><span class="st">"value"</span>) <span class="sc">%>%</span></span>
|
||||
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># rename columns</span></span>
|
||||
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52a_1"</span>, <span class="st">"In terms of questions I have about my life, my spirituality answers..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52b_1"</span>, <span class="st">"Growing spiritually is important..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52c_1"</span>, <span class="st">"When I<e2><80><99>m faced with an important decision, spirituality plays a role..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52d_1"</span>, <span class="st">"Spirituality is part of my life..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52e_1"</span>, <span class="st">"When I think of things that help me grow and mature as a person, spirituality has an effect on my personal growth..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>)) <span class="sc">%>%</span></span>
|
||||
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">"Q52f_1"</span>, <span class="st">"My spiritual beliefs affect aspects of my life..."</span>,text, <span class="at">ignore.case =</span> <span class="cn">TRUE</span>))</span>
|
||||
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot</span></span>
|
||||
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Used for gradient colour schemes, as with violin plots</span></span>
|
||||
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(viridis) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>Loading required package: viridisLite</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>q52_plot <span class="ot"><-</span> q52_data <span class="sc">%>%</span></span>
|
||||
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%>%</span> <span class="co"># Reorder data</span></span>
|
||||
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>text, <span class="at">y=</span>value, <span class="at">fill=</span>text, <span class="at">color=</span>text)) <span class="sc">+</span></span>
|
||||
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
|
||||
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>, <span class="at">alpha=</span><span class="fl">0.8</span>) <span class="sc">+</span></span>
|
||||
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_jitter</span>(<span class="at">color=</span><span class="st">"black"</span>, <span class="at">size=</span><span class="fl">0.2</span>, <span class="at">alpha=</span><span class="fl">0.2</span>) <span class="sc">+</span></span>
|
||||
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
|
||||
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position=</span><span class="st">"none"</span>, <span class="at">axis.text.y =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)) <span class="sc">+</span></span>
|
||||
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span> <span class="co"># This switch X and Y axis and allows to get the horizontal version</span></span>
|
||||
<span id="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">""</span>) <span class="sc">+</span></span>
|
||||
<span id="cb22-11"><a href="#cb22-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Spirituality scales"</span>) <span class="sc">+</span></span>
|
||||
<span id="cb22-12"><a href="#cb22-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> <span class="cf">function</span>(x) <span class="fu">str_wrap</span>(x, <span class="at">width =</span> <span class="dv">45</span>))</span>
|
||||
<span id="cb22-13"><a href="#cb22-13" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb22-14"><a href="#cb22-14" aria-hidden="true" tabindex="-1"></a><span class="co"># using gridExtra to specify explicit dimensions for printing</span></span>
|
||||
<span id="cb22-15"><a href="#cb22-15" aria-hidden="true" tabindex="-1"></a>q52_plot</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/q52_boxplot.png"</span>, <span class="at">width =</span> <span class="dv">20</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units =</span> <span class="st">"cm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>There’s an enhanced version of this plot we can use, called <code>ggstatsplot()</code> to get a different view:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co"># As an alternative trying ggstatsplot:</span></span>
|
||||
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(rstantools)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>This is rstantools version 2.3.1.1</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggstatsplot)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>You can cite this package as:
|
||||
Patil, I. (2021). Visualizations with statistical details: The 'ggstatsplot' approach.
|
||||
Journal of Open Source Software, 6(61), 3167, doi:10.21105/joss.03167</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>q52_plot_alt <span class="ot"><-</span> <span class="fu">ggbetweenstats</span>(</span>
|
||||
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> q52_data,</span>
|
||||
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> text,</span>
|
||||
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> value,</span>
|
||||
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> <span class="at">outlier.tagging =</span> <span class="cn">TRUE</span>,</span>
|
||||
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Intrinsic Spirituality Scale Responses"</span></span>
|
||||
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a>) <span class="sc">+</span></span>
|
||||
<span id="cb28-8"><a href="#cb28-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> <span class="cf">function</span>(x) <span class="fu">str_wrap</span>(x, <span class="at">width =</span> <span class="dv">30</span>)) <span class="sc">+</span></span>
|
||||
<span id="cb28-9"><a href="#cb28-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Customizations</span></span>
|
||||
<span id="cb28-10"><a href="#cb28-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
|
||||
<span id="cb28-11"><a href="#cb28-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># Change fonts in the plot</span></span>
|
||||
<span id="cb28-12"><a href="#cb28-12" aria-hidden="true" tabindex="-1"></a> <span class="at">text =</span> <span class="fu">element_text</span>(<span class="at">family =</span> <span class="st">"Helvetica"</span>, <span class="at">size =</span> <span class="dv">8</span>, <span class="at">color =</span> <span class="st">"black"</span>),</span>
|
||||
<span id="cb28-13"><a href="#cb28-13" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.title =</span> <span class="fu">element_text</span>(</span>
|
||||
<span id="cb28-14"><a href="#cb28-14" aria-hidden="true" tabindex="-1"></a> <span class="at">family =</span> <span class="st">"Abril Fatface"</span>, </span>
|
||||
<span id="cb28-15"><a href="#cb28-15" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="dv">20</span>,</span>
|
||||
<span id="cb28-16"><a href="#cb28-16" aria-hidden="true" tabindex="-1"></a> <span class="at">face =</span> <span class="st">"bold"</span>,</span>
|
||||
<span id="cb28-17"><a href="#cb28-17" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"#2a475e"</span></span>
|
||||
<span id="cb28-18"><a href="#cb28-18" aria-hidden="true" tabindex="-1"></a> ),</span>
|
||||
<span id="cb28-19"><a href="#cb28-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># Statistical annotations below the main title</span></span>
|
||||
<span id="cb28-20"><a href="#cb28-20" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.subtitle =</span> <span class="fu">element_text</span>(</span>
|
||||
<span id="cb28-21"><a href="#cb28-21" aria-hidden="true" tabindex="-1"></a> <span class="at">family =</span> <span class="st">"Helvetica"</span>, </span>
|
||||
<span id="cb28-22"><a href="#cb28-22" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="dv">12</span>, </span>
|
||||
<span id="cb28-23"><a href="#cb28-23" aria-hidden="true" tabindex="-1"></a> <span class="at">face =</span> <span class="st">"bold"</span>,</span>
|
||||
<span id="cb28-24"><a href="#cb28-24" aria-hidden="true" tabindex="-1"></a> <span class="at">color=</span><span class="st">"#1b2838"</span></span>
|
||||
<span id="cb28-25"><a href="#cb28-25" aria-hidden="true" tabindex="-1"></a> ),</span>
|
||||
<span id="cb28-26"><a href="#cb28-26" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.title.position =</span> <span class="st">"plot"</span>, <span class="co"># slightly different from default</span></span>
|
||||
<span id="cb28-27"><a href="#cb28-27" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.text =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">10</span>, <span class="at">color =</span> <span class="st">"black"</span>),</span>
|
||||
<span id="cb28-28"><a href="#cb28-28" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">7</span>),</span>
|
||||
<span id="cb28-29"><a href="#cb28-29" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.title =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">12</span>),</span>
|
||||
<span id="cb28-30"><a href="#cb28-30" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.line =</span> <span class="fu">element_line</span>(<span class="at">colour =</span> <span class="st">"grey50"</span>),</span>
|
||||
<span id="cb28-31"><a href="#cb28-31" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid.minor =</span> <span class="fu">element_blank</span>(),</span>
|
||||
<span id="cb28-32"><a href="#cb28-32" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid.major.x =</span> <span class="fu">element_blank</span>(),</span>
|
||||
<span id="cb28-33"><a href="#cb28-33" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid =</span> <span class="fu">element_line</span>(<span class="at">color =</span> <span class="st">"#b4aea9"</span>),</span>
|
||||
<span id="cb28-34"><a href="#cb28-34" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.grid.major.y =</span> <span class="fu">element_line</span>(<span class="at">linetype =</span> <span class="st">"dashed"</span>),</span>
|
||||
<span id="cb28-35"><a href="#cb28-35" aria-hidden="true" tabindex="-1"></a> <span class="at">panel.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">"#fbf9f4"</span>, <span class="at">color =</span> <span class="st">"#fbf9f4"</span>),</span>
|
||||
<span id="cb28-36"><a href="#cb28-36" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">"#fbf9f4"</span>, <span class="at">color =</span> <span class="st">"#fbf9f4"</span>)</span>
|
||||
<span id="cb28-37"><a href="#cb28-37" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>Scale for x is already present.
|
||||
Adding another scale for x, which will replace the existing scale.</code></pre>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>q52_plot_alt</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/q52_plot_alt.png"</span>, <span class="at">width =</span> <span class="dv">20</span>, <span class="at">height =</span> <span class="dv">12</span>, <span class="at">units =</span> <span class="st">"cm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>One thing that might be interesting to test here is whether spirituality and religiosity are similar for our respondents.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x=</span>Q52_score, <span class="at">y=</span>Q57_1)) <span class="sc">+</span> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Spirituality Scale Score"</span>, <span class="at">y =</span> <span class="st">"How Religious?"</span>) <span class="sc">+</span></span>
|
||||
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size=</span><span class="dv">1</span>, <span class="at">alpha=</span><span class="fl">0.3</span>) <span class="sc">+</span> <span class="fu">geom_smooth</span>(<span class="at">method=</span><span class="st">"auto"</span>, <span class="at">se=</span><span class="cn">TRUE</span>, <span class="at">fullrange=</span><span class="cn">FALSE</span>, <span class="at">level=</span><span class="fl">0.95</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'</code></pre>
|
||||
</div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co"># using http://sthda.com/english/wiki/ggplot2-scatter-plots-quick-start-guide-r-software-and-data-visualization</span></span>
|
||||
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(climate_experience_data, <span class="fu">aes</span>(<span class="at">x=</span>Q52_score, <span class="at">y=</span>Q57_1)) <span class="sc">+</span></span>
|
||||
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Spirituality Scale Score"</span>, <span class="at">y =</span> <span class="st">"How Religious?"</span>) <span class="sc">+</span></span>
|
||||
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size=</span><span class="dv">1</span>, <span class="at">alpha=</span><span class="fl">0.3</span>) <span class="sc">+</span> <span class="fu">stat_density_2d</span>(<span class="fu">aes</span>(<span class="at">fill =</span> ..level..), <span class="at">geom=</span><span class="st">"polygon"</span>, <span class="at">alpha=</span><span class="fl">0.3</span>)<span class="sc">+</span></span>
|
||||
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradient</span>(<span class="at">low=</span><span class="st">"blue"</span>, <span class="at">high=</span><span class="st">"red"</span>) <span class="sc">+</span></span>
|
||||
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output cell-output-stderr">
|
||||
<pre><code>Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
|
||||
i Please use `after_stat(level)` instead.</code></pre>
|
||||
</div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-16-2.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
</div>
|
||||
<p>Because the responses to these two questions, about spirituality and religiosity are on a continuum, we can also use them, like we did in previous charts, to subset other datasets. A simple way of doing this is to separate our respondents into “high,” “medium,” and “low” bins for the two questions. Rather than working with hard values, like assigning 0-3, 4-6 and 7-10 for low medium and high, we’ll work with the range of values that respondents actually chose. This is particularly appropriate as the median answer to these questions was not “5”. So we’ll use the statistical concept of standard deviation, which R can calculate almost magically for us, in the following way:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Create low/med/high bins based on Mean and +1/-1 Standard Deviation</span></span>
|
||||
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> climate_experience_data <span class="sc">%>%</span></span>
|
||||
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||||
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a> <span class="at">Q52_bin =</span> <span class="fu">case_when</span>(</span>
|
||||
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a> Q52_score <span class="sc">></span> <span class="fu">mean</span>(Q52_score) <span class="sc">+</span> <span class="fu">sd</span>(Q52_score) <span class="sc">~</span> <span class="st">"high"</span>,</span>
|
||||
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a> Q52_score <span class="sc"><</span> <span class="fu">mean</span>(Q52_score) <span class="sc">-</span> <span class="fu">sd</span>(Q52_score) <span class="sc">~</span> <span class="st">"low"</span>,</span>
|
||||
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
|
||||
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">factor</span>(<span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"low"</span>, <span class="st">"medium"</span>, <span class="st">"high"</span>))</span>
|
||||
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a> )</span>
|
||||
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb36-12"><a href="#cb36-12" aria-hidden="true" tabindex="-1"></a><span class="do">## Q57 subsetting based on Religiosity --------------------------------------------------------------</span></span>
|
||||
<span id="cb36-13"><a href="#cb36-13" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> climate_experience_data <span class="sc">%>%</span></span>
|
||||
<span id="cb36-14"><a href="#cb36-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||||
<span id="cb36-15"><a href="#cb36-15" aria-hidden="true" tabindex="-1"></a> <span class="at">Q57_bin =</span> <span class="fu">case_when</span>(</span>
|
||||
<span id="cb36-16"><a href="#cb36-16" aria-hidden="true" tabindex="-1"></a> Q57_1 <span class="sc">></span> <span class="fu">mean</span>(Q57_1) <span class="sc">+</span> <span class="fu">sd</span>(Q57_1) <span class="sc">~</span> <span class="st">"high"</span>,</span>
|
||||
<span id="cb36-17"><a href="#cb36-17" aria-hidden="true" tabindex="-1"></a> Q57_1 <span class="sc"><</span> <span class="fu">mean</span>(Q57_1) <span class="sc">-</span> <span class="fu">sd</span>(Q57_1) <span class="sc">~</span> <span class="st">"low"</span>,</span>
|
||||
<span id="cb36-18"><a href="#cb36-18" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
|
||||
<span id="cb36-19"><a href="#cb36-19" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">factor</span>(<span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"low"</span>, <span class="st">"medium"</span>, <span class="st">"high"</span>))</span>
|
||||
<span id="cb36-20"><a href="#cb36-20" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>As in the previous chapter, it’s useful to explore multiple factors when possible. So I’d like us to take the data about political affiliation to visualise alongside our religion and spirituality data. this will help us to see where effects are more or less significant and give us a point of comparison.</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="do">## Q53 subsetting based on Political LR orientation --------------------------------------------------------------</span></span>
|
||||
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate low/med/high bins based on Mean and SD</span></span>
|
||||
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a>climate_experience_data <span class="ot"><-</span> climate_experience_data <span class="sc">%>%</span></span>
|
||||
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
|
||||
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a> <span class="at">Q53_bin =</span> <span class="fu">case_when</span>(</span>
|
||||
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a> Q53_1 <span class="sc">></span> <span class="fu">mean</span>(Q53_1) <span class="sc">+</span> <span class="fu">sd</span>(Q53_1) <span class="sc">~</span> <span class="st">"high"</span>,</span>
|
||||
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a> Q53_1 <span class="sc"><</span> <span class="fu">mean</span>(Q53_1) <span class="sc">-</span> <span class="fu">sd</span>(Q53_1) <span class="sc">~</span> <span class="st">"low"</span>,</span>
|
||||
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a> <span class="cn">TRUE</span> <span class="sc">~</span> <span class="st">"medium"</span></span>
|
||||
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">factor</span>(<span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"low"</span>, <span class="st">"medium"</span>, <span class="st">"high"</span>))</span>
|
||||
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
<p>Now let’s use those bins to explore some of the responses about attitudes towards climate change:</p>
|
||||
<div class="cell">
|
||||
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Faceted plot working with 3x3 grid</span></span>
|
||||
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">select</span>(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q58)</span>
|
||||
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(df) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Q52_bin"</span>, <span class="st">"Q53_bin"</span>, <span class="st">"Q57_bin"</span>, <span class="st">"response"</span>)</span>
|
||||
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a>facet_names <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">`</span><span class="at">Q52_bin</span><span class="st">`</span> <span class="ot">=</span> <span class="st">"Spirituality"</span>, <span class="st">`</span><span class="at">Q53_bin</span><span class="st">`</span> <span class="ot">=</span> <span class="st">"Politics L/R"</span>, <span class="st">`</span><span class="at">Q57_bin</span><span class="st">`</span> <span class="ot">=</span> <span class="st">"Religiosity"</span>, <span class="st">`</span><span class="at">low</span><span class="st">`</span><span class="ot">=</span><span class="st">"low"</span>, <span class="st">`</span><span class="at">medium</span><span class="st">`</span><span class="ot">=</span><span class="st">"medium"</span>, <span class="st">`</span><span class="at">high</span><span class="st">`</span><span class="ot">=</span><span class="st">"high"</span>)</span>
|
||||
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a>facet_labeller <span class="ot"><-</span> <span class="cf">function</span>(variable,value){<span class="fu">return</span>(facet_names[value])}</span>
|
||||
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot"><-</span> <span class="fu">factor</span>(df<span class="sc">$</span>response, <span class="at">ordered =</span> <span class="cn">TRUE</span>, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"1"</span>, <span class="st">"2"</span>, <span class="st">"3"</span>, <span class="st">"4"</span>, <span class="st">"5"</span>))</span>
|
||||
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>response <span class="ot"><-</span> <span class="fu">fct_recode</span>(df<span class="sc">$</span>response, <span class="st">"More than once a week"</span> <span class="ot">=</span> <span class="st">"1"</span>, <span class="st">"Once a week"</span> <span class="ot">=</span> <span class="st">"2"</span>, <span class="st">"At least once a month"</span> <span class="ot">=</span> <span class="st">"3"</span>, <span class="st">"Only on special holy days"</span> <span class="ot">=</span> <span class="st">"4"</span>, <span class="st">"Never"</span> <span class="ot">=</span> <span class="st">"5"</span>)</span>
|
||||
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a>df <span class="sc">%>%</span> </span>
|
||||
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># we need to get the data including facet info in long format, so we use pivot_longer()</span></span>
|
||||
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="sc">!</span>response, <span class="at">names_to =</span> <span class="st">"bin_name"</span>, <span class="at">values_to =</span> <span class="st">"b"</span>) <span class="sc">%>%</span> </span>
|
||||
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># add counts for plot below</span></span>
|
||||
<span id="cb38-12"><a href="#cb38-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(response, bin_name, b) <span class="sc">%>%</span></span>
|
||||
<span id="cb38-13"><a href="#cb38-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(bin_name,b) <span class="sc">%>%</span></span>
|
||||
<span id="cb38-14"><a href="#cb38-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">perc=</span><span class="fu">paste0</span>(<span class="fu">round</span>(n<span class="sc">*</span><span class="dv">100</span><span class="sc">/</span><span class="fu">sum</span>(n),<span class="dv">1</span>),<span class="st">"%"</span>)) <span class="sc">%>%</span> </span>
|
||||
<span id="cb38-15"><a href="#cb38-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># run ggplot</span></span>
|
||||
<span id="cb38-16"><a href="#cb38-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> n, <span class="at">y =</span> <span class="st">""</span>, <span class="at">fill =</span> response)) <span class="sc">+</span></span>
|
||||
<span id="cb38-17"><a href="#cb38-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">position=</span><span class="fu">position_fill</span>(), <span class="fu">aes</span>(<span class="at">fill=</span>response)) <span class="sc">+</span></span>
|
||||
<span id="cb38-18"><a href="#cb38-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> perc), <span class="at">position =</span> <span class="fu">position_fill</span>(<span class="at">vjust=</span>.<span class="dv">5</span>), <span class="at">size=</span><span class="dv">2</span>) <span class="sc">+</span></span>
|
||||
<span id="cb38-19"><a href="#cb38-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_brewer</span>(<span class="at">palette =</span> <span class="st">"Dark2"</span>, <span class="at">type =</span> <span class="st">"qual"</span>) <span class="sc">+</span></span>
|
||||
<span id="cb38-20"><a href="#cb38-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">labels =</span> scales<span class="sc">::</span><span class="fu">percent_format</span>()) <span class="sc">+</span></span>
|
||||
<span id="cb38-21"><a href="#cb38-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="fu">vars</span>(b), <span class="fu">vars</span>(bin_name), <span class="at">labeller=</span><span class="fu">as_labeller</span>(facet_names)) <span class="sc">+</span> </span>
|
||||
<span id="cb38-22"><a href="#cb38-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption, <span class="at">x =</span> <span class="st">""</span>, <span class="at">y =</span> <span class="st">""</span>) <span class="sc">+</span> </span>
|
||||
<span id="cb38-23"><a href="#cb38-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">title =</span> <span class="cn">NULL</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="cell-output-display">
|
||||
<p><img src="chapter_2_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
|
||||
</div>
|
||||
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">"figures/q58_faceted.png"</span>, <span class="at">width =</span> <span class="dv">30</span>, <span class="at">height =</span> <span class="dv">10</span>, <span class="at">units =</span> <span class="st">"cm"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
</div>
|
||||
|
||||
<!--
|
||||
Use mutate to put "prefer not to say" at the bottom
|
||||
|
@ -402,17 +741,6 @@ religious_affiliationc_table <- chart_single_result_flextable(climate_experience
|
|||
religious_affiliationc_table
|
||||
save_as_docx(religious_affiliationc_table, path = "./figures/q56_religious_affiliation_islam.docx")
|
||||
|
||||
# Q57
|
||||
# Religiosity
|
||||
caption <- "Respondent Religiosity"
|
||||
religiosity <- qualtrics_process_single_multiple_choice(as.character(climate_experience_data_named$Q57_1))
|
||||
religiosity_plot <- plot_horizontal_bar(religiosity)
|
||||
religiosity_plot <- religiosity_plot + labs(caption = caption, x = "", y = "")
|
||||
religiosity_plot
|
||||
ggsave("figures/q57_religiosity_plot.png", width = 20, height = 10, units = "cm")
|
||||
religiosity_table <- chart_single_result_flextable(climate_experience_data_named$Q57_1, desc(Variable))
|
||||
religiosity_table
|
||||
save_as_docx(religious_affiliationc_table, path = "./figures/q57_religiousity.docx")
|
||||
|
||||
# Q58
|
||||
|
||||
|
@ -569,19 +897,6 @@ climate_experience_data <- climate_experience_data %>%
|
|||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
### Spirituality scale --------------------------------------------------------------
|
||||
# Calculate overall mean spirituality score based on six questions:
|
||||
climate_experience_data$Q52_score <- rowMeans(select(climate_experience_data, Q52a_1:Q52f_1))
|
||||
|
||||
# Create low/med/high bins based on Mean and +1/-1 Standard Deviation
|
||||
climate_experience_data <- climate_experience_data %>%
|
||||
mutate(
|
||||
Q52_bin = case_when(
|
||||
Q52_score > mean(Q52_score) + sd(Q52_score) ~ "high",
|
||||
Q52_score < mean(Q52_score) - sd(Q52_score) ~ "low",
|
||||
TRUE ~ "medium"
|
||||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
-->
|
||||
<div class="callout callout-style-default callout-tip callout-titled">
|
||||
|
|
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-10-1.png
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-11-1.png
Normal file
After Width: | Height: | Size: 32 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-13-1.png
Normal file
After Width: | Height: | Size: 402 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-14-1.png
Normal file
After Width: | Height: | Size: 401 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-15-1.png
Normal file
After Width: | Height: | Size: 204 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-16-1.png
Normal file
After Width: | Height: | Size: 134 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-16-2.png
Normal file
After Width: | Height: | Size: 166 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-19-1.png
Normal file
After Width: | Height: | Size: 65 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-8-1.png
Normal file
After Width: | Height: | Size: 32 KiB |
BIN
docs/chapter_2_files/figure-html/unnamed-chunk-9-1.png
Normal file
After Width: | Height: | Size: 32 KiB |
|
@ -27,12 +27,20 @@ The first thing to note here is that we've drawn in a different type of data fil
|
|||
|
||||
One of the challenges we faced when running this study is how to gather responsible data from surveys regarding religious identity. We'll dive into this in depth as we do analysis and look at some of the agreements and conflicts in terms of respondent attribution. Just to set the stage, we used the following kinds of question to ask about religion and spirituality:
|
||||
|
||||
|
||||
### "What is your religion?"
|
||||
|
||||
1. Question 56 asks respondents simply, "What is your religion?" and then provides a range of possible answers. We included follow-up questions regarding denomination for respondents who indicated they were "Christian" or "Muslim". For respondents who ticked "Christian" we asked, "What is your denomination?" nad for respondents who ticked "Muslim" we asked "Which of the following would you identify with?" and then left a range of possible options which could be ticked such as "Sunni," "Shia," "Sufi" etc.
|
||||
|
||||
This is one way of measuring religion, that is, to ask a person if they consider themselves formally affiliated with a particular group. This kind of question has some (serious) limitations, but we'll get to that in a moment.
|
||||
|
||||
### "How religious would you say you are?"
|
||||
|
||||
We also asked respondents (Q57): "Regardless of whether you belong to a particular religion, how religious would you say you are?" and then provided a slider from 0 (not religious at all) to 10 (very religious).
|
||||
|
||||
|
||||
### Participation in Worship
|
||||
|
||||
We included some classic indicators about how often respondents go to worship (Q58): Apart from weddings, funerals and other special occasions, how often do you attend religious services? and (Q59): "Q59 Apart from when you are at religious services, how often do you pray?"
|
||||
|
||||
- More than once a week (1)
|
||||
|
@ -41,12 +49,16 @@ We included some classic indicators about how often respondents go to worship (Q
|
|||
- Only on special holy days (4)
|
||||
- Never (5)
|
||||
|
||||
Each of these measures a particular kind of dimension, and it is interesting to note that sometimes there are stronger correlations between how often a person attends worship services (weekly versus once a year) and a particular view, than there is between their affiliation (if they are Christian or Pagan). We'll do some exploratory work shortly to see how this is the case in our sample. We also included a series of questions about spirituality in Q52 and used a nature relatedness scale Q51.
|
||||
Each of these measures a particular kind of dimension, and it is interesting to note that sometimes there are stronger correlations between how often a person attends worship services (weekly versus once a year) and a particular view, than there is between their affiliation (if they are Christian or Pagan). We'll do some exploratory work shortly to see how this is the case in our sample.
|
||||
|
||||
### Spirituality
|
||||
|
||||
We also included a series of questions about spirituality in Q52 and used a slightly overlapping nature relatedness scale Q51.
|
||||
|
||||
You'll find that many surveys will only use one of these forms of question and ignore the rest. I think this is a really bad idea as religious belonging, identity, and spirituality are far too complex to work off a single form of response. We can also test out how these different attributions relate to other demographic features, like interest in politics, economic attainment, etc.
|
||||
|
||||
::: {.callout-tip}
|
||||
## So *who's* religious?
|
||||
### So *Who's* Religious?
|
||||
|
||||
As I've already hinted in the previous chapter, measuring religiosity is complicated. I suspect some readers may be wondering something like, "what's the right question to ask?" here. Do we get the most accurate representation by asking people to self-report their religious affiliation? Or is it more accurate to ask individuals to report on how religious they are? Is it, perhaps, better to assume that the indirect query about practice, e.g. how frequently one attends services at a place of worship may be the most reliable proxy?
|
||||
|
||||
|
@ -54,6 +66,8 @@ Highlight challenges of various approaches pointing to literature.
|
|||
|
||||
:::
|
||||
|
||||
## Exploring data around religious affiliation:
|
||||
|
||||
Let's dive into the data and see how this all works out. We'll start with the question 56 data, around religious affiliation:
|
||||
|
||||
```{r}
|
||||
|
@ -120,6 +134,294 @@ ggsave("chart.png", plot=plot1, width = 8, height = 10, units=c("in"))
|
|||
|
||||
```
|
||||
|
||||
## Working With a Continum: Religiosity and Spirituality
|
||||
|
||||
So far we've just worked with bar plots, but there are a lot of other possible visualisations and types of data which demand them.
|
||||
|
||||
As I've mentioned above, on this survey we also asked respondents to tell us on by rating themselves on a scale of 0-10 with 0 being "not religious at all" and 10 being "very religious" in response to the question, "Regardless of whether you belong to a particular religion, how religious would you say you are?"
|
||||
|
||||
We'll recycle some code from our previous import to bring in the Q57 data:
|
||||
|
||||
```{r}
|
||||
religiosity <- as_tibble(as_factor(climate_experience_data$Q57_1))
|
||||
names(religiosity) <- c("response")
|
||||
religiosity <- filter(religiosity, !is.na(response))
|
||||
religiosity_sums <- religiosity %>%
|
||||
dplyr::count(response) %>% # <1>
|
||||
dplyr::mutate(response = forcats::fct_rev(forcats::fct_inorder(response)))
|
||||
religiosity_sums <- religiosity_sums %>%
|
||||
dplyr::mutate(perc = scales::percent(n / sum(n), accuracy = .1, trim = FALSE))
|
||||
```
|
||||
|
||||
1. Note: we have removed `sort = TRUE` in the above statement as it will enforce sorting the data by quantities rather than the factor order. It wouldn't really make sense to plot this chart in the order of response.
|
||||
|
||||
Now, let's plot that data:
|
||||
```{r}
|
||||
caption <- "Respondent Religiosity"
|
||||
ggplot(religiosity_sums, aes(x = response, y = n, color=response)) +
|
||||
geom_col(colour = "white", aes(fill = response)) + # <1>
|
||||
## get rid of all elements except y axis labels + adjust plot margin
|
||||
coord_flip() + # <1>
|
||||
theme(plot.margin = margin(rep(15, 4))) +
|
||||
labs(caption = caption)
|
||||
```
|
||||
|
||||
We've added a few elements here:
|
||||
1. Colors, because colours are fun.
|
||||
2. `coord_flip` to rotate the chart so we have bars going horizontally
|
||||
|
||||
Since we're thinking about how things look just now, let's play with themes for a minute. `ggplot` is a really powerful tool for visualising information, but it also has some quite nice features for making things look pretty.
|
||||
|
||||
[If you'd like to take a proper deep dive on all this theme stuff, R-Charts has a great set of examples showing you how a number of different theme packages look in practice, ["R-Charts on Themes"](https://r-charts.com/ggplot2/themes/).]{.aside}
|
||||
|
||||
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 `theme_light()` in the following way:
|
||||
|
||||
```{r}
|
||||
ggplot(religiosity_sums, aes(x = response, y = n, color=response)) +
|
||||
geom_col(colour = "white", aes(fill = response)) + # <1>
|
||||
## get rid of all elements except y axis labels + adjust plot margin
|
||||
coord_flip() + # <1>
|
||||
theme(plot.margin = margin(rep(15, 4))) +
|
||||
labs(caption = caption) +
|
||||
theme_light()
|
||||
```
|
||||
|
||||
You can also use additional packages like `ggthemes()` or `hrbrthemes()` so for example we might want to try the `pander` theme which has it's own special (and very cheerful) colour palette.
|
||||
|
||||
```{r}
|
||||
library(ggthemes) |> suppressPackageStartupMessages()
|
||||
ggplot(religiosity_sums, aes(x = response, y = n, color=response)) + geom_col(colour = "white", aes(fill = response)) + coord_flip() + theme(plot.margin = margin(rep(15, 4))) + labs(caption = caption) +
|
||||
theme_pander() +
|
||||
scale_fill_pander()
|
||||
```
|
||||
|
||||
Or, you might try the well-crafted typgraphy from `hbrthemes` in the `theme_ipsum_pub` theme:
|
||||
|
||||
```{r}
|
||||
library(hrbrthemes) |> suppressPackageStartupMessages()
|
||||
ggplot(religiosity_sums, aes(x = response, y = n, color=response)) + geom_col(colour = "white", aes(fill = response)) + coord_flip() + theme(plot.margin = margin(rep(15, 4))) + labs(caption = caption) +
|
||||
theme_ipsum_pub() +
|
||||
scale_fill_pander()
|
||||
```
|
||||
We're going to come back to this chart, but let's set it to one side for a moment and build up a visualisation of the spirituality scale data. The spirituality scale questions come from research by ___ and ___. These researchers developed a series of questions which they asked respondents in a survey. The advantage here is that you're getting at the question of spirituality from a lot of different angles, and then you combine the scores from all the questions to get a mean "spirituality score".
|
||||
|
||||
```{r}
|
||||
### Spirituality scale --------------------------------------------------------------
|
||||
# Calculate overall mean spirituality score based on six questions:
|
||||
climate_experience_data$Q52_score <- rowMeans(select(climate_experience_data, Q52a_1:Q52f_1))
|
||||
```
|
||||
|
||||
Like we did in chapter 1, let's start by exploring the data and get a bit of a sense of the character of the responses overall. One good place to start is to find out the mean response for our two continum questions:
|
||||
|
||||
```{r}
|
||||
# t_testing and means
|
||||
|
||||
# Spirituality scale
|
||||
|
||||
# JK note to self: need to fix stat_summary plot here
|
||||
|
||||
# stat_summary(climate_experience_data$Q52_score)
|
||||
mean(climate_experience_data$Q52_score)
|
||||
|
||||
# Q57 Regardless of whether you belong to a particular religion, how religious would you say you are?
|
||||
# 0-10, Not religious at all => Very religious; mean=5.58
|
||||
|
||||
mean(climate_experience_data$Q57_1) # religiosity
|
||||
|
||||
# Q58 Apart from weddings, funerals and other special occasions, how often do you attend religious services?
|
||||
# coded at 1-5, lower value = stronger mean=3.439484
|
||||
|
||||
mean(climate_experience_data$Q58) # service attendance
|
||||
|
||||
# Q59 Apart from when you are at religious services, how often do you pray?
|
||||
# coded at 1-5, lower = stronger mean=2.50496
|
||||
|
||||
mean(climate_experience_data$Q59)
|
||||
```
|
||||
|
||||
Now let's try out some visualisations:
|
||||
|
||||
```{r}
|
||||
## Q52 Spirituality data ------------------------
|
||||
|
||||
q52_data <- select(climate_experience_data, Q52a_1:Q52f_1)
|
||||
# Data is at wide format, we need to make it 'tidy' or 'long'
|
||||
q52_data <- q52_data %>%
|
||||
gather(key="text", value="value") %>%
|
||||
# rename columns
|
||||
mutate(text = gsub("Q52_", "",text, ignore.case = TRUE)) %>%
|
||||
mutate(value = round(as.numeric(value),0))
|
||||
|
||||
# Change names of rows to question text
|
||||
q52_data <- q52_data %>%
|
||||
gather(key="text", value="value") %>%
|
||||
# rename columns
|
||||
mutate(text = gsub("Q52a_1", "In terms of questions I have about my life, my spirituality answers...",text, ignore.case = TRUE)) %>%
|
||||
mutate(text = gsub("Q52b_1", "Growing spiritually is important...",text, ignore.case = TRUE)) %>%
|
||||
mutate(text = gsub("Q52c_1", "When I’m faced with an important decision, spirituality plays a role...",text, ignore.case = TRUE)) %>%
|
||||
mutate(text = gsub("Q52d_1", "Spirituality is part of my life...",text, ignore.case = TRUE)) %>%
|
||||
mutate(text = gsub("Q52e_1", "When I think of things that help me grow and mature as a person, spirituality has an effect on my personal growth...",text, ignore.case = TRUE)) %>%
|
||||
mutate(text = gsub("Q52f_1", "My spiritual beliefs affect aspects of my life...",text, ignore.case = TRUE))
|
||||
|
||||
# Plot
|
||||
# Used for gradient colour schemes, as with violin plots
|
||||
library(viridis)
|
||||
|
||||
q52_plot <- q52_data %>%
|
||||
mutate(text = fct_reorder(text, value)) %>% # Reorder data
|
||||
ggplot( aes(x=text, y=value, fill=text, color=text)) +
|
||||
geom_boxplot() +
|
||||
scale_fill_viridis(discrete=TRUE, alpha=0.8) +
|
||||
geom_jitter(color="black", size=0.2, alpha=0.2) +
|
||||
theme_ipsum() +
|
||||
theme(legend.position="none", axis.text.y = element_text(size = 8)) +
|
||||
coord_flip() + # This switch X and Y axis and allows to get the horizontal version
|
||||
xlab("") +
|
||||
ylab("Spirituality scales") +
|
||||
scale_x_discrete(labels = function(x) str_wrap(x, width = 45))
|
||||
|
||||
# using gridExtra to specify explicit dimensions for printing
|
||||
q52_plot
|
||||
ggsave("figures/q52_boxplot.png", width = 20, height = 10, units = "cm")
|
||||
```
|
||||
|
||||
There's an enhanced version of this plot we can use, called `ggstatsplot()` to get a different view:
|
||||
|
||||
```{r}
|
||||
# As an alternative trying ggstatsplot:
|
||||
library(rstantools)
|
||||
library(ggstatsplot)
|
||||
q52_plot_alt <- ggbetweenstats(
|
||||
data = q52_data,
|
||||
x = text,
|
||||
y = value,
|
||||
outlier.tagging = TRUE,
|
||||
title = "Intrinsic Spirituality Scale Responses"
|
||||
) +
|
||||
scale_x_discrete(labels = function(x) str_wrap(x, width = 30)) +
|
||||
# Customizations
|
||||
theme(
|
||||
# Change fonts in the plot
|
||||
text = element_text(family = "Helvetica", size = 8, color = "black"),
|
||||
plot.title = element_text(
|
||||
family = "Abril Fatface",
|
||||
size = 20,
|
||||
face = "bold",
|
||||
color = "#2a475e"
|
||||
),
|
||||
# Statistical annotations below the main title
|
||||
plot.subtitle = element_text(
|
||||
family = "Helvetica",
|
||||
size = 12,
|
||||
face = "bold",
|
||||
color="#1b2838"
|
||||
),
|
||||
plot.title.position = "plot", # slightly different from default
|
||||
axis.text = element_text(size = 10, color = "black"),
|
||||
axis.text.x = element_text(size = 7),
|
||||
axis.title = element_text(size = 12),
|
||||
axis.line = element_line(colour = "grey50"),
|
||||
panel.grid.minor = element_blank(),
|
||||
panel.grid.major.x = element_blank(),
|
||||
panel.grid = element_line(color = "#b4aea9"),
|
||||
panel.grid.major.y = element_line(linetype = "dashed"),
|
||||
panel.background = element_rect(fill = "#fbf9f4", color = "#fbf9f4"),
|
||||
plot.background = element_rect(fill = "#fbf9f4", color = "#fbf9f4")
|
||||
)
|
||||
|
||||
q52_plot_alt
|
||||
ggsave("figures/q52_plot_alt.png", width = 20, height = 12, units = "cm")
|
||||
|
||||
```
|
||||
|
||||
One thing that might be interesting to test here is whether spirituality and religiosity are similar for our respondents.
|
||||
|
||||
```{r}
|
||||
ggplot(climate_experience_data, aes(x=Q52_score, y=Q57_1)) + labs(x="Spirituality Scale Score", y = "How Religious?") +
|
||||
geom_point(size=1, alpha=0.3) + geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95)
|
||||
|
||||
# using http://sthda.com/english/wiki/ggplot2-scatter-plots-quick-start-guide-r-software-and-data-visualization
|
||||
|
||||
ggplot(climate_experience_data, aes(x=Q52_score, y=Q57_1)) +
|
||||
labs(x="Spirituality Scale Score", y = "How Religious?") +
|
||||
geom_point(size=1, alpha=0.3) + stat_density_2d(aes(fill = ..level..), geom="polygon", alpha=0.3)+
|
||||
scale_fill_gradient(low="blue", high="red") +
|
||||
theme_minimal()
|
||||
```
|
||||
|
||||
Because the responses to these two questions, about spirituality and religiosity are on a continuum, we can also use them, like we did in previous charts, to subset other datasets. A simple way of doing this is to separate our respondents into "high," "medium," and "low" bins for the two questions. Rather than working with hard values, like assigning 0-3, 4-6 and 7-10 for low medium and high, we'll work with the range of values that respondents actually chose. This is particularly appropriate as the median answer to these questions was not "5". So we'll use the statistical concept of standard deviation, which R can calculate almost magically for us, in the following way:
|
||||
|
||||
```{r}
|
||||
# Create low/med/high bins based on Mean and +1/-1 Standard Deviation
|
||||
climate_experience_data <- climate_experience_data %>%
|
||||
mutate(
|
||||
Q52_bin = case_when(
|
||||
Q52_score > mean(Q52_score) + sd(Q52_score) ~ "high",
|
||||
Q52_score < mean(Q52_score) - sd(Q52_score) ~ "low",
|
||||
TRUE ~ "medium"
|
||||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
|
||||
## Q57 subsetting based on Religiosity --------------------------------------------------------------
|
||||
climate_experience_data <- climate_experience_data %>%
|
||||
mutate(
|
||||
Q57_bin = case_when(
|
||||
Q57_1 > mean(Q57_1) + sd(Q57_1) ~ "high",
|
||||
Q57_1 < mean(Q57_1) - sd(Q57_1) ~ "low",
|
||||
TRUE ~ "medium"
|
||||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
|
||||
As in the previous chapter, it's useful to explore multiple factors when possible. So I'd like us to take the data about political affiliation to visualise alongside our religion and spirituality data. this will help us to see where effects are more or less significant and give us a point of comparison.
|
||||
|
||||
```{r}
|
||||
## Q53 subsetting based on Political LR orientation --------------------------------------------------------------
|
||||
# Generate low/med/high bins based on Mean and SD
|
||||
climate_experience_data <- climate_experience_data %>%
|
||||
mutate(
|
||||
Q53_bin = case_when(
|
||||
Q53_1 > mean(Q53_1) + sd(Q53_1) ~ "high",
|
||||
Q53_1 < mean(Q53_1) - sd(Q53_1) ~ "low",
|
||||
TRUE ~ "medium"
|
||||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
|
||||
Now let's use those bins to explore some of the responses about attitudes towards climate change:
|
||||
|
||||
```{r}
|
||||
# Faceted plot working with 3x3 grid
|
||||
df <- select(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q58)
|
||||
names(df) <- c("Q52_bin", "Q53_bin", "Q57_bin", "response")
|
||||
facet_names <- c(`Q52_bin` = "Spirituality", `Q53_bin` = "Politics L/R", `Q57_bin` = "Religiosity", `low`="low", `medium`="medium", `high`="high")
|
||||
facet_labeller <- function(variable,value){return(facet_names[value])}
|
||||
df$response <- factor(df$response, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
|
||||
df$response <- fct_recode(df$response, "More than once a week" = "1", "Once a week" = "2", "At least once a month" = "3", "Only on special holy days" = "4", "Never" = "5")
|
||||
df %>%
|
||||
# we need to get the data including facet info in long format, so we use pivot_longer()
|
||||
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
|
||||
# add counts for plot below
|
||||
count(response, bin_name, b) %>%
|
||||
group_by(bin_name,b) %>%
|
||||
mutate(perc=paste0(round(n*100/sum(n),1),"%")) %>%
|
||||
# run ggplot
|
||||
ggplot(aes(x = n, y = "", fill = response)) +
|
||||
geom_col(position=position_fill(), aes(fill=response)) +
|
||||
geom_text(aes(label = perc), position = position_fill(vjust=.5), size=2) +
|
||||
scale_fill_brewer(palette = "Dark2", type = "qual") +
|
||||
scale_x_continuous(labels = scales::percent_format()) +
|
||||
facet_grid(vars(b), vars(bin_name), labeller=as_labeller(facet_names)) +
|
||||
labs(caption = caption, x = "", y = "") +
|
||||
guides(fill = guide_legend(title = NULL))
|
||||
ggsave("figures/q58_faceted.png", width = 30, height = 10, units = "cm")
|
||||
```
|
||||
|
||||
<!--
|
||||
Use mutate to put "prefer not to say" at the bottom
|
||||
# Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels
|
||||
|
@ -151,17 +453,6 @@ religious_affiliationc_table <- chart_single_result_flextable(climate_experience
|
|||
religious_affiliationc_table
|
||||
save_as_docx(religious_affiliationc_table, path = "./figures/q56_religious_affiliation_islam.docx")
|
||||
|
||||
# Q57
|
||||
# Religiosity
|
||||
caption <- "Respondent Religiosity"
|
||||
religiosity <- qualtrics_process_single_multiple_choice(as.character(climate_experience_data_named$Q57_1))
|
||||
religiosity_plot <- plot_horizontal_bar(religiosity)
|
||||
religiosity_plot <- religiosity_plot + labs(caption = caption, x = "", y = "")
|
||||
religiosity_plot
|
||||
ggsave("figures/q57_religiosity_plot.png", width = 20, height = 10, units = "cm")
|
||||
religiosity_table <- chart_single_result_flextable(climate_experience_data_named$Q57_1, desc(Variable))
|
||||
religiosity_table
|
||||
save_as_docx(religious_affiliationc_table, path = "./figures/q57_religiousity.docx")
|
||||
|
||||
# Q58
|
||||
|
||||
|
@ -318,19 +609,6 @@ climate_experience_data <- climate_experience_data %>%
|
|||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
### Spirituality scale --------------------------------------------------------------
|
||||
# Calculate overall mean spirituality score based on six questions:
|
||||
climate_experience_data$Q52_score <- rowMeans(select(climate_experience_data, Q52a_1:Q52f_1))
|
||||
|
||||
# Create low/med/high bins based on Mean and +1/-1 Standard Deviation
|
||||
climate_experience_data <- climate_experience_data %>%
|
||||
mutate(
|
||||
Q52_bin = case_when(
|
||||
Q52_score > mean(Q52_score) + sd(Q52_score) ~ "high",
|
||||
Q52_score < mean(Q52_score) - sd(Q52_score) ~ "low",
|
||||
TRUE ~ "medium"
|
||||
) %>% factor(levels = c("low", "medium", "high"))
|
||||
)
|
||||
|
||||
-->
|
||||
|
||||
|
|