updated ch2

This commit is contained in:
Jeremy Kidwell 2023-10-13 07:04:52 +01:00
parent 3eeab10d02
commit 938e88dc4f
7 changed files with 53 additions and 24 deletions

View file

@ -572,7 +572,7 @@ div.csl-indent {
<dl class="code-annotation-container-grid"> <dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt> <dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt>
<dd> <dd>
<span data-code-cell="annotated-cell-10" data-code-lines="1" data-code-annotation="2">Well re-order the column by size.</span> <span data-code-cell="annotated-cell-10" data-code-annotation="2" data-code-lines="1">Well re-order the column by size.</span>
</dd> </dd>
</dl> </dl>
</div> </div>
@ -595,19 +595,19 @@ div.csl-indent {
<dl class="code-annotation-container-grid"> <dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt> <dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
<dd> <dd>
<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> <span data-code-cell="annotated-cell-11" data-code-annotation="1" data-code-lines="1">First, remove the column with region names and the totals for the regions as we want just integer data.</span>
</dd> </dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt> <dt data-target-cell="annotated-cell-11" data-target-annotation="2">2</dt>
<dd> <dd>
<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 &lt;- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that youll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals &lt;- as.data.frame(uk_census_2021_religion_totals)</code></span> <span data-code-cell="annotated-cell-11" data-code-annotation="2" data-code-lines="3">Second calculate the totals. In this example we use the tidyverse library <code>dplyr()</code>, but you can also do this using base R with <code>colsums()</code> like this: <code>uk_census_2021_religion_totals &lt;- colSums(uk_census_2021_religion_totals, na.rm = TRUE)</code>. The downside with base R is that youll also need to convert the result into a dataframe for <code>ggplot</code> like this: <code>uk_census_2021_religion_totals &lt;- as.data.frame(uk_census_2021_religion_totals)</code></span>
</dd> </dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="3">3</dt> <dt data-target-cell="annotated-cell-11" data-target-annotation="3">3</dt>
<dd> <dd>
<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> <span data-code-cell="annotated-cell-11" data-code-annotation="3" data-code-lines="4">In order to visualise this data using ggplot, we need to shift this data from wide to long format. This is a quick job using gather()</span>
</dd> </dd>
<dt data-target-cell="annotated-cell-11" data-target-annotation="4">4</dt> <dt data-target-cell="annotated-cell-11" data-target-annotation="4">4</dt>
<dd> <dd>
<span data-code-cell="annotated-cell-11" data-code-lines="5" data-code-annotation="4">Now plot it out and have a look!</span> <span data-code-cell="annotated-cell-11" data-code-annotation="4" data-code-lines="5">Now plot it out and have a look!</span>
</dd> </dd>
</dl> </dl>
</div> </div>

View file

@ -336,15 +336,15 @@ So <em>Whos</em> Religious?
<dl class="code-annotation-container-grid"> <dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt> <dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
<dd> <dd>
<span data-code-annotation="1" data-code-cell="annotated-cell-4" data-code-lines="2">First we generate new a dataframe with sums per category and</span> <span data-code-lines="2" data-code-cell="annotated-cell-4" data-code-annotation="1">First we generate new a dataframe with sums per category and</span>
</dd> </dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt> <dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt>
<dd> <dd>
<span data-code-annotation="2" data-code-cell="annotated-cell-4" data-code-lines="3">…sort in descending order</span> <span data-code-lines="3" data-code-cell="annotated-cell-4" data-code-annotation="2">…sort in descending order</span>
</dd> </dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt> <dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt>
<dd> <dd>
<span data-code-annotation="3" data-code-cell="annotated-cell-4" data-code-lines="5">Then we add new column with percentages based on the sums youve just generated</span> <span data-code-lines="5" data-code-cell="annotated-cell-4" data-code-annotation="3">Then we add new column with percentages based on the sums youve just generated</span>
</dd> </dd>
</dl> </dl>
</div> </div>
@ -413,25 +413,36 @@ So <em>Whos</em> Religious?
<dl class="code-annotation-container-grid"> <dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-8" data-target-annotation="1">1</dt> <dt data-target-cell="annotated-cell-8" data-target-annotation="1">1</dt>
<dd> <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 wouldnt really make sense to plot this chart in the order of response.</span> <span data-code-lines="5" data-code-cell="annotated-cell-8" data-code-annotation="1">Note: we have removed <code>sort = TRUE</code> in the above statement as it will enforce sorting the data by quantities rather than the factor order. It wouldnt really make sense to plot this chart in the order of response.</span>
</dd> </dd>
</dl> </dl>
</div> </div>
</div> </div>
<p>Now, lets plot that data:</p> <p>Now, lets plot that data:</p>
<div class="cell"> <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">&lt;-</span> <span class="st">"Respondent Religiosity"</span></span> <div class="sourceCode cell-code" id="annotated-cell-9"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-9-1"><a href="#annotated-cell-9-1" aria-hidden="true" tabindex="-1"></a>caption <span class="ot">&lt;-</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-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> <a class="code-annotation-anchor" data-target-cell="annotated-cell-9" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-9-3" class="code-annotation-target"><a href="#annotated-cell-9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">colour =</span> <span class="st">"white"</span>, <span class="fu">aes</span>(<span class="at">fill =</span> response)) <span class="sc">+</span></span>
<span id="annotated-cell-9-4"><a href="#annotated-cell-9-4" aria-hidden="true" tabindex="-1"></a> <span class="do">## get rid of all elements except y axis labels + adjust plot margin</span></span> <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> <a class="code-annotation-anchor" data-target-cell="annotated-cell-9" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-9-5" class="code-annotation-target"><a href="#annotated-cell-9-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="annotated-cell-9-6"><a href="#annotated-cell-9-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">plot.margin =</span> <span class="fu">margin</span>(<span class="fu">rep</span>(<span class="dv">15</span>, <span class="dv">4</span>))) <span class="sc">+</span></span> <span id="annotated-cell-9-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> <span id="annotated-cell-9-7"><a href="#annotated-cell-9-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">caption =</span> caption)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-9" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="3" data-code-cell="annotated-cell-9" data-code-annotation="1">Weve added colors, because colours are fun.</span>
</dd>
<dt data-target-cell="annotated-cell-9" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="5" data-code-cell="annotated-cell-9" data-code-annotation="2">Also new here is <code>coord_flip</code> to rotate the chart so we have bars going horizontally</span>
</dd>
</dl>
</div>
<div class="cell-output-display"> <div class="cell-output-display">
<p><img src="chapter_2_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p> <p><img src="chapter_2_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
</div> </div>
</div> </div>
<p>Weve 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 were thinking about how things look just now, lets play with themes for a minute. <code>ggplot</code> is a really powerful tool for visualising information, but it also has some quite nice features for making things look pretty.</p> <p>Since were thinking about how things look just now, lets play with themes for a minute. <code>ggplot</code> is a really powerful tool for visualising information, but it also has some quite nice features for making things look pretty.</p>
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">If youd like to take a proper deep dive on all this theme stuff, R-Charts has a great set of examples showing you how a number of different theme packages look in practice, <a href="https://r-charts.com/ggplot2/themes/">“R-Charts on Themes”</a>.</span></div></div> <div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="">If youd like to take a proper deep dive on all this theme stuff, R-Charts has a great set of examples showing you how a number of different theme packages look in practice, <a href="https://r-charts.com/ggplot2/themes/">“R-Charts on Themes”</a>.</span></div></div>
<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> <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>
@ -479,8 +490,10 @@ So <em>Whos</em> Religious?
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></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-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-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-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="co"># JK note to self: need to fix stat_summary plot here</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a><span 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> <span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="co"># stat_summary(climate_experience_data$Q52_score)</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(climate_experience_data<span class="sc">$</span>Q52_score)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout"> <div class="cell-output cell-output-stdout">
<pre><code>[1] 6.047454</code></pre> <pre><code>[1] 6.047454</code></pre>
</div> </div>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 401 KiB

After

Width:  |  Height:  |  Size: 401 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 204 KiB

After

Width:  |  Height:  |  Size: 203 KiB

File diff suppressed because one or more lines are too long

View file

@ -131,7 +131,6 @@ plot1 <- ggplot(religious_affiliation_ethnicity_sums, aes(x = n, y = Religion))
geom_col(colour = "white") + facet_wrap(~Ethnicity, scales="free_x") geom_col(colour = "white") + facet_wrap(~Ethnicity, scales="free_x")
ggsave("chart.png", plot=plot1, width = 8, height = 10, units=c("in")) ggsave("chart.png", plot=plot1, width = 8, height = 10, units=c("in"))
``` ```
## Working With a Continum: Religiosity and Spirituality ## Working With a Continum: Religiosity and Spirituality
@ -161,14 +160,13 @@ caption <- "Respondent Religiosity"
ggplot(religiosity_sums, aes(x = response, y = n, color=response)) + ggplot(religiosity_sums, aes(x = response, y = n, color=response)) +
geom_col(colour = "white", aes(fill = response)) + # <1> geom_col(colour = "white", aes(fill = response)) + # <1>
## get rid of all elements except y axis labels + adjust plot margin ## get rid of all elements except y axis labels + adjust plot margin
coord_flip() + # <1> coord_flip() + # <2>
theme(plot.margin = margin(rep(15, 4))) + theme(plot.margin = margin(rep(15, 4))) +
labs(caption = caption) labs(caption = caption)
``` ```
We've added a few elements here: 1. We've added colors, because colours are fun.
1. Colors, because colours are fun. 2. Also new here is `coord_flip` to rotate the chart so we have bars going horizontally
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. 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.
@ -213,15 +211,33 @@ climate_experience_data$Q52_score <- rowMeans(select(climate_experience_data, Q5
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: 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}
# Spirituality first:
mean(climate_experience_data$Q52_score)
# Then religiosity
mean(climate_experience_data$Q57_1)
```
```{r} ```{r}
# t_testing and means # t_testing and means
# Spirituality scale # Spirituality scale
library(rstatix)
religiosity_stats <- as.tibble(climate_experience_data$Q57_1)
spirituality_stats <- as.tibble(climate_experience_data$Q52_score)
plot(religiosity_stats ~ spirituality_stats, data=CO2)
stats %>% get_summary_stats(value, type="mean_sd")
# JK note to self: need to fix stat_summary plot here # JK note to self: need to fix stat_summary plot here
# stat_summary(climate_experience_data$Q52_score) # 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? # 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 # 0-10, Not religious at all => Very religious; mean=5.58