diff --git a/docs/Hacking-Religion--TRS---Data-Science-in-Action.pdf b/docs/Hacking-Religion--TRS---Data-Science-in-Action.pdf index 65b92bf..b9bee7a 100644 Binary files a/docs/Hacking-Religion--TRS---Data-Science-in-Action.pdf and b/docs/Hacking-Religion--TRS---Data-Science-in-Action.pdf differ diff --git a/docs/chapter_1.html b/docs/chapter_1.html index 2a5fc6f..7f22509 100644 --- a/docs/chapter_1.html +++ b/docs/chapter_1.html @@ -579,7 +579,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
2
-We’ll re-order the column by size. +We’ll re-order the column by size.
@@ -602,19 +602,19 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
1
-First, remove the column with region names and the totals for the regions as we want just integer data. +First, remove the column with region names and the totals for the regions as we want just integer data.
2
-Second calculate the totals. In this example we use the tidyverse library dplyr(), but you can also do this using base R with colsums() like this: uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE). The downside with base R is that you’ll also need to convert the result into a dataframe for ggplot like this: uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals) +Second calculate the totals. In this example we use the tidyverse library dplyr(), but you can also do this using base R with colsums() like this: uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE). The downside with base R is that you’ll also need to convert the result into a dataframe for ggplot like this: uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)
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() +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()
4
-Now plot it out and have a look! +Now plot it out and have a look!
@@ -641,6 +641,7 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all

If you’re looking closely, you will notice that I’ve added two elements to our previous ggplot. I’ve asked ggplot to fill in the columns with reference to the dataset column we’ve just created. Then I’ve also asked ggplot to alter the position="dodge" which places bars side by side rather than stacked on top of one another. You can give it a try without this instruction to see how this works. We will use stacked bars in a later chapter, so remember this feature.

If you inspect our chart, you can see that we’re getting closer, but it’s not really that helpful to compare the totals. What we need to do is get percentages that can be compared side by side. This is easy to do using another dplyr feature mutate:

+

It’s worth noting that an alternative approach is to leave the numbers intact and simply label them differently so they render as percentages on your charts. You can do this with the `scales() library and the label_percent() function. The downside of this approach is that it won’t transfer to tables if you make them.
uk_census_2021_religion_totals <- uk_census_2021_religion_totals %>% 
   dplyr::mutate(perc = scales::percent(value / sum(value), accuracy = 0.1, trim = FALSE))
@@ -681,7 +682,9 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all
 
 

2.5 Is your chart accurate? Telling the truth in data science

-

There is some technical work yet to be done fine-tuning the visualisation of our chart here. But I’d like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. It’s possible (though perhaps unlikely) that all those non-responses were Sikh respondents who felt uncomfortable identifying themselves on such a survey. If even half of the non-responses were of this nature, this would dramatically shift the results especially in comparison to other minority groups. So there is some work for us to do here in representing non-response as a category on the census. But it’s equally possible that someone might feel uncertain when answering, but nonetheless land on a particular decision marking “Christian” when they wondered if they should instead tick “no religion. Some surveys attempt to capture uncertainty in this way, asking respondents to mark how confident they are about their answers, but the census hasn’t capture this so we simply don’t know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This hasn’t been done on the census (which would have been quite hard work given that most of the instruments were printed hard copies!), so again, we can’t really be sure if those answers given are stable. Finally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.

+

There is some technical work yet to be done fine-tuning the visualisation of our chart here. But I’d like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. It’s possible (though perhaps unlikely) that all those non-responses were Sikh respondents who felt uncomfortable identifying themselves on such a survey. If even half of the non-responses were of this nature, this would dramatically shift the results especially in comparison to other minority groups. So there is some work for us to do here in representing non-response as a category on the census.

+

It’s equally possible that someone might feel uncertain when answering, but nonetheless land on a particular decision marking “Christian” when they wondered if they should instead tick “no religion. Some surveys attempt to capture uncertainty in this way, asking respondents to mark how confident they are about their answers, but the census hasn’t capture this so we simply don’t know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This hasn’t been done on the census (which would have been quite hard work given that most of the instruments were printed hard copies!), so again, we can’t really be sure if those answers given are stable.

+

Finally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.

What does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive, almost completely population level, sample so there is additional validity there. The easiest way to represent all this reality in the form of speaking truthfully about our data is to acknowledge that however valid it may seem, it is nonetheless a snapshot. For this reason, I would always advise that the best title for a chart is one which specifies the data set. We should also probably do something different with those non-responses:

ggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position="dodge", stat ="identity", colour = "black") + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2021 Census of England and Wales") + xlab("") + ylab("")
@@ -699,69 +702,216 @@ i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all

2.7 Multifactor Visualisation

One element of R data analysis that can get really interesting is working with multiple variables. Above we’ve looked at the breakdown of religious affiliation across the whole of England and Wales (Scotland operates an independent census), and by placing this data alongside a specific region, we’ve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantative variable (also known as bivariate data) into the mix, however can also generate a lot more information and we have to think about visualising it in different ways which can still communicate with visual clarity in spite of the additional visual noise which is inevitable with enhanced complexity. Let’s have a look at the way that religion in England and Wales breaks down by ethnicity.

+
+
+
+ +
+
+What is Nomis? +
+
+
+

For the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It’s worth noting that if you’re not in the UK, there are similar options for other countries. Nearly every R textbook I’ve ever seen works with USA census data, so you’ll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.

+

Here’s the process to identify a dataset within the nomis platform:

-
library(nomisr)
-
-# Process to explore nomis() data for specific datasets
-religion_search <- nomis_search(name = "*Religion*")
-religion_measures <- nomis_get_metadata("NM_529_1", "measures")
-tibble::glimpse(religion_measures)
-
-
Rows: 2
-Columns: 3
-$ id             <chr> "20100", "20301"
-$ label.en       <chr> "value", "percent"
-$ description.en <chr> "value", "percent"
-
-
religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE")
-
-# Get table of Census 2011 religion data from nomis
-z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301))
-# Filter down to simplified dataset with England / Wales and percentages without totals
-uk_census_2011_religion <- filter(z, GEOGRAPHY_NAME=="England and Wales" & RURAL_URBAN_NAME=="Total" & C_RELPUK11_NAME != "All categories: Religion")
-# Drop unnecessary columns
-uk_census_2011_religion <- select(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)
-# Plot results
-plot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
-ggsave(filename = "plot.png", plot = plot1)
-
-
Saving 7 x 5 in image
-
-
# grab data from nomis for 2011 census religion / ethnicity table
-z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100))
-# select relevant columns
-uk_census_2011_religion_ethnicitity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)
-# Filter down to simplified dataset with England / Wales and percentages without totals
-uk_census_2011_religion_ethnicitity <- filter(uk_census_2011_religion_ethnicitity, GEOGRAPHY_NAME=="England and Wales" & C_RELPUK11_NAME != "All categories: Religion" & C_ETHPUK11_NAME != "All categories: Ethnic group")
-# Simplify data to only include general totals and omit subcategories
-uk_census_2011_religion_ethnicitity <- uk_census_2011_religion_ethnicitity %>% filter(grepl('Total', C_ETHPUK11_NAME))
-
-ggplot(uk_census_2011_religion_ethnicitity, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position="dodge", stat ="identity", colour = "black") + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
-
-

+
# Process to explore nomis() data for specific datasets
+library(nomisr)
+# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
+#religion_search <- nomis_search(name = "*Religion*")
+#religion_measures <- nomis_get_metadata("ST104", "measures")
+#tibble::glimpse(religion_measures)
+#religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE")
+
-

The trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.

-
# Filter down to simplified dataset with England / Wales and percentages without totals
-uk_census_2011_religion_ethnicitity_white <- filter(uk_census_2011_religion_ethnicitity, C_ETHPUK11_NAME == "White: Total")
-uk_census_2011_religion_ethnicitity_nonwhite <- filter(uk_census_2011_religion_ethnicitity, C_ETHPUK11_NAME != "White: Total")
-
-ggplot(uk_census_2011_religion_ethnicitity_nonwhite, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position="dodge", stat ="identity", colour = "black") + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
library(nomisr)
+# Get table of Census 2011 religion data from nomis
+# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
+#z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301))
+#saveRDS(z, file = "z.rds")
+z <- readRDS(file = (here("example_data", "z.rds")))
+
+# Filter down to simplified dataset with England / Wales and percentages without totals
+uk_census_2011_religion <- filter(z, GEOGRAPHY_NAME=="England and Wales" & RURAL_URBAN_NAME=="Total" & C_RELPUK11_NAME != "All categories: Religion")
+# Drop unnecessary columns
+uk_census_2011_religion <- select(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)
+# Plot results
+plot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+# ggsave(filename = "plot.png", plot = plot1)
+
+# grab daata from nomis for 2001 census religion / ethnicity
+
+# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:
+#z0 <- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100))
+#saveRDS(z0, file = "z0.rds")
+z0 <- readRDS(file = (here("example_data", "z0.rds")))
+
+# select relevant columns
+uk_census_2001_religion_ethnicity <- select(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)
+# Filter down to simplified dataset with England / Wales and percentages without totals
+uk_census_2001_religion_ethnicity <- filter(uk_census_2001_religion_ethnicity, GEOGRAPHY_NAME=="England and Wales" & C_RELPUK11_NAME != "All categories: Religion")
+# Simplify data to only include general totals and omit subcategories
+uk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% filter(grepl('Total', C_ETHHUK11_NAME))
+
+# grab data from nomis for 2011 census religion / ethnicity table
+# commenting out nomis_get temporarily until I can get renv working properly here
+#z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100))
+#saveRDS(z1, file = "z1.rds")
+z1 <- readRDS(file = (here("example_data", "z1.rds")))
+
+# select relevant columns
+uk_census_2011_religion_ethnicity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)
+# Filter down to simplified dataset with England / Wales and percentages without totals
+uk_census_2011_religion_ethnicity <- filter(uk_census_2011_religion_ethnicity, GEOGRAPHY_NAME=="England and Wales" & C_RELPUK11_NAME != "All categories: Religion" & C_ETHPUK11_NAME != "All categories: Ethnic group")
+# Simplify data to only include general totals and omit subcategories
+uk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% filter(grepl('Total', C_ETHPUK11_NAME))
+
+# grab data from nomis for 2021 census religion / ethnicity table
+#z2 <- nomis_get_data(id = "NM_2131_1", time = "latest", geography = "TYPE499", measures=c(20100))
+#saveRDS(z2, file = "z2.rds")
+z2 <- readRDS(file = (here("example_data", "z2.rds")))
+
+# select relevant columns
+uk_census_2021_religion_ethnicity <- select(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)
+# Filter down to simplified dataset with England / Wales and percentages without totals
+uk_census_2021_religion_ethnicity <- filter(uk_census_2021_religion_ethnicity, GEOGRAPHY_NAME=="England and Wales" & C2021_RELIGION_10_NAME != "Total" & C2021_ETH_8_NAME != "Total")
+# 2021 census includes white sub-groups so we need to omit those so we just have totals:
+uk_census_2021_religion_ethnicity <- filter(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME != "White: English, Welsh, Scottish, Northern Irish or British" & C2021_ETH_8_NAME != "White: Irish" & C2021_ETH_8_NAME != "White: Gypsy or Irish Traveller, Roma or Other White")
+
+ggplot(uk_census_2011_religion_ethnicity, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position="dodge", stat ="identity", colour = "black") + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

-

This still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.

+

The trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.

-
ggplot(uk_census_2011_religion_ethnicitity_nonwhite, aes(x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~C_ETHPUK11_NAME, ncol = 2) + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2011 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
# Filter down to simplified dataset with England / Wales and percentages without totals
+uk_census_2011_religion_ethnicity_white <- filter(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME == "White: Total")
+uk_census_2011_religion_ethnicity_nonwhite <- filter(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME != "White: Total")
+
+ggplot(uk_census_2011_religion_ethnicity_nonwhite, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position="dodge", stat ="identity", colour = "black") + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

- +

This still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.

+
+
ggplot(uk_census_2011_religion_ethnicity_nonwhite, aes(x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~C_ETHPUK11_NAME, ncol = 2) + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2011 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
+

+
+
+

For our finale chart, I’d like to take the faceted chart we’ve just done, and add in totals for the previous two census years (2001 and 2011) so we can see how trends are changing in terms of religious affiliation within ethnic self-identification categories. We’ll draw on some techniques we’re already developed above using rbind() to connect up each of these charts (after we’ve added a column identifying each chart by the census year). We will also need to use one new technique to change the wording of ethnic categories as this isn’t consistent from one census to the next and ggplot will struggle to chart things if the terms being used are exactly the same. We’ll use mutate() again to accomplish this with some slightly different code.

+
+
# First add column to each dataframe so we don't lose track of the census it comes from:
+uk_census_2001_religion_ethnicity$dataset <- c("2001")
+uk_census_2011_religion_ethnicity$dataset <- c("2011")
+uk_census_2021_religion_ethnicity$dataset <- c("2021")
+
+# Let's tidy the names of each column:
+
+names(uk_census_2001_religion_ethnicity) <- c("Geography", "Religion", "Ethnicity", "Value", "Year")
+names(uk_census_2011_religion_ethnicity) <- c("Geography", "Religion", "Ethnicity", "Value", "Year")
+names(uk_census_2021_religion_ethnicity) <- c("Geography", "Religion", "Ethnicity", "Value", "Year")
+
+# Next we need to change the terms using mutate()
+uk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% 
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^White: Total$", replacement = "White")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Mixed: Total$", replacement = "Mixed")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Asian: Total$", replacement = "Asian")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Black or Black British: Total$", replacement = "Black")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Chinese or Other ethnic group: Total$", replacement = "Other"))
+  
+uk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% 
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^White: Total$", replacement = "White")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Mixed/multiple ethnic group: Total$", replacement = "Mixed")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Asian/Asian British: Total$", replacement = "Asian")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Black/African/Caribbean/Black British: Total$", replacement = "Black")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Other ethnic group: Total$", replacement = "Other"))
+
+uk_census_2021_religion_ethnicity <- uk_census_2021_religion_ethnicity %>% 
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^White: Total$", replacement = "White")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Mixed or Multiple ethnic groups$", replacement = "Mixed")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Asian, Asian British or Asian Welsh$", replacement = "Asian")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Black, Black British, Black Welsh, Caribbean or African$", replacement = "Black")) %>%
+  mutate(Ethnicity = str_replace_all(Ethnicity, 
+            pattern = "^Other ethnic group$", replacement = "Other"))
+
+# Now let's merge the tables:
+
+uk_census_merged_religion_ethnicity <- rbind(uk_census_2021_religion_ethnicity, uk_census_2011_religion_ethnicity)
+
+uk_census_merged_religion_ethnicity <- rbind(uk_census_merged_religion_ethnicity, uk_census_2001_religion_ethnicity)
+
+# As above, we'll split out non-white and white:
+
+uk_census_merged_religion_ethnicity_nonwhite <- filter(uk_census_merged_religion_ethnicity, Ethnicity != "White")
+
+# Time to plot!
+
+ggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion, y=Value)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~Ethnicity, ncol = 2) + scale_fill_brewer(palette = "Set1") + ggtitle("Religious Affiliation in the 2001-2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
+

+
+
+

There are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn’t really very easy to read. You can use the very powerful and flexible scales() library to bring in some more readable formatting of numbers in a variety of places in R including in ggplot visualizations.

+
+
library(scales)
+
+

+Attaching package: 'scales'
+
+
+
The following object is masked from 'package:purrr':
+
+    discard
+
+
+
The following object is masked from 'package:readr':
+
+    col_factor
+
+
ggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion, y=Value)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~Ethnicity, ncol = 2) + scale_fill_brewer(palette = "Set1") + scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6), breaks = breaks_extended(8)) + ggtitle("Religious Affiliation in the 2001-2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
+

+
+
# https://ggplot2-book.org/scales-position#sec-position-continuous-breaks
+
+

This chart shows an increase in almost every category, though it’s a bit hard to read in some cases. However, this information is based on the increase in raw numbers. It’s possbile that numbers may be going up, but in some cases the percentage share for a particular category has actually gone down. Let’s transform and visualise our data as percentages to see what kind of trends we can actually isolate:

+
+
uk_census_merged_religion_ethnicity <- uk_census_merged_religion_ethnicity %>%
+  group_by(Ethnicity, Year) %>%
+  dplyr::mutate(Percent = Value/sum(Value))
+
+ggplot(uk_census_merged_religion_ethnicity, aes(fill=Year, x=Religion, y=Percent)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~Ethnicity, scales="free_x") + scale_fill_brewer(palette = "Set1") + scale_y_continuous(labels = scales::percent) + ggtitle("Religious Affiliation in the 2001-2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
+

+
+
+

Now you can see why this shift is important - the visualisation tells a completely different story in some cases across the two different charts. In the first, working off raw numbers we see a net increase in Christianity across all categories. But if we take into account the fact that the overall share of population is growing for each of these groups, their actual composition is changing in a different direction. The proportion of each group is declining across the three census periods (albeit with an exception for the “Other” category from 2011 to 2021).

+

To highlight a few features of this final plot, I’ve used a specific feature within facet_wrap scales = "free_x" to let each of the individual facets adjust the total range on the x-axis. Since we’re looking at trends here and not absolute values, having correspondence across scales isn’t important and this makes for something a bit more visually tidy. I’ve also shifted the code for scale_y_continuous to render values as percentages (rather than millions).

+

In case you want to print this plot out and hang it on your wall, you can use the ggsave tool to render the chart as an image file:

+
+
plot1 <- ggplot(uk_census_merged_religion_ethnicity, aes(fill=Year, x=Religion, y=Percent)) + geom_bar(position="dodge", stat ="identity", colour = "black") + facet_wrap(~Ethnicity, scales="free_x") + scale_fill_brewer(palette = "Set1") + scale_y_continuous(labels = scales::percent) + ggtitle("Religious Affiliation in the 2001-2021 Census of England and Wales") + xlab("") + ylab("") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
+ggsave("chart.png", plot=plot1, width = 8, height = 10, units=c("in"))
+

References

diff --git a/docs/chapter_1_files/figure-html/unnamed-chunk-18-1.png b/docs/chapter_1_files/figure-html/unnamed-chunk-18-1.png index b08d52c..ad999cc 100644 Binary files a/docs/chapter_1_files/figure-html/unnamed-chunk-18-1.png and b/docs/chapter_1_files/figure-html/unnamed-chunk-18-1.png differ diff --git a/docs/chapter_1_files/figure-html/unnamed-chunk-19-1.png b/docs/chapter_1_files/figure-html/unnamed-chunk-19-1.png index 94a034b..b08d52c 100644 Binary files a/docs/chapter_1_files/figure-html/unnamed-chunk-19-1.png and b/docs/chapter_1_files/figure-html/unnamed-chunk-19-1.png differ diff --git a/docs/chapter_2.html b/docs/chapter_2.html index ed0ba8f..daa0f14 100644 --- a/docs/chapter_2.html +++ b/docs/chapter_2.html @@ -355,15 +355,15 @@ So who’s religious?
1
-First we generate new a dataframe with sums per category and +First we generate new a dataframe with sums per category and
2
-…sort in descending order +…sort in descending order
3
-Then we add new column with percentages based on the sums you’ve just generated +Then we add new column with percentages based on the sums you’ve just generated
@@ -395,7 +395,25 @@ So who’s religious?

-

Add colours Use mutate to put “prefer not to say” at the bottom # Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels

+

I’ve added one feature to our chart that wasn’t in the bar charts in chapter 1, text labels with the actual value on each bar.

+

You may be thinking about the plots we’ve just finished in chapter 1 and wondering how they compare. Let’s use the same facet approach that we’ve just used to render this data in a subsetted way.

+
+
# First we need to add in data on ethnic self-identification from our respondents:
+df <- select(climate_experience_data, Q56, Q0)
+religious_affiliation_ethnicity <- as_tibble(as_factor(df))
+names(religious_affiliation_ethnicity) <- c("Religion", "Ethnicity")
+
+religious_affiliation_ethnicity_sums <- religious_affiliation_ethnicity %>%  
+  group_by(Ethnicity) %>%
+  dplyr::count(Religion, sort = TRUE) %>%
+  dplyr::mutate(Religion = forcats::fct_rev(forcats::fct_inorder(Religion)))
+
+plot1 <- ggplot(religious_affiliation_ethnicity_sums, aes(x = n, y = Religion)) +
+  geom_col(colour = "white") + facet_wrap(~Ethnicity, scales="free_x")
+
+ggsave("chart.png", plot=plot1, width = 8, height = 10, units=c("in"))
+
+

Use mutate to put “prefer not to say” at the bottom # Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels

6 Q56 follow-ups

diff --git a/docs/search.json b/docs/search.json index cd177b2..aa2099a 100644 --- a/docs/search.json +++ b/docs/search.json @@ -81,14 +81,14 @@ "href": "chapter_1.html#making-your-first-data-visulation-the-humble-bar-chart", "title": "2  The 2021 UK Census", "section": "2.4 Making your first data visulation: the humble bar chart", - "text": "2.4 Making your first data visulation: the humble bar chart\nWe’ve got a nice lean set of data, so now it’s time to visualise this. We’ll start by making a pie chart:\n\nuk_census_2021_religion_wmids <- uk_census_2021_religion_wmids %>% select(no_religion:no_response)\nuk_census_2021_religion_wmids <- gather(uk_census_2021_religion_wmids)\n\nThere are two basic ways to do visualisations in R. You can work with basic functions in R, often called “base R” or you can work with an alternative library called ggplot:\n\n2.4.1 Base R\n\ndf <- uk_census_2021_religion_wmids[order(uk_census_2021_religion_wmids$value,decreasing = TRUE),]\nbarplot(height=df$value, names=df$key)\n\n\n\n\n\n\n2.4.2 GGPlot\n\nggplot(uk_census_2021_religion_wmids, aes(x = key, y = value)) +\n geom_bar(stat = \"identity\")\n\n\n2\n\nWe’ll re-order the column by size.\n\n\n\n\n\n\n2ggplot(uk_census_2021_religion_wmids, aes(x= reorder(key,-value),value)) + geom_bar(stat =\"identity\")\n\n\n\n\nLet’s assume we’re working with a data set that doesn’t include a “totals” column and that we might want to get sums for each column. This is pretty easy to do in R:\n\n1uk_census_2021_religion_totals <- uk_census_2021_religion %>% select(no_religion:no_response)\nuk_census_2021_religion_totals <- uk_census_2021_religion_totals %>%\n2 summarise(across(everything(), ~ sum(., na.rm = TRUE)))\n3uk_census_2021_religion_totals <- gather(uk_census_2021_religion_totals)\n4ggplot(uk_census_2021_religion_totals, aes(x= reorder(key,-value),value)) + geom_bar(stat =\"identity\")\n\n\n1\n\nFirst, remove the column with region names and the totals for the regions as we want just integer data.\n\n2\n\nSecond calculate the totals. In this example we use the tidyverse library dplyr(), but you can also do this using base R with colsums() like this: uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE). The downside with base R is that you’ll also need to convert the result into a dataframe for ggplot like this: uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)\n\n3\n\nIn 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()\n\n4\n\nNow plot it out and have a look!\n\n\n\n\n\n\n\nYou might have noticed that these two dataframes give us somewhat different results. But with data science, it’s much more interesting to compare these two side-by-side in a visualisation. We can join these two dataframes and plot the bars side by side using bind() - which can be done by columns with cbind() and rows using rbind():\n\nuk_census_2021_religion_merged <- rbind(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)\n\nDo you notice there’s going to be a problem here? How can we tell one set from the other? We need to add in something idenfiable first! This isn’t too hard to do as we can simply create a new column for each with identifiable information before we bind them:\n\nuk_census_2021_religion_totals$dataset <- c(\"totals\")\nuk_census_2021_religion_wmids$dataset <- c(\"wmids\")\nuk_census_2021_religion_merged <- rbind(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)\n\nNow we’re ready to plot out our data as a grouped barplot:\n\nggplot(uk_census_2021_religion_merged, aes(fill=dataset, x= reorder(key,-value), value)) + geom_bar(position=\"dodge\", stat =\"identity\")\n\n\n\n\nIf you’re looking closely, you will notice that I’ve added two elements to our previous ggplot. I’ve asked ggplot to fill in the columns with reference to the dataset column we’ve just created. Then I’ve also asked ggplot to alter the position=\"dodge\" which places bars side by side rather than stacked on top of one another. You can give it a try without this instruction to see how this works. We will use stacked bars in a later chapter, so remember this feature.\nIf you inspect our chart, you can see that we’re getting closer, but it’s not really that helpful to compare the totals. What we need to do is get percentages that can be compared side by side. This is easy to do using another dplyr feature mutate:\n\nuk_census_2021_religion_totals <- uk_census_2021_religion_totals %>% \n dplyr::mutate(perc = scales::percent(value / sum(value), accuracy = 0.1, trim = FALSE))\nuk_census_2021_religion_wmids <- uk_census_2021_religion_wmids %>% \n dplyr::mutate(perc = scales::percent(value / sum(value), accuracy = 0.1, trim = FALSE))\nuk_census_2021_religion_merged <- rbind(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)\nggplot(uk_census_2021_religion_merged, aes(fill=dataset, x=key, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\")\n\n\n\n\nNow you can see a very rough comparison, which sets bars from the W Midlands data and overall data side by side for each category. The same principles that we’ve used here can be applied to draw in more data. You could, for example, compare census data from different years, e.g. 2001 2011 and 2021. Our use of dplyr::mutate above can be repeated to add an infinite number of further series’ which can be plotted in bar groups.\nWe’ll draw this data into comparison with later sets in the next chapter. But the one glaring issue which remains for our chart is that it’s lacking in really any aesthetic refinements. This is where ggplot really shines as a tool as you can add all sorts of things.\nThese are basically just added to our ggplot code. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the scale using scale_fill_brewer. This uses a particular tool (and a personal favourite of mine) called colorbrewer. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.\n\nggplot(uk_census_2021_religion_merged, aes(fill=dataset, x=key, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\") + scale_fill_brewer(palette = \"Set1\")\n\n\n\n\nWe might also want to add a border to our bars to make them more visually striking (notice the addition of color to the geom_bar below. I’ve also added reorder() to the x value to sort descending from the largest to smallest.\nYou can find more information about reordering ggplots on the R Graph gallery.\n\nuk_census_2021_religion_merged$dataset <- factor(uk_census_2021_religion_merged$dataset, levels = c('wmids', 'totals'))\nggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\")\n\n\n\n\nWe can fine tune a few other visual features here as well, like adding a title with ggtitle and a them with some prettier fonts with theme_ipsum() (which requires the hrbrthemes() library). We can also remove the x and y axis labels (not the data labels, which are rather important).\n\nggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the UK: 2021\") + xlab(\"\") + ylab(\"\")" + "text": "2.4 Making your first data visulation: the humble bar chart\nWe’ve got a nice lean set of data, so now it’s time to visualise this. We’ll start by making a pie chart:\n\nuk_census_2021_religion_wmids <- uk_census_2021_religion_wmids %>% select(no_religion:no_response)\nuk_census_2021_religion_wmids <- gather(uk_census_2021_religion_wmids)\n\nThere are two basic ways to do visualisations in R. You can work with basic functions in R, often called “base R” or you can work with an alternative library called ggplot:\n\n2.4.1 Base R\n\ndf <- uk_census_2021_religion_wmids[order(uk_census_2021_religion_wmids$value,decreasing = TRUE),]\nbarplot(height=df$value, names=df$key)\n\n\n\n\n\n\n2.4.2 GGPlot\n\nggplot(uk_census_2021_religion_wmids, aes(x = key, y = value)) +\n geom_bar(stat = \"identity\")\n\n\n2\n\nWe’ll re-order the column by size.\n\n\n\n\n\n\n2ggplot(uk_census_2021_religion_wmids, aes(x= reorder(key,-value),value)) + geom_bar(stat =\"identity\")\n\n\n\n\nLet’s assume we’re working with a data set that doesn’t include a “totals” column and that we might want to get sums for each column. This is pretty easy to do in R:\n\n1uk_census_2021_religion_totals <- uk_census_2021_religion %>% select(no_religion:no_response)\nuk_census_2021_religion_totals <- uk_census_2021_religion_totals %>%\n2 summarise(across(everything(), ~ sum(., na.rm = TRUE)))\n3uk_census_2021_religion_totals <- gather(uk_census_2021_religion_totals)\n4ggplot(uk_census_2021_religion_totals, aes(x= reorder(key,-value),value)) + geom_bar(stat =\"identity\")\n\n\n1\n\nFirst, remove the column with region names and the totals for the regions as we want just integer data.\n\n2\n\nSecond calculate the totals. In this example we use the tidyverse library dplyr(), but you can also do this using base R with colsums() like this: uk_census_2021_religion_totals <- colSums(uk_census_2021_religion_totals, na.rm = TRUE). The downside with base R is that you’ll also need to convert the result into a dataframe for ggplot like this: uk_census_2021_religion_totals <- as.data.frame(uk_census_2021_religion_totals)\n\n3\n\nIn 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()\n\n4\n\nNow plot it out and have a look!\n\n\n\n\n\n\n\nYou might have noticed that these two dataframes give us somewhat different results. But with data science, it’s much more interesting to compare these two side-by-side in a visualisation. We can join these two dataframes and plot the bars side by side using bind() - which can be done by columns with cbind() and rows using rbind():\n\nuk_census_2021_religion_merged <- rbind(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)\n\nDo you notice there’s going to be a problem here? How can we tell one set from the other? We need to add in something idenfiable first! This isn’t too hard to do as we can simply create a new column for each with identifiable information before we bind them:\n\nuk_census_2021_religion_totals$dataset <- c(\"totals\")\nuk_census_2021_religion_wmids$dataset <- c(\"wmids\")\nuk_census_2021_religion_merged <- rbind(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)\n\nNow we’re ready to plot out our data as a grouped barplot:\n\nggplot(uk_census_2021_religion_merged, aes(fill=dataset, x= reorder(key,-value), value)) + geom_bar(position=\"dodge\", stat =\"identity\")\n\n\n\n\nIf you’re looking closely, you will notice that I’ve added two elements to our previous ggplot. I’ve asked ggplot to fill in the columns with reference to the dataset column we’ve just created. Then I’ve also asked ggplot to alter the position=\"dodge\" which places bars side by side rather than stacked on top of one another. You can give it a try without this instruction to see how this works. We will use stacked bars in a later chapter, so remember this feature.\nIf you inspect our chart, you can see that we’re getting closer, but it’s not really that helpful to compare the totals. What we need to do is get percentages that can be compared side by side. This is easy to do using another dplyr feature mutate:\nIt’s worth noting that an alternative approach is to leave the numbers intact and simply label them differently so they render as percentages on your charts. You can do this with the `scales() library and the label_percent() function. The downside of this approach is that it won’t transfer to tables if you make them.\n\nuk_census_2021_religion_totals <- uk_census_2021_religion_totals %>% \n dplyr::mutate(perc = scales::percent(value / sum(value), accuracy = 0.1, trim = FALSE))\nuk_census_2021_religion_wmids <- uk_census_2021_religion_wmids %>% \n dplyr::mutate(perc = scales::percent(value / sum(value), accuracy = 0.1, trim = FALSE))\nuk_census_2021_religion_merged <- rbind(uk_census_2021_religion_totals, uk_census_2021_religion_wmids)\nggplot(uk_census_2021_religion_merged, aes(fill=dataset, x=key, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\")\n\n\n\n\nNow you can see a very rough comparison, which sets bars from the W Midlands data and overall data side by side for each category. The same principles that we’ve used here can be applied to draw in more data. You could, for example, compare census data from different years, e.g. 2001 2011 and 2021. Our use of dplyr::mutate above can be repeated to add an infinite number of further series’ which can be plotted in bar groups.\nWe’ll draw this data into comparison with later sets in the next chapter. But the one glaring issue which remains for our chart is that it’s lacking in really any aesthetic refinements. This is where ggplot really shines as a tool as you can add all sorts of things.\nThese are basically just added to our ggplot code. So, for example, let’s say we want to improve the colours used for our bars. You can specify the formatting for the fill on the scale using scale_fill_brewer. This uses a particular tool (and a personal favourite of mine) called colorbrewer. Part of my appreciation of this tool is that you can pick colours which are not just visually pleasing, and produce useful contrast / complementary schemes, but you can also work proactively to accommodate colourblindness. Working with colour schemes which can be divergent in a visually obvious way will be even more important when we work on geospatial data and maps in a later chapter.\n\nggplot(uk_census_2021_religion_merged, aes(fill=dataset, x=key, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\") + scale_fill_brewer(palette = \"Set1\")\n\n\n\n\nWe might also want to add a border to our bars to make them more visually striking (notice the addition of color to the geom_bar below. I’ve also added reorder() to the x value to sort descending from the largest to smallest.\nYou can find more information about reordering ggplots on the R Graph gallery.\n\nuk_census_2021_religion_merged$dataset <- factor(uk_census_2021_religion_merged$dataset, levels = c('wmids', 'totals'))\nggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\")\n\n\n\n\nWe can fine tune a few other visual features here as well, like adding a title with ggtitle and a them with some prettier fonts with theme_ipsum() (which requires the hrbrthemes() library). We can also remove the x and y axis labels (not the data labels, which are rather important).\n\nggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the UK: 2021\") + xlab(\"\") + ylab(\"\")" }, { "objectID": "chapter_1.html#is-your-chart-accurate-telling-the-truth-in-data-science", "href": "chapter_1.html#is-your-chart-accurate-telling-the-truth-in-data-science", "title": "2  The 2021 UK Census", "section": "2.5 Is your chart accurate? Telling the truth in data science", - "text": "2.5 Is your chart accurate? Telling the truth in data science\nThere is some technical work yet to be done fine-tuning the visualisation of our chart here. But I’d like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. It’s possible (though perhaps unlikely) that all those non-responses were Sikh respondents who felt uncomfortable identifying themselves on such a survey. If even half of the non-responses were of this nature, this would dramatically shift the results especially in comparison to other minority groups. So there is some work for us to do here in representing non-response as a category on the census. But it’s equally possible that someone might feel uncertain when answering, but nonetheless land on a particular decision marking “Christian” when they wondered if they should instead tick “no religion. Some surveys attempt to capture uncertainty in this way, asking respondents to mark how confident they are about their answers, but the census hasn’t capture this so we simply don’t know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This hasn’t been done on the census (which would have been quite hard work given that most of the instruments were printed hard copies!), so again, we can’t really be sure if those answers given are stable. Finally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.\nWhat does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive, almost completely population level, sample so there is additional validity there. The easiest way to represent all this reality in the form of speaking truthfully about our data is to acknowledge that however valid it may seem, it is nonetheless a snapshot. For this reason, I would always advise that the best title for a chart is one which specifies the data set. We should also probably do something different with those non-responses:\n\nggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\")\n\n\n\n\nChange orientation of X axis labels + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\nRelabel fields Simplify y-axis labels Add percentage text to bars (or maybe save for next chapter?)" + "text": "2.5 Is your chart accurate? Telling the truth in data science\nThere is some technical work yet to be done fine-tuning the visualisation of our chart here. But I’d like to pause for a moment and consider an ethical question. Is the title of this chart truthful and accurate? On one hand, it is a straight-forward reference to the nature of the question asked on the 2021 census survey instrument. However, as you will see in the next chapter, large data sets from the same year which asked a fairly similar question yield different results. Part of this could be attributed to the amount of non-respose to this specific question which, in the 2021 census is between 5-6% across many demographics. It’s possible (though perhaps unlikely) that all those non-responses were Sikh respondents who felt uncomfortable identifying themselves on such a survey. If even half of the non-responses were of this nature, this would dramatically shift the results especially in comparison to other minority groups. So there is some work for us to do here in representing non-response as a category on the census.\nIt’s equally possible that someone might feel uncertain when answering, but nonetheless land on a particular decision marking “Christian” when they wondered if they should instead tick “no religion. Some surveys attempt to capture uncertainty in this way, asking respondents to mark how confident they are about their answers, but the census hasn’t capture this so we simply don’t know. If a large portion of respondents in the”Christian” category were hovering between this and another response, again, they might shift their answers when responding on a different day, perhaps having just had a conversation with a friend which shifted their thinking. Even the inertia of survey design can have an effect on this, so responding to other questions in a particular way, thinking about ethnic identity, for example, can prime a person to think about their religious identity in a different or more focussed way, altering their response to the question. For this reason, some survey instruments randomise the order of questions. This hasn’t been done on the census (which would have been quite hard work given that most of the instruments were printed hard copies!), so again, we can’t really be sure if those answers given are stable.\nFinally, researchers have also found that when people are asked to mark their religious affiliation, sometimes they can prefer to mark more than one answer. A person might consider themselves to be “Muslim” but also “Spiritual but not religious” preferring the combination of those identities. It is also the case that respondents can identify with more unexpected hybrid religious identities, such as “Christian” and “Hindu”. The census only allows respondents to tick a single box for the religion category. It is worth noting that, in contrast, the responses for ethnicity allow for combinations. Given that this is the case, it’s impossible to know which way a person went at the fork in the road as they were forced to choose just one half of this kind of hybrid identity. Finally, it is interesting to wonder exactly what it means for a person when they tick a box like this. Is it because they attend synagogue on a weekly basis? Some persons would consider weekly attendance at workship a prerequisite for membership in a group, but others would not. Indeed we can infer from surveys and research which aims to track rates of participation in weekly worship that many people who tick boxes for particular religious identities on the census have never attended a worship service at all.\nWhat does this mean for our results? Are they completely unreliable and invalid? I don’t think this is the case or that taking a clear-eyed look at the force and stability of our underlying data should be cause for despair. Instead, the most appropriate response is humility. Someone has made a statement which is recorded in the census, of this we can be sure. They felt it to be an accurate response on some level based on the information they had at the time. And with regard to the census, it is a massive, almost completely population level, sample so there is additional validity there. The easiest way to represent all this reality in the form of speaking truthfully about our data is to acknowledge that however valid it may seem, it is nonetheless a snapshot. For this reason, I would always advise that the best title for a chart is one which specifies the data set. We should also probably do something different with those non-responses:\n\nggplot(uk_census_2021_religion_merged, aes(fill=fct_reorder(dataset, value), x=reorder(key,-value),value, y=perc)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\")\n\n\n\n\nChange orientation of X axis labels + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\nRelabel fields Simplify y-axis labels Add percentage text to bars (or maybe save for next chapter?)" }, { "objectID": "chapter_1.html#making-our-script-reproducible", @@ -102,14 +102,14 @@ "href": "chapter_1.html#multifactor-visualisation", "title": "2  The 2021 UK Census", "section": "2.7 Multifactor Visualisation", - "text": "2.7 Multifactor Visualisation\nOne element of R data analysis that can get really interesting is working with multiple variables. Above we’ve looked at the breakdown of religious affiliation across the whole of England and Wales (Scotland operates an independent census), and by placing this data alongside a specific region, we’ve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantative variable (also known as bivariate data) into the mix, however can also generate a lot more information and we have to think about visualising it in different ways which can still communicate with visual clarity in spite of the additional visual noise which is inevitable with enhanced complexity. Let’s have a look at the way that religion in England and Wales breaks down by ethnicity.\n\nlibrary(nomisr)\n\n# Process to explore nomis() data for specific datasets\nreligion_search <- nomis_search(name = \"*Religion*\")\nreligion_measures <- nomis_get_metadata(\"NM_529_1\", \"measures\")\ntibble::glimpse(religion_measures)\n\nRows: 2\nColumns: 3\n$ id <chr> \"20100\", \"20301\"\n$ label.en <chr> \"value\", \"percent\"\n$ description.en <chr> \"value\", \"percent\"\n\nreligion_geography <- nomis_get_metadata(\"NM_529_1\", \"geography\", \"TYPE\")\n\n# Get table of Census 2011 religion data from nomis\nz <- nomis_get_data(id = \"NM_529_1\", time = \"latest\", geography = \"TYPE499\", measures=c(20301))\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2011_religion <- filter(z, GEOGRAPHY_NAME==\"England and Wales\" & RURAL_URBAN_NAME==\"Total\" & C_RELPUK11_NAME != \"All categories: Religion\")\n# Drop unnecessary columns\nuk_census_2011_religion <- select(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)\n# Plot results\nplot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE)) + geom_bar(stat = \"identity\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\nggsave(filename = \"plot.png\", plot = plot1)\n\nSaving 7 x 5 in image\n\n# grab data from nomis for 2011 census religion / ethnicity table\nz1 <- nomis_get_data(id = \"NM_659_1\", time = \"latest\", geography = \"TYPE499\", measures=c(20100))\n# select relevant columns\nuk_census_2011_religion_ethnicitity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2011_religion_ethnicitity <- filter(uk_census_2011_religion_ethnicitity, GEOGRAPHY_NAME==\"England and Wales\" & C_RELPUK11_NAME != \"All categories: Religion\" & C_ETHPUK11_NAME != \"All categories: Ethnic group\")\n# Simplify data to only include general totals and omit subcategories\nuk_census_2011_religion_ethnicitity <- uk_census_2011_religion_ethnicitity %>% filter(grepl('Total', C_ETHPUK11_NAME))\n\nggplot(uk_census_2011_religion_ethnicitity, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nThe trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.\n\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2011_religion_ethnicitity_white <- filter(uk_census_2011_religion_ethnicitity, C_ETHPUK11_NAME == \"White: Total\")\nuk_census_2011_religion_ethnicitity_nonwhite <- filter(uk_census_2011_religion_ethnicitity, C_ETHPUK11_NAME != \"White: Total\")\n\nggplot(uk_census_2011_religion_ethnicitity_nonwhite, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nThis still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.\n\nggplot(uk_census_2011_religion_ethnicitity_nonwhite, aes(x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + facet_wrap(~C_ETHPUK11_NAME, ncol = 2) + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2011 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))" + "text": "2.7 Multifactor Visualisation\nOne element of R data analysis that can get really interesting is working with multiple variables. Above we’ve looked at the breakdown of religious affiliation across the whole of England and Wales (Scotland operates an independent census), and by placing this data alongside a specific region, we’ve already made a basic entry into working with multiple variables but this can get much more interesting. Adding an additional quantative variable (also known as bivariate data) into the mix, however can also generate a lot more information and we have to think about visualising it in different ways which can still communicate with visual clarity in spite of the additional visual noise which is inevitable with enhanced complexity. Let’s have a look at the way that religion in England and Wales breaks down by ethnicity.\n\n\n\n\n\n\nWhat is Nomis?\n\n\n\nFor the UK, census data is made available for programmatic research like this via an organisation called NOMIS. Luckily for us, there is an R library you can use to access nomis directly which greatly simplifies the process of pulling data down from the platform. It’s worth noting that if you’re not in the UK, there are similar options for other countries. Nearly every R textbook I’ve ever seen works with USA census data, so you’ll find plenty of documentation available on the tools you can use for US Census data. Similarly for the EU, Canada, Austrailia etc.\nHere’s the process to identify a dataset within the nomis platform:\n\n# Process to explore nomis() data for specific datasets\nlibrary(nomisr)\n# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:\n#religion_search <- nomis_search(name = \"*Religion*\")\n#religion_measures <- nomis_get_metadata(\"ST104\", \"measures\")\n#tibble::glimpse(religion_measures)\n#religion_geography <- nomis_get_metadata(\"NM_529_1\", \"geography\", \"TYPE\")\n\n\n\n\nlibrary(nomisr)\n# Get table of Census 2011 religion data from nomis\n# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:\n#z <- nomis_get_data(id = \"NM_529_1\", time = \"latest\", geography = \"TYPE499\", measures=c(20301))\n#saveRDS(z, file = \"z.rds\")\nz <- readRDS(file = (here(\"example_data\", \"z.rds\")))\n\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2011_religion <- filter(z, GEOGRAPHY_NAME==\"England and Wales\" & RURAL_URBAN_NAME==\"Total\" & C_RELPUK11_NAME != \"All categories: Religion\")\n# Drop unnecessary columns\nuk_census_2011_religion <- select(uk_census_2011_religion, C_RELPUK11_NAME, OBS_VALUE)\n# Plot results\nplot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE)) + geom_bar(stat = \"identity\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n# ggsave(filename = \"plot.png\", plot = plot1)\n\n# grab daata from nomis for 2001 census religion / ethnicity\n\n# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided:\n#z0 <- nomis_get_data(id = \"NM_1872_1\", time = \"latest\", geography = \"TYPE499\", measures=c(20100))\n#saveRDS(z0, file = \"z0.rds\")\nz0 <- readRDS(file = (here(\"example_data\", \"z0.rds\")))\n\n# select relevant columns\nuk_census_2001_religion_ethnicity <- select(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE)\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2001_religion_ethnicity <- filter(uk_census_2001_religion_ethnicity, GEOGRAPHY_NAME==\"England and Wales\" & C_RELPUK11_NAME != \"All categories: Religion\")\n# Simplify data to only include general totals and omit subcategories\nuk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% filter(grepl('Total', C_ETHHUK11_NAME))\n\n# grab data from nomis for 2011 census religion / ethnicity table\n# commenting out nomis_get temporarily until I can get renv working properly here\n#z1 <- nomis_get_data(id = \"NM_659_1\", time = \"latest\", geography = \"TYPE499\", measures=c(20100))\n#saveRDS(z1, file = \"z1.rds\")\nz1 <- readRDS(file = (here(\"example_data\", \"z1.rds\")))\n\n# select relevant columns\nuk_census_2011_religion_ethnicity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE)\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2011_religion_ethnicity <- filter(uk_census_2011_religion_ethnicity, GEOGRAPHY_NAME==\"England and Wales\" & C_RELPUK11_NAME != \"All categories: Religion\" & C_ETHPUK11_NAME != \"All categories: Ethnic group\")\n# Simplify data to only include general totals and omit subcategories\nuk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% filter(grepl('Total', C_ETHPUK11_NAME))\n\n# grab data from nomis for 2021 census religion / ethnicity table\n#z2 <- nomis_get_data(id = \"NM_2131_1\", time = \"latest\", geography = \"TYPE499\", measures=c(20100))\n#saveRDS(z2, file = \"z2.rds\")\nz2 <- readRDS(file = (here(\"example_data\", \"z2.rds\")))\n\n# select relevant columns\nuk_census_2021_religion_ethnicity <- select(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE)\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2021_religion_ethnicity <- filter(uk_census_2021_religion_ethnicity, GEOGRAPHY_NAME==\"England and Wales\" & C2021_RELIGION_10_NAME != \"Total\" & C2021_ETH_8_NAME != \"Total\")\n# 2021 census includes white sub-groups so we need to omit those so we just have totals:\nuk_census_2021_religion_ethnicity <- filter(uk_census_2021_religion_ethnicity, C2021_ETH_8_NAME != \"White: English, Welsh, Scottish, Northern Irish or British\" & C2021_ETH_8_NAME != \"White: Irish\" & C2021_ETH_8_NAME != \"White: Gypsy or Irish Traveller, Roma or Other White\")\n\nggplot(uk_census_2011_religion_ethnicity, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nThe trouble with using grouped bars here, as you can see, is that there are quite sharp disparities which make it hard to compare in meaningful ways. We could use logarithmic rather than linear scaling as an option, but this is hard for many general public audiences to apprecaite without guidance. One alternative quick fix is to extract data from “white” respondents which can then be placed in a separate chart with a different scale.\n\n# Filter down to simplified dataset with England / Wales and percentages without totals\nuk_census_2011_religion_ethnicity_white <- filter(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME == \"White: Total\")\nuk_census_2011_religion_ethnicity_nonwhite <- filter(uk_census_2011_religion_ethnicity, C_ETHPUK11_NAME != \"White: Total\")\n\nggplot(uk_census_2011_religion_ethnicity_nonwhite, aes(fill=C_ETHPUK11_NAME, x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nThis still doesn’t quite render with as much visual clarity and communication as I’d like. For a better look, we can use a technique in R called “faceting” to create a series of small charts which can be viewed alongside one another.\n\nggplot(uk_census_2011_religion_ethnicity_nonwhite, aes(x=C_RELPUK11_NAME, y=OBS_VALUE)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + facet_wrap(~C_ETHPUK11_NAME, ncol = 2) + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2011 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nFor our finale chart, I’d like to take the faceted chart we’ve just done, and add in totals for the previous two census years (2001 and 2011) so we can see how trends are changing in terms of religious affiliation within ethnic self-identification categories. We’ll draw on some techniques we’re already developed above using rbind() to connect up each of these charts (after we’ve added a column identifying each chart by the census year). We will also need to use one new technique to change the wording of ethnic categories as this isn’t consistent from one census to the next and ggplot will struggle to chart things if the terms being used are exactly the same. We’ll use mutate() again to accomplish this with some slightly different code.\n\n# First add column to each dataframe so we don't lose track of the census it comes from:\nuk_census_2001_religion_ethnicity$dataset <- c(\"2001\")\nuk_census_2011_religion_ethnicity$dataset <- c(\"2011\")\nuk_census_2021_religion_ethnicity$dataset <- c(\"2021\")\n\n# Let's tidy the names of each column:\n\nnames(uk_census_2001_religion_ethnicity) <- c(\"Geography\", \"Religion\", \"Ethnicity\", \"Value\", \"Year\")\nnames(uk_census_2011_religion_ethnicity) <- c(\"Geography\", \"Religion\", \"Ethnicity\", \"Value\", \"Year\")\nnames(uk_census_2021_religion_ethnicity) <- c(\"Geography\", \"Religion\", \"Ethnicity\", \"Value\", \"Year\")\n\n# Next we need to change the terms using mutate()\nuk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% \n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^White: Total$\", replacement = \"White\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Mixed: Total$\", replacement = \"Mixed\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Asian: Total$\", replacement = \"Asian\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Black or Black British: Total$\", replacement = \"Black\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Chinese or Other ethnic group: Total$\", replacement = \"Other\"))\n \nuk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% \n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^White: Total$\", replacement = \"White\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Mixed/multiple ethnic group: Total$\", replacement = \"Mixed\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Asian/Asian British: Total$\", replacement = \"Asian\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Black/African/Caribbean/Black British: Total$\", replacement = \"Black\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Other ethnic group: Total$\", replacement = \"Other\"))\n\nuk_census_2021_religion_ethnicity <- uk_census_2021_religion_ethnicity %>% \n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^White: Total$\", replacement = \"White\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Mixed or Multiple ethnic groups$\", replacement = \"Mixed\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Asian, Asian British or Asian Welsh$\", replacement = \"Asian\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Black, Black British, Black Welsh, Caribbean or African$\", replacement = \"Black\")) %>%\n mutate(Ethnicity = str_replace_all(Ethnicity, \n pattern = \"^Other ethnic group$\", replacement = \"Other\"))\n\n# Now let's merge the tables:\n\nuk_census_merged_religion_ethnicity <- rbind(uk_census_2021_religion_ethnicity, uk_census_2011_religion_ethnicity)\n\nuk_census_merged_religion_ethnicity <- rbind(uk_census_merged_religion_ethnicity, uk_census_2001_religion_ethnicity)\n\n# As above, we'll split out non-white and white:\n\nuk_census_merged_religion_ethnicity_nonwhite <- filter(uk_census_merged_religion_ethnicity, Ethnicity != \"White\")\n\n# Time to plot!\n\nggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion, y=Value)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + facet_wrap(~Ethnicity, ncol = 2) + scale_fill_brewer(palette = \"Set1\") + ggtitle(\"Religious Affiliation in the 2001-2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nThere are a few formatting issues which remain. Our y-axis number labels are in scientific format which isn’t really very easy to read. You can use the very powerful and flexible scales() library to bring in some more readable formatting of numbers in a variety of places in R including in ggplot visualizations.\n\nlibrary(scales)\n\n\nAttaching package: 'scales'\n\n\nThe following object is masked from 'package:purrr':\n\n discard\n\n\nThe following object is masked from 'package:readr':\n\n col_factor\n\nggplot(uk_census_merged_religion_ethnicity_nonwhite, aes(fill=Year, x=Religion, y=Value)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + facet_wrap(~Ethnicity, ncol = 2) + scale_fill_brewer(palette = \"Set1\") + scale_y_continuous(labels = unit_format(unit = \"M\", scale = 1e-6), breaks = breaks_extended(8)) + ggtitle(\"Religious Affiliation in the 2001-2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n# https://ggplot2-book.org/scales-position#sec-position-continuous-breaks\n\nThis chart shows an increase in almost every category, though it’s a bit hard to read in some cases. However, this information is based on the increase in raw numbers. It’s possbile that numbers may be going up, but in some cases the percentage share for a particular category has actually gone down. Let’s transform and visualise our data as percentages to see what kind of trends we can actually isolate:\n\nuk_census_merged_religion_ethnicity <- uk_census_merged_religion_ethnicity %>%\n group_by(Ethnicity, Year) %>%\n dplyr::mutate(Percent = Value/sum(Value))\n\nggplot(uk_census_merged_religion_ethnicity, aes(fill=Year, x=Religion, y=Percent)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + facet_wrap(~Ethnicity, scales=\"free_x\") + scale_fill_brewer(palette = \"Set1\") + scale_y_continuous(labels = scales::percent) + ggtitle(\"Religious Affiliation in the 2001-2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\n\n\n\nNow you can see why this shift is important - the visualisation tells a completely different story in some cases across the two different charts. In the first, working off raw numbers we see a net increase in Christianity across all categories. But if we take into account the fact that the overall share of population is growing for each of these groups, their actual composition is changing in a different direction. The proportion of each group is declining across the three census periods (albeit with an exception for the “Other” category from 2011 to 2021).\nTo highlight a few features of this final plot, I’ve used a specific feature within facet_wrap scales = \"free_x\" to let each of the individual facets adjust the total range on the x-axis. Since we’re looking at trends here and not absolute values, having correspondence across scales isn’t important and this makes for something a bit more visually tidy. I’ve also shifted the code for scale_y_continuous to render values as percentages (rather than millions).\nIn case you want to print this plot out and hang it on your wall, you can use the ggsave tool to render the chart as an image file:\n\nplot1 <- ggplot(uk_census_merged_religion_ethnicity, aes(fill=Year, x=Religion, y=Percent)) + geom_bar(position=\"dodge\", stat =\"identity\", colour = \"black\") + facet_wrap(~Ethnicity, scales=\"free_x\") + scale_fill_brewer(palette = \"Set1\") + scale_y_continuous(labels = scales::percent) + ggtitle(\"Religious Affiliation in the 2001-2021 Census of England and Wales\") + xlab(\"\") + ylab(\"\") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))\n\nggsave(\"chart.png\", plot=plot1, width = 8, height = 10, units=c(\"in\"))" }, { "objectID": "chapter_2.html", "href": "chapter_2.html", "title": "3  Survey Data: Spotlight Project", "section": "", - "text": "4 Loading in some data\n# R Setup -----------------------------------------------------------------\nsetwd(\"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion\")\nlibrary(here)\n\nhere() starts at /Users/kidwellj/gits/hacking_religion_textbook\n\nlibrary(tidyverse)\n\n-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --\nv dplyr 1.1.3 v readr 2.1.4\nv forcats 1.0.0 v stringr 1.5.0\nv ggplot2 3.4.3 v tibble 3.2.1\nv lubridate 1.9.3 v tidyr 1.3.0\nv purrr 1.0.2 \n\n\n-- Conflicts ------------------------------------------ tidyverse_conflicts() --\nx dplyr::filter() masks stats::filter()\nx dplyr::lag() masks stats::lag()\ni Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors\n\nlibrary(haven) # used for importing SPSS .sav files\nhere::i_am(\"chapter_2.qmd\")\n\nhere() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion\n\nclimate_experience_data <- read_sav(here(\"example_data\", \"climate_experience_data.sav\"))\nThe first thing to note here is that we’ve drawn in a different type of data file, this time from an .sav file, usully produced by the statistics software package SPSS. This uses a different R Library (I use haven for this). The upside is that in some cases where you have survey data with both a code and a value like “1” is eqivalent to “very much agree” this will preserve both in the R dataframe that is created. Now that you’ve loaded in data, you have a new R dataframe called “climate_experience_data” with a lot of columns with just under 1000 survey responses.\nOne 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:\nThis 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.\nWe 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).\nWe 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?”\nEach 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.\nYou’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.\nLet’s dive into the data and see how this all works out. We’ll start with the question 56 data, around religious affiliation:\nreligious_affiliation <- as_tibble(as_factor(climate_experience_data$Q56))\nnames(religious_affiliation) <- c(\"response\")\nreligious_affiliation <- filter(religious_affiliation, !is.na(response))\nThere are few things we need to do here to get the data into initial proper shape. This might be called “cleaning” the data:\nIf we pause at this point to view the data, you’ll see it’s basically just a long list of survey responses. What we need is a count of each unique response (or factor). This will take a few more steps:\nreligious_affiliation_sums <- religious_affiliation %>% \n1 dplyr::count(response, sort = TRUE) %>%\n2 dplyr::mutate(response = forcats::fct_rev(forcats::fct_inorder(response)))\nreligious_affiliation_sums <- religious_affiliation_sums %>% \n3 dplyr::mutate(perc = scales::percent(n / sum(n), accuracy = .1, trim = FALSE))\n\n\n1\n\nFirst we generate new a dataframe with sums per category and\n\n2\n\n…sort in descending order\n\n3\n\nThen we add new column with percentages based on the sums you’ve just generated\nThat should give us a tidy table of results, which you can see if you view the contents of our new religious_affiliation_sums dataframe:\nhead(religious_affiliation_sums)\n\n# A tibble: 6 x 3\n response n perc \n <fct> <int> <chr> \n1 Christian 342 \"33.9%\"\n2 Muslim 271 \"26.9%\"\n3 No religion 108 \"10.7%\"\n4 Hindu 72 \" 7.1%\"\n5 Atheist 54 \" 5.4%\"\n6 Spiritual but not religious 38 \" 3.8%\"\n# make plot\nggplot(religious_affiliation_sums, aes(x = n, y = response)) +\n geom_col(colour = \"white\") + \n ## add percentage labels\n geom_text(aes(label = perc),\n ## make labels left-aligned and white\n hjust = 1, nudge_x = -.5, colour = \"white\", size=3)\nAdd colours Use mutate to put “prefer not to say” at the bottom # Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels\ncaption <- “Christian Denomination” # TODO: copy plot above for Q56 to add two additional plots using climate_experience_data_named\\(Q56b and climate_experience_data_named\\)Q56c # Religious Affiliation b - Christian Denomination Subquestion christian_denomination <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q56b) christian_denomination_table <- chart_single_result_flextable(climate_experience_data_named\\)Q56b, desc(Count)) christian_denomination_table save_as_docx(christian_denomination_table, path = “./figures/q56_religious_affiliation_xn_denomination.docx”)\nchristian_denomination_hi <- filter(climate_experience_data_named, Q56 == “Christian”, Q57_bin == “high”) christian_denomination_hi <- qualtrics_process_single_multiple_choice(christian_denomination_hi$Q56b) christian_denomination_hi\ncaption <- “Islamic Identity” # Should the label be different than income since the data examined is the Affiliation? # TODO: adjust plot to factor using numbered responses on this question (perhaps also above) religious_affiliationc <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q56c) religious_affiliationc_plot <- plot_horizontal_bar(religious_affiliationc) religious_affiliationc_plot <- religious_affiliationc_plot + labs(caption = caption, x = \"\", y = \"\") religious_affiliationc_plot ggsave(\"figures/q56c_religious_affiliation.png\", width = 20, height = 10, units = \"cm\") religious_affiliationc_table <- chart_single_result_flextable(climate_experience_data_named\\)Q56c, Count) religious_affiliationc_table save_as_docx(religious_affiliationc_table, path = “./figures/q56_religious_affiliation_islam.docx”)\ncaption <- “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”)\ncaption <- “Respondent Attendance of Religious Services” religious_service_attend <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q58) religious_service_attend_plot <- plot_horizontal_bar(religious_service_attend) religious_service_attend_plot <- religious_service_attend_plot + labs(title = caption, x = \"\", y = \"\") religious_service_attend_plot ggsave(\"figures/q58_religious_service_attend.png\", width = 20, height = 10, units = \"cm\") religious_service_attend_table <- chart_single_result_flextable(climate_experience_data_named\\)Q58, Count) religious_service_attend_table save_as_docx(religious_service_attend_table, path = “./figures/q58_religious_service_attend.docx”)\ndf <- 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”)\ncaption <- “Respondent Prayer Outside of Religious Services” prayer <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q59) prayer_plot <- plot_horizontal_bar(prayer) prayer_plot <- prayer_plot + labs(caption = caption, x = \"\", y = \"\") prayer_plot ggsave(\"figures/q59_prayer.png\", width = 20, height = 10, units = \"cm\") prayer_table <- chart_single_result_flextable(climate_experience_data_named\\)Q59, Count) prayer_table save_as_docx(prayer_table, path = “./figures/q59_prayer.docx”)\ndf <- select(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q59) names(df) <- c(“Q52_bin”, “Q53_bin”, “Q57_bin”, “response”) facet_names <- c(Q52_bin = “Spirituality”, Q53_bin = “Politics L/R”, Q57_bin = “Religiosity”, low=“low”, medium=“medium”, high=“high”) facet_labeller <- function(variable,value){return(facet_names[value])} df\\(response <- factor(df\\)response, ordered = TRUE, levels = c(“1”, “2”, “3”, “4”, “5”)) df\\(response <- fct_recode(df\\)response, “More than once a week” = “1”, “Once a week” = “2”, “At least once a month” = “3”, “Only on special holy days” = “4”, “Never” = “5”) df %>% # we need to get the data including facet info in long format, so we use pivot_longer() pivot_longer(!response, names_to = “bin_name”, values_to = “b”) %>% # add counts for plot below count(response, bin_name, b) %>% group_by(bin_name,b) %>% mutate(perc=paste0(round(n*100/sum(n),1),“%”)) %>% # run ggplot ggplot(aes(x = n, y = ““, fill = response)) + geom_col(position=position_fill(), aes(fill=response)) + geom_text(aes(label = perc), position = position_fill(vjust=.5), size=2) + scale_fill_brewer(palette =”Dark2”, type = “qual”) + scale_x_continuous(labels = scales::percent_format()) + facet_grid(vars(b), vars(bin_name), labeller=as_labeller(facet_names)) + labs(caption = caption, x = ““, y =”“) + guides(fill = guide_legend(title = NULL)) ggsave(”figures/q59_faceted.png”, width = 30, height = 10, units = “cm”)\nq6_data <- qualtrics_process_single_multiple_choice_unsorted_streamlined(climate_experience_data$Q6)\ntitle <- “Do you think the climate is changing?”\nlevel_order <- c(“Don<80><99>t know”, “Definitely not changing”, “Probably not changing”, “Probably changing”, “Definitely changing”) ## code if a specific palette is needed for matching fill = wheel(ochre, num = as.integer(count(q6_data[1]))) # make plot q6_data_plot <- ggplot(q6_data, aes(x = n, y = response, fill = fill)) + geom_col(colour = “white”) + ## add percentage labels geom_text(aes(label = perc), ## make labels left-aligned and white hjust = 1, colour = “black”, size=4) + # use nudge_x = 30, to shift position ## reduce spacing between labels and bars scale_fill_identity(guide = “none”) + ## get rid of all elements except y axis labels + adjust plot margin theme_ipsum_rc() + theme(plot.margin = margin(rep(15, 4))) + easy_center_title() + # with thanks for helpful info on doing wrap here: https://stackoverflow.com/questions/21878974/wrap-long-axis-labels-via-labeller-label-wrap-in-ggplot2 scale_y_discrete(labels = wrap_format(30), limits = level_order) + theme(plot.title = element_text(size =18, hjust = 0.5), axis.text.y = element_text(size =16)) + labs(title = title, x = ““, y =”“)\nq6_data_plot\nggsave(“figures/q6.png”, width = 18, height = 12, units = “cm”)\nclimate_experience_data$Q51_score <- rowMeans(select(climate_experience_data, Q51_remote_vacation:Q51_heritage))\nclimate_experience_data <- climate_experience_data %>% mutate( Q51_bin = case_when( Q51_score > mean(Q51_score) + sd(Q51_score) ~ “high”, Q51_score < mean(Q51_score) - sd(Q51_score) ~ “low”, TRUE ~ “medium” ) %>% factor(levels = c(“low”, “medium”, “high”)) )\nclimate_experience_data$Q52_score <- rowMeans(select(climate_experience_data, Q52a_1:Q52f_1))\nclimate_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”)) )" + "text": "4 Loading in some data\n# R Setup -----------------------------------------------------------------\nsetwd(\"/Users/kidwellj/gits/hacking_religion_textbook/hacking_religion\")\nlibrary(here)\n\nhere() starts at /Users/kidwellj/gits/hacking_religion_textbook\n\nlibrary(tidyverse)\n\n-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --\nv dplyr 1.1.3 v readr 2.1.4\nv forcats 1.0.0 v stringr 1.5.0\nv ggplot2 3.4.3 v tibble 3.2.1\nv lubridate 1.9.3 v tidyr 1.3.0\nv purrr 1.0.2 \n\n\n-- Conflicts ------------------------------------------ tidyverse_conflicts() --\nx dplyr::filter() masks stats::filter()\nx dplyr::lag() masks stats::lag()\ni Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors\n\nlibrary(haven) # used for importing SPSS .sav files\nhere::i_am(\"chapter_2.qmd\")\n\nhere() starts at /Users/kidwellj/gits/hacking_religion_textbook/hacking_religion\n\nclimate_experience_data <- read_sav(here(\"example_data\", \"climate_experience_data.sav\"))\nThe first thing to note here is that we’ve drawn in a different type of data file, this time from an .sav file, usully produced by the statistics software package SPSS. This uses a different R Library (I use haven for this). The upside is that in some cases where you have survey data with both a code and a value like “1” is eqivalent to “very much agree” this will preserve both in the R dataframe that is created. Now that you’ve loaded in data, you have a new R dataframe called “climate_experience_data” with a lot of columns with just under 1000 survey responses.\nOne 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:\nThis 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.\nWe 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).\nWe 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?”\nEach 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.\nYou’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.\nLet’s dive into the data and see how this all works out. We’ll start with the question 56 data, around religious affiliation:\nreligious_affiliation <- as_tibble(as_factor(climate_experience_data$Q56))\nnames(religious_affiliation) <- c(\"response\")\nreligious_affiliation <- filter(religious_affiliation, !is.na(response))\nThere are few things we need to do here to get the data into initial proper shape. This might be called “cleaning” the data:\nIf we pause at this point to view the data, you’ll see it’s basically just a long list of survey responses. What we need is a count of each unique response (or factor). This will take a few more steps:\nreligious_affiliation_sums <- religious_affiliation %>% \n1 dplyr::count(response, sort = TRUE) %>%\n2 dplyr::mutate(response = forcats::fct_rev(forcats::fct_inorder(response)))\nreligious_affiliation_sums <- religious_affiliation_sums %>% \n3 dplyr::mutate(perc = scales::percent(n / sum(n), accuracy = .1, trim = FALSE))\n\n\n1\n\nFirst we generate new a dataframe with sums per category and\n\n2\n\n…sort in descending order\n\n3\n\nThen we add new column with percentages based on the sums you’ve just generated\nThat should give us a tidy table of results, which you can see if you view the contents of our new religious_affiliation_sums dataframe:\nhead(religious_affiliation_sums)\n\n# A tibble: 6 x 3\n response n perc \n <fct> <int> <chr> \n1 Christian 342 \"33.9%\"\n2 Muslim 271 \"26.9%\"\n3 No religion 108 \"10.7%\"\n4 Hindu 72 \" 7.1%\"\n5 Atheist 54 \" 5.4%\"\n6 Spiritual but not religious 38 \" 3.8%\"\n# make plot\nggplot(religious_affiliation_sums, aes(x = n, y = response)) +\n geom_col(colour = \"white\") + \n ## add percentage labels\n geom_text(aes(label = perc),\n ## make labels left-aligned and white\n hjust = 1, nudge_x = -.5, colour = \"white\", size=3)\nI’ve added one feature to our chart that wasn’t in the bar charts in chapter 1, text labels with the actual value on each bar.\nYou may be thinking about the plots we’ve just finished in chapter 1 and wondering how they compare. Let’s use the same facet approach that we’ve just used to render this data in a subsetted way.\n# First we need to add in data on ethnic self-identification from our respondents:\ndf <- select(climate_experience_data, Q56, Q0)\nreligious_affiliation_ethnicity <- as_tibble(as_factor(df))\nnames(religious_affiliation_ethnicity) <- c(\"Religion\", \"Ethnicity\")\n\nreligious_affiliation_ethnicity_sums <- religious_affiliation_ethnicity %>% \n group_by(Ethnicity) %>%\n dplyr::count(Religion, sort = TRUE) %>%\n dplyr::mutate(Religion = forcats::fct_rev(forcats::fct_inorder(Religion)))\n\nplot1 <- ggplot(religious_affiliation_ethnicity_sums, aes(x = n, y = Religion)) +\n geom_col(colour = \"white\") + facet_wrap(~Ethnicity, scales=\"free_x\")\n\nggsave(\"chart.png\", plot=plot1, width = 8, height = 10, units=c(\"in\"))\nUse mutate to put “prefer not to say” at the bottom # Info here: https://r4ds.had.co.nz/factors.html#modifying-factor-levels\ncaption <- “Christian Denomination” # TODO: copy plot above for Q56 to add two additional plots using climate_experience_data_named\\(Q56b and climate_experience_data_named\\)Q56c # Religious Affiliation b - Christian Denomination Subquestion christian_denomination <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q56b) christian_denomination_table <- chart_single_result_flextable(climate_experience_data_named\\)Q56b, desc(Count)) christian_denomination_table save_as_docx(christian_denomination_table, path = “./figures/q56_religious_affiliation_xn_denomination.docx”)\nchristian_denomination_hi <- filter(climate_experience_data_named, Q56 == “Christian”, Q57_bin == “high”) christian_denomination_hi <- qualtrics_process_single_multiple_choice(christian_denomination_hi$Q56b) christian_denomination_hi\ncaption <- “Islamic Identity” # Should the label be different than income since the data examined is the Affiliation? # TODO: adjust plot to factor using numbered responses on this question (perhaps also above) religious_affiliationc <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q56c) religious_affiliationc_plot <- plot_horizontal_bar(religious_affiliationc) religious_affiliationc_plot <- religious_affiliationc_plot + labs(caption = caption, x = \"\", y = \"\") religious_affiliationc_plot ggsave(\"figures/q56c_religious_affiliation.png\", width = 20, height = 10, units = \"cm\") religious_affiliationc_table <- chart_single_result_flextable(climate_experience_data_named\\)Q56c, Count) religious_affiliationc_table save_as_docx(religious_affiliationc_table, path = “./figures/q56_religious_affiliation_islam.docx”)\ncaption <- “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”)\ncaption <- “Respondent Attendance of Religious Services” religious_service_attend <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q58) religious_service_attend_plot <- plot_horizontal_bar(religious_service_attend) religious_service_attend_plot <- religious_service_attend_plot + labs(title = caption, x = \"\", y = \"\") religious_service_attend_plot ggsave(\"figures/q58_religious_service_attend.png\", width = 20, height = 10, units = \"cm\") religious_service_attend_table <- chart_single_result_flextable(climate_experience_data_named\\)Q58, Count) religious_service_attend_table save_as_docx(religious_service_attend_table, path = “./figures/q58_religious_service_attend.docx”)\ndf <- 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”)\ncaption <- “Respondent Prayer Outside of Religious Services” prayer <- qualtrics_process_single_multiple_choice(climate_experience_data_named\\(Q59) prayer_plot <- plot_horizontal_bar(prayer) prayer_plot <- prayer_plot + labs(caption = caption, x = \"\", y = \"\") prayer_plot ggsave(\"figures/q59_prayer.png\", width = 20, height = 10, units = \"cm\") prayer_table <- chart_single_result_flextable(climate_experience_data_named\\)Q59, Count) prayer_table save_as_docx(prayer_table, path = “./figures/q59_prayer.docx”)\ndf <- select(climate_experience_data, Q52_bin, Q53_bin, Q57_bin, Q59) names(df) <- c(“Q52_bin”, “Q53_bin”, “Q57_bin”, “response”) facet_names <- c(Q52_bin = “Spirituality”, Q53_bin = “Politics L/R”, Q57_bin = “Religiosity”, low=“low”, medium=“medium”, high=“high”) facet_labeller <- function(variable,value){return(facet_names[value])} df\\(response <- factor(df\\)response, ordered = TRUE, levels = c(“1”, “2”, “3”, “4”, “5”)) df\\(response <- fct_recode(df\\)response, “More than once a week” = “1”, “Once a week” = “2”, “At least once a month” = “3”, “Only on special holy days” = “4”, “Never” = “5”) df %>% # we need to get the data including facet info in long format, so we use pivot_longer() pivot_longer(!response, names_to = “bin_name”, values_to = “b”) %>% # add counts for plot below count(response, bin_name, b) %>% group_by(bin_name,b) %>% mutate(perc=paste0(round(n*100/sum(n),1),“%”)) %>% # run ggplot ggplot(aes(x = n, y = ““, fill = response)) + geom_col(position=position_fill(), aes(fill=response)) + geom_text(aes(label = perc), position = position_fill(vjust=.5), size=2) + scale_fill_brewer(palette =”Dark2”, type = “qual”) + scale_x_continuous(labels = scales::percent_format()) + facet_grid(vars(b), vars(bin_name), labeller=as_labeller(facet_names)) + labs(caption = caption, x = ““, y =”“) + guides(fill = guide_legend(title = NULL)) ggsave(”figures/q59_faceted.png”, width = 30, height = 10, units = “cm”)\nq6_data <- qualtrics_process_single_multiple_choice_unsorted_streamlined(climate_experience_data$Q6)\ntitle <- “Do you think the climate is changing?”\nlevel_order <- c(“Don<80><99>t know”, “Definitely not changing”, “Probably not changing”, “Probably changing”, “Definitely changing”) ## code if a specific palette is needed for matching fill = wheel(ochre, num = as.integer(count(q6_data[1]))) # make plot q6_data_plot <- ggplot(q6_data, aes(x = n, y = response, fill = fill)) + geom_col(colour = “white”) + ## add percentage labels geom_text(aes(label = perc), ## make labels left-aligned and white hjust = 1, colour = “black”, size=4) + # use nudge_x = 30, to shift position ## reduce spacing between labels and bars scale_fill_identity(guide = “none”) + ## get rid of all elements except y axis labels + adjust plot margin theme_ipsum_rc() + theme(plot.margin = margin(rep(15, 4))) + easy_center_title() + # with thanks for helpful info on doing wrap here: https://stackoverflow.com/questions/21878974/wrap-long-axis-labels-via-labeller-label-wrap-in-ggplot2 scale_y_discrete(labels = wrap_format(30), limits = level_order) + theme(plot.title = element_text(size =18, hjust = 0.5), axis.text.y = element_text(size =16)) + labs(title = title, x = ““, y =”“)\nq6_data_plot\nggsave(“figures/q6.png”, width = 18, height = 12, units = “cm”)\nclimate_experience_data$Q51_score <- rowMeans(select(climate_experience_data, Q51_remote_vacation:Q51_heritage))\nclimate_experience_data <- climate_experience_data %>% mutate( Q51_bin = case_when( Q51_score > mean(Q51_score) + sd(Q51_score) ~ “high”, Q51_score < mean(Q51_score) - sd(Q51_score) ~ “low”, TRUE ~ “medium” ) %>% factor(levels = c(“low”, “medium”, “high”)) )\nclimate_experience_data$Q52_score <- rowMeans(select(climate_experience_data, Q52a_1:Q52f_1))\nclimate_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”)) )" }, { "objectID": "chapter_2.html#q57-subsetting-based-on-religiosity", diff --git a/hacking_religion/chapter_1.qmd b/hacking_religion/chapter_1.qmd index 2014b11..441f8c0 100644 --- a/hacking_religion/chapter_1.qmd +++ b/hacking_religion/chapter_1.qmd @@ -201,10 +201,11 @@ Here's the process to identify a dataset within the nomis platform: ```{r} # Process to explore nomis() data for specific datasets library(nomisr) -religion_search <- nomis_search(name = "*Religion*") -religion_measures <- nomis_get_metadata("ST104", "measures") -tibble::glimpse(religion_measures) -religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE") +# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided: +#religion_search <- nomis_search(name = "*Religion*") +#religion_measures <- nomis_get_metadata("ST104", "measures") +#tibble::glimpse(religion_measures) +#religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE") ``` ::: @@ -212,9 +213,12 @@ religion_geography <- nomis_get_metadata("NM_529_1", "geography", "TYPE") ```{r} library(nomisr) - # Get table of Census 2011 religion data from nomis -z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301)) +# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided: +#z <- nomis_get_data(id = "NM_529_1", time = "latest", geography = "TYPE499", measures=c(20301)) +#saveRDS(z, file = "z.rds") +z <- readRDS(file = (here("example_data", "z.rds"))) + # Filter down to simplified dataset with England / Wales and percentages without totals uk_census_2011_religion <- filter(z, GEOGRAPHY_NAME=="England and Wales" & RURAL_URBAN_NAME=="Total" & C_RELPUK11_NAME != "All categories: Religion") # Drop unnecessary columns @@ -225,7 +229,11 @@ plot1 <- ggplot(uk_census_2011_religion, aes(x = C_RELPUK11_NAME, y = OBS_VALUE) # grab daata from nomis for 2001 census religion / ethnicity -z0 <- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100)) +# temporarily commenting out until renv can be implemented and runtime errors in other environments avoided: +#z0 <- nomis_get_data(id = "NM_1872_1", time = "latest", geography = "TYPE499", measures=c(20100)) +#saveRDS(z0, file = "z0.rds") +z0 <- readRDS(file = (here("example_data", "z0.rds"))) + # select relevant columns uk_census_2001_religion_ethnicity <- select(z0, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHHUK11_NAME, OBS_VALUE) # Filter down to simplified dataset with England / Wales and percentages without totals @@ -234,7 +242,11 @@ uk_census_2001_religion_ethnicity <- filter(uk_census_2001_religion_ethnicity, G uk_census_2001_religion_ethnicity <- uk_census_2001_religion_ethnicity %>% filter(grepl('Total', C_ETHHUK11_NAME)) # grab data from nomis for 2011 census religion / ethnicity table -z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100)) +# commenting out nomis_get temporarily until I can get renv working properly here +#z1 <- nomis_get_data(id = "NM_659_1", time = "latest", geography = "TYPE499", measures=c(20100)) +#saveRDS(z1, file = "z1.rds") +z1 <- readRDS(file = (here("example_data", "z1.rds"))) + # select relevant columns uk_census_2011_religion_ethnicity <- select(z1, GEOGRAPHY_NAME, C_RELPUK11_NAME, C_ETHPUK11_NAME, OBS_VALUE) # Filter down to simplified dataset with England / Wales and percentages without totals @@ -243,7 +255,10 @@ uk_census_2011_religion_ethnicity <- filter(uk_census_2011_religion_ethnicity, G uk_census_2011_religion_ethnicity <- uk_census_2011_religion_ethnicity %>% filter(grepl('Total', C_ETHPUK11_NAME)) # grab data from nomis for 2021 census religion / ethnicity table -z2 <- nomis_get_data(id = "NM_2131_1", time = "latest", geography = "TYPE499", measures=c(20100)) +#z2 <- nomis_get_data(id = "NM_2131_1", time = "latest", geography = "TYPE499", measures=c(20100)) +#saveRDS(z2, file = "z2.rds") +z2 <- readRDS(file = (here("example_data", "z2.rds"))) + # select relevant columns uk_census_2021_religion_ethnicity <- select(z2, GEOGRAPHY_NAME, C2021_RELIGION_10_NAME, C2021_ETH_8_NAME, OBS_VALUE) # Filter down to simplified dataset with England / Wales and percentages without totals