# Load necessary libraries
pacman::p_load(pacman, readr,tidyverse, dplyr, ggplot2, gridExtra, tidyr, lubridate, stringr)
# Load the data
data_TRrb5 <- read.csv("data-TRrb5.csv", header = TRUE, stringsAsFactors = FALSE)
data_1ArwA <- read.csv("data-1ArwA.csv", header = TRUE, stringsAsFactors = FALSE)
data_LbYTZ <- read.csv("data-LbYTZ.csv", header = TRUE, stringsAsFactors = FALSE)
# Visualising 'data-1ArwA.csv' - Beliefs in God, Angels, Heaven, Hell, and the Devil
data_1ArwA <- data_1ArwA %>%
rename(Concept = For.each.of.the.following.items.I.am.going.to.read.you..please.tell.me.whether.it.is.something.you.believe.in..something.you.re.not.sure.about..or.something.you.don.t.believe.in.)
# Convert the data to long format for easier visualisation
data_1ArwA_long <- data_1ArwA %>%
pivot_longer(
cols = c(X..Believe.in, X..Not.sure.about, X..Don.t.believe.in),
names_to = "Belief_Type",
values_to = "Percentage"
) %>%
mutate(
Belief_Type = case_when(
Belief_Type == "X..Believe.in" ~ "Believe in",
Belief_Type == "X..Not.sure.about" ~ "Not sure about",
Belief_Type == "X..Don.t.believe.in" ~ "Don't believe in",
TRUE ~ Belief_Type
)
)
ggplot(data_1ArwA_long, aes(x = Concept, y = Percentage, fill = Belief_Type)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
theme_minimal() +
geom_text(aes(label = Percentage),
position = position_dodge(width = 0.9), # Match dodge width of bars
hjust = .5, vjust = -.5, # Position labels slightly outside the end of the bar
color = "black", size = 3) +
labs(
title = "Americans Belief in Five Spiritual Concepts, May 1-24, 2023: Source Gallup",
x = "Concept",
y = "Percentage (%)",
fill = "Belief Type"
) +
theme(axis.text.x = element_text(angle = 0, hjust = 0.5))

# Create Data frame
data_TRrb5_filtered_all <- data.frame(
Category = c(
rep("Weekly", 5), rep("Nearly Weekly/Monthly", 5), rep("Less often", 5),
rep("Protestant/Other Christian", 5), rep("Catholic", 5), rep("None", 5),
rep("Party identification", 5), rep("Republican", 5), rep("Independent", 5),
rep("Democrat", 5), rep("Less than $40,000", 5), rep("$40,000-100,000", 5),
rep("$100,000+", 5), rep("Age", 5), rep("55 and older", 5),
rep("35-54", 5), rep("18-34", 5), rep("Gender", 5), rep("Female", 5),
rep("Male", 5), rep("Education", 5), rep("Not college grad", 5),
rep("College grad", 5)
),
Concept = rep(c("God", "Angels", "Heaven", "Hell", "The.devil"), 23),
Percentage = c(
98, 94, 92, 84, 86, 94, 90, 90, 80, 77, 57, 52, 48, 41, 40,
94, 87, 89, 81, 80, 85, 80, 76, 61, 60, 25, 25, 17, 15, 12,
NA, NA, NA, NA, NA, 87, 85, 84, 79, 78, 68, 63, 60, 51, 51,
66, 60, 56, 48, 44, 81, 85, 76, 65, 63, 72, 66, 63, 57, 57,
68, 60, 62, 55, 54, NA, NA, NA, NA, NA, 83, 75, 75, 64, 63,
73, 68, 66, 58, 59, 59, 61, 55, 52, 49, NA, NA, NA, NA, NA,
79, 76, 72, 61, 58, 70, 64, 63, 57, 58, NA, NA, NA, NA, NA,
76, 75, 71, 63, 61, 69, 58, 59, 51, 51
),
Grouping_Label = c(
rep("Attendance: Weekly", 5), rep("Attendance: Nearly Weekly/Monthly", 5),
rep("Attendance: Less often", 5), rep("Religion: Protestant/Other Christian", 5),
rep("Religion: Catholic", 5), rep("Religion: None", 5),
rep("Party identification", 5), rep("Party: Republican", 5),
rep("Party: Independent", 5), rep("Party: Democrat", 5),
rep("Income: Less than $40,000", 5), rep("Income: $40,000-100,000", 5),
rep("Income: $100,000+", 5), rep("Age", 5), rep("Age: 55 and older", 5),
rep("Age: 35-54", 5), rep("Age: 18-34", 5), rep("Gender", 5),
rep("Gender: Female", 5), rep("Gender: Male", 5), rep("Education", 5),
rep("Education: Not college grad", 5), rep("Education: College grad", 5)
)
)
# Visualising 'data-TRrb5.csv'- Belief by Various Demographic Groupings
data_TRrb5_filtered_all <- data_TRrb5_filtered_all %>%
mutate(Grouping_Label = case_when(
Category %in% c("18-34", "35-54", "55 and older") ~ paste("Age:", Category),
Category %in% c("Female", "Male") ~ paste("Gender:", Category),
Category %in% c("Democrat", "Independent", "Republican") ~ paste("Party:", Category),
Category %in% c("Catholic", "Protestant/Other Christian", "None") ~ paste("Religion:", Category),
Category %in% c("$40,000-100,000", "$100,000+", "Less than $40,000") ~ paste("Income:", Category),
Category %in% c("Weekly", "Nearly Weekly/Monthly", "Less often") ~ paste("Attendance:", Category),
Category %in% c("College grad", "Not college grad") ~ paste("Education:", Category),
TRUE ~ Category # Default case if any other categories are present
))
ggplot(data_TRrb5_filtered_all, aes(x = Grouping_Label, y = Percentage, fill = Concept)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ Concept, scales = "free_y", ncol = 3) + # Facet by each concept in 3 columns
theme_minimal() +
labs(title = "Americans Belief in Five Spiritual Concepts, by Demographic Subgroup, May 1-24, 2023: Source Gallup",
x = "Demographic Groupings", y = "Percentage (%)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Visualising `data-LbYTZ.csv` - Historical belief trends
data_LbYTZ_clean <- data_LbYTZ %>%
rename(Date = 1) %>%
pivot_longer(cols = -Date, names_to = "Concept", values_to = "Percentage") %>%
mutate(Percentage = as.numeric(Percentage))
ggplot(data_LbYTZ_clean, aes(x = Date, y = Percentage, color = Concept, group = Concept)) +
geom_line(linewidth = 2) +
geom_point(size = 4) +
geom_text(aes(label = Percentage), color = "black", vjust = -1, size = 3) + # Add labels above the points
theme_minimal() +
labs(title = "Americans Belief in Five Spiritual Concepts, 2001-2023: Source Gallup",
x = "Date", y = "Percentage (%)") +
theme(axis.text.x = element_text(angle = 0, hjust = .5))
