Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created August 8, 2024 14:36
Show Gist options
  • Save ryanburge/70e6a7762c236e86e0b8cdf55ec2a18e to your computer and use it in GitHub Desktop.
Save ryanburge/70e6a7762c236e86e0b8cdf55ec2a18e to your computer and use it in GitHub Desktop.
library(readxl)
library(janitor)
cen20 <- read_excel("E://data/rel_cen20.xlsx", sheet = 2)
cen20 <- cen20 %>% clean_names()
mus <- cen20 %>%
filter(group_name == "Muslim Estimate") %>%
select(state_name, congregations, adherents, pct_adh = adherents_as_percent_of_total_adherents, pct_pop = adherents_as_percent_of_population)
mus <- mus %>%
mutate(bins = frcode(pct_pop > .03 ~ "3%+",
pct_pop > .01 ~ "1%-3%",
pct_pop > 0 ~ "<1%"))
mus$id <- tolower(mus$state_name)
library(fiftystater)
data("fifty_states")
mapp <- left_join(fifty_states, mus, by = c("id")) %>% as_tibble()
centers <- read_csv("D://state_centers_inset.csv") %>% rename(id = full)
centers$id <- tolower(centers$id)
centers <- left_join(mapp, centers, by = c("id")) %>% as_tibble()
centers <- centers %>% distinct(id, .keep_all = TRUE)
fips <- data.frame(
stringsAsFactors = FALSE,
id = c("Alabama","Alaska","Arizona",
"Arkansas","California","Colorado","Connecticut",
"Delaware","Florida","Georgia","Hawaii","Idaho",
"Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana",
"Maine","Maryland","Massachusetts","Michigan",
"Minnesota","Mississippi","Missouri","Montana","Nebraska",
"Nevada","New Hampshire","New Jersey","New Mexico",
"New York","North Carolina","North Dakota","Ohio",
"Oklahoma","Oregon","Pennsylvania","Rhode Island",
"South Carolina","South Dakota","Tennessee","Texas","Utah",
"Vermont","Virginia","Washington","West Virginia",
"Wisconsin","Wyoming","American Samoa","Guam",
"Northern Mariana Islands","Puerto Rico","Virgin Islands"),
abb = c("AL","AK","AZ","AR","CA",
"CO","CT","DE","FL","GA","HI","ID","IL","IN","IA",
"KS","KY","LA","ME","MD","MA","MI","MN","MS",
"MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND",
"OH","OK","OR","PA","RI","SC","SD","TN","TX",
"UT","VT","VA","WA","WV","WI","WY","AS","GU","MP",
"PR","VI"),
FIPS = c("01","02","04","05","06",
"08","09","10","12","13","15","16","17","18","19",
"20","21","22","23","24","25","26","27","28",
"29","30","31","32","33","34","35","36","37","38",
"39","40","41","42","44","45","46","47","48",
"49","50","51","53","54","55","56","60","66","69",
"72","78")
)
centers <- left_join(centers, fips)
centers <- centers %>%
mutate(mean = round(pct_pop, 2)) %>%
mutate(lab = mean*100) %>%
mutate(lab = paste0(lab, '%')) %>%
mutate(lab = str_replace(lab, "0%", "<1%")) %>%
mutate(label = paste(state, lab, sep = ": "))
library(ggrepel)
mapp %>%
ggplot(., mapping = aes(x = long, y = lat, group = group, fill = bins)) +
geom_polygon(color = "black", linewidth = 0.1) +
coord_map(projection = "albers", lat0 = 41, lat1 = 44) +
scale_fill_manual(values = c( "#03045e", "#00b4d8", "#caf0f8")) +
ggrepel::geom_label_repel(data = centers,
aes(x = longitude, y = latitude, label = label, group = group), size = 2.5, fill = "white",
seed = 1002, family = "font", force = .001, show.legend = FALSE) +
theme_rb(legend = TRUE) +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0, "lines"),
plot.background = element_blank(),
legend.position = "bottom",
plot.title = element_text(size = 18)) +
guides(fill = guide_legend(reverse = TRUE)) +
labs(title = "Share of the State That is Muslim", caption = "@ryanburge\nData: 2020 Religion Census")
save("cen20_muslim_state.png")
library(readxl)
cen20 <- read_excel("E://data/rel_cen20.xlsx", sheet = 3)
library(janitor)
cen20 <- cen20 %>%
clean_names()
mi <- cen20 %>%
filter(group_name == "Muslim Estimate") %>%
filter(state_name == "Michigan") %>%
select(fips, county_name, congregations, adherents, pct_adh = adherents_as_percent_of_total_adherents, pct_pop = adherents_as_percent_of_total_population)
mi <- mi %>%
mutate(bins = frcode(pct_pop > .02 ~ "2%+",
pct_pop > .01 ~ "1%-2%",
pct_pop > 0 ~ "0%-1%"))
library(urbnmapr)
territories_counties <- get_urbn_map(map = "territories_counties")
territories_counties <- territories_counties %>%
rename(fips = county_fips)
mapp <- left_join(territories_counties, mi)
color_palette <- c("25%+" = "#08306B", "10%-25%" = "#2171B5", "5%-10%" = "#6BAED6", "1%-5%" = "#C6DBEF", "NA" = "#BDBDBD")
mapp %>%
filter(state_name == "Michigan") %>%
ggplot(aes(long, lat, group = group, fill = bins)) +
geom_polygon(color = "black", size = 0.05) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
labs(title = " Muslim Concentration by County", caption = "@ryanburge\nData: 2020 Religious Census", fill = "") +
theme(legend.title = element_text(), legend.key.width = unit(.5, "in")) +
theme_rb() +
scale_fill_manual(values = c("0%-1%" = "#d8bfd8", "1%-2%" = "#9370db", "2%+" = "#4b0082")) +
urbnthemes::theme_urbn_map() +
theme(plot.title = element_text(size=20, family= "font", face= "bold")) +
theme(plot.caption = element_text(size = 14, family="font")) +
theme(text=element_text(size=18, family="font")) +
theme(legend.text = element_text(size = 20))
save("relcen_muslim_michigan.png")
cc20 <- cces20 %>%
filter(religpew == 6) %>%
select(immstat, weight = commonweight, inputstate)
cc21 <- cces21 %>%
filter(religpew == 6) %>%
select(immstat, weight = commonweight, inputstate)
cc22 <- cces22 %>%
filter(religpew == 6) %>%
select(immstat, weight = commonweight, inputstate)
cc23 <- cces23 %>%
filter(religpew == 6) %>%
select(immstat, weight = commonweight, inputstate)
all <- bind_rows(cc20, cc21, cc22, cc23)
gg1 <- all %>%
mutate(imm = frcode(immstat == 1 ~ "Immigrant,\nNaturalized",
immstat == 2 ~ "Immigrant,\nNon-Citizen",
immstat == 3 ~ "First\nGeneration",
immstat == 4 ~ "Second\nGeneration",
immstat == 5 ~ "Third\nGeneration\nor More")) %>%
ct(imm, wt = weight, show_na = FALSE) %>%
mutate(type = "Muslims")
cc20 <- cces20 %>%
select(immstat, weight = commonweight, inputstate)
cc21 <- cces21 %>%
select(immstat, weight = commonweight, inputstate)
cc22 <- cces22 %>%
select(immstat, weight = commonweight, inputstate)
cc23 <- cces23 %>%
select(immstat, weight = commonweight, inputstate)
all <- bind_rows(cc20, cc21, cc22, cc23)
gg2 <- all %>%
mutate(imm = frcode(immstat == 1 ~ "Immigrant,\nNaturalized",
immstat == 2 ~ "Immigrant,\nNon-Citizen",
immstat == 3 ~ "First\nGeneration",
immstat == 4 ~ "Second\nGeneration",
immstat == 5 ~ "Third\nGeneration\nor More")) %>%
ct(imm, wt = weight, show_na = FALSE) %>%
mutate(type = "Entire\nSample")
all <- bind_rows(gg1, gg2)
all %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = type, y = pct, fill = type)) +
geom_col(color = "black") +
facet_wrap(~ imm) +
theme_rb() +
scale_fill_calc() +
y_pct() +
geom_text(aes(y = pct + .045, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 8, family = "font") +
labs(x = "", y = "", title = "Immigration Status of Muslims vs All Americans", caption = '@ryanburge\nData: Cooperative Election Study, 2020-2023')
save("muslim_immigration.png", ht = 8, wd = 5)
## There are 64 Muslims from Michigan in the sample
## Total sample is 171200
## A total of 1518 Muslims
## Total votes cast in Michigan: 5,453,892
## Turnout was 70.5% of all eligible population in Michigan in 2020. https://www.michigan.gov/sos/-/media/Project/Websites/sos/Election-Results-and-Statistics/General-Voter-Reg-Turnout-Stats.pdf
## Total number of Muslims in Michigan in 241,828
gg <- cces %>%
filter(year >= 2020) %>%
mutate(trad = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion == 3 ~ "Mormon",
religion == 4 ~ "Orthodox",
religion == 5 ~ "Jewish",
religion == 6 ~ "Muslim",
religion == 7 ~ "Buddhist",
religion == 8 ~ "Hindu",
religion == 9 ~ "Atheist",
religion == 10 ~ "Agnostic",
religion == 11 ~ "Nothing in Particular")) %>%
mutate(kids = case_when(havekids == 1 ~ 1,
havekids == 2 ~ 0)) %>%
group_by(trad) %>%
mean_ci(kids, wt = weight, ci = .84) %>% filter(trad != 'NA')
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(trad, mean), y = mean, fill = trad)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
y_pct() +
error_bar()+
scale_fill_manual(values = c(moma.colors("Warhol", 11))) +
lab_bar(top = FALSE, type = lab, pos = .03, sz = 9) +
labs(x = "", y = "", title = "Share of Each Tradition That Are Currently Parents to Minor Children", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2023")
save("parents_trads_23.png")
gg <- cces %>%
filter(year >= 2020) %>%
mutate(trad = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion == 3 ~ "Mormon",
religion == 4 ~ "Orthodox",
religion == 5 ~ "Jewish",
religion == 6 ~ "Muslim",
religion == 7 ~ "Buddhist",
religion == 8 ~ "Hindu",
religion == 9 ~ "Atheist",
religion == 10 ~ "Agnostic",
religion == 11 ~ "Nothing in Particular")) %>%
mutate(kids = case_when(havekids == 1 ~ 1,
havekids == 2 ~ 0)) %>%
filter(havekids == 1) %>%
# group_by(trad) %>%
mean_ci(numkids, wt = weight, ci = .84) %>% filter(trad != 'NA')
gg %>%
mutate(lab = round(mean, 1)) %>%
ggplot(., aes(x = reorder(trad, mean), y = mean, fill = trad)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
y_pct() +
scale_fill_manual(values = c(moma.colors("Warhol", 11))) +
geom_text(aes(y = mean + .14, label = lab), position = position_dodge(width = .9), size = 8, family = "font") +
labs(x = "", y = "", title = "Among Parents, Average Number of Children", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2023")
save("parents_trads_23_numkids.png")
gg <- cces %>%
filter(year >= 2020) %>%
mutate(trad = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion == 3 ~ "Mormon",
religion == 4 ~ "Orthodox",
religion == 5 ~ "Jewish",
religion == 6 ~ "Muslim",
religion == 7 ~ "Buddhist",
religion == 8 ~ "Hindu",
religion == 9 ~ "Atheist",
religion == 10 ~ "Agnostic",
religion == 11 ~ "Nothing in Particular")) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 ~ "65+")) %>%
group_by(trad) %>%
ct(age2, wt = weight, show_na = FALSE) %>% filter(trad != 'NA')
lvl <- gg %>%
filter(age2 == "18-35") %>%
select(trad, sort = pct)
gg <- left_join(gg, lvl)
gg$trad <- fct_reorder(gg$trad, gg$sort, .desc = TRUE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(age2))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ trad, ncol =1, strip.position = "left") +
scale_fill_manual(values = c(moma.colors("Connors", 5))) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 1)) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 7, family = "font", color = "black") +
labs(x = "", y = "", title = "Age Distribution of Adults in Religious Traditions", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2023")
save("age2_trad2_distribution.png", wd = 9, ht = 6)
table <- mi %>%
select(county = county_name, congregations, adherents, pct_pop) %>%
mutate(pct_pop = pct_pop*1)
table <- table %>%
arrange(desc(pct_pop))
library(gt)
table %>%
gt() %>%
tab_header(
title = "List of Counties with a Mosque in Michigan",
subtitle = "") %>%
opt_table_font(
font = c(
"Abel Regular", "Segoe UI",
default_fonts()[-c(1:3)]
)) %>%
tab_options(
table.font.size = px(40L)
) %>%
cols_label(county = "County",
congregations = "Congregations",
adherents = "Adherents",
pct_pop = "% Muslim") %>%
fmt_percent(
columns = vars(pct_pop),
decimals = 2
) %>%
fmt_number(
columns = vars(adherents),
use_seps = TRUE,
decimals = 0
) %>%
gtsave("E://graphs23/michigan_muslims.png")
county <- cen20 %>%
filter(group_name == "Muslim Estimate") %>%
select(fips, county_name, state_name, congregations, adherents, pct_adh = adherents_as_percent_of_total_adherents, pct_pop = adherents_as_percent_of_total_population)
gg <- cces %>%
filter(year >= 2020) %>%
mutate(trad = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion == 3 ~ "Mormon",
religion == 4 ~ "Orthodox",
religion == 5 ~ "Jewish",
religion == 6 ~ "Muslim",
religion == 7 ~ "Buddhist",
religion == 8 ~ "Hindu",
religion == 9 ~ "Atheist",
religion == 10 ~ "Agnostic",
religion == 11 ~ "Nothing in Particular")) %>%
group_by(trad) %>%
mean_ci(age, wt = weight) %>% filter(trad != 'NA')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment