Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created January 5, 2025 16:28
Show Gist options
  • Select an option

  • Save ryanburge/51c03598968bd12c74c02e72697c387f to your computer and use it in GitHub Desktop.

Select an option

Save ryanburge/51c03598968bd12c74c02e72697c387f to your computer and use it in GitHub Desktop.
gg <- gss %>%
ct(year)
gg %>% mean_ci(n)
gg %>%
ggplot(., aes(x = year, y = n, fill = n)) +
geom_col(color = "black") +
theme_rb() +
geom_text(
aes(y = n + 250, label = n),
position = position_dodge(width = .9),
size = 5,
family = "font",
angle = 90, # Rotate the text vertically
vjust = 0.5 # Adjust vertical alignment for better readability
) +
theme_rb() +
scale_fill_gradient(
low = "#56B4E9", # Light blue
high = "#009E73" # Green
) +
add_text(x = 1985, y = 4200, word = 'Average Sample Size = 2130', sz = 8) +
labs(x = "", y = "", title = "Total Number of Respondents in the General Social Survey, 1972-2022")
save("gss_sample_size.png")
library(rio)
anes <- import("E://data/anes_2022.dta")
gg <- anes %>%
mutate(year = VCF0004) %>%
ct(year)
gg %>% mean_ci(year)
gg %>%
ggplot(., aes(x = year, y = n, fill = n)) +
geom_col(color = "black") +
theme_rb() +
geom_text(
aes(y = n + 450, label = n),
position = position_dodge(width = .9),
size = 5,
family = "font",
angle = 90, # Rotate the text vertically
vjust = 0.5 # Adjust vertical alignment for better readability
) +
theme_rb() +
scale_fill_gradient(
low = "#8E44AD", # Purple
high = "#F39C12" # Orange
) +
add_text(x = 1965, y = 7200, word = 'Average Sample Size = 1981', sz = 8) +
labs(x = "", y = "", title = "Total Number of Respondents in the American National Election Study, 1948-2020")
save("anes_sample_size.png")
one <- gss %>%
filter(year <= 2018) %>%
group_by(year) %>%
ct(denom, wt = wtssall)
two <- gss %>%
filter(year > 2018) %>%
group_by(year) %>%
ct(denom, wt = wtssnrps)
both <- bind_rows(one, two)
both <- both %>%
filter(denom == 14 | denom == 22) %>%
mutate(denom = frcode(denom == 14 ~ "Southern Baptist",
denom == 22 ~ "United Methodist"))
library(ggtext)
both %>%
ggplot(., aes(x = year, y = n, color = denom)) +
geom_line() +
geom_point(stroke = 1, shape = 21, fill = "white") +
scale_y_continuous(limits = c(0, 340)) +
scale_color_manual(values = c("#0099B4FF", "#AD002AFF", "#42B540FF")) +
theme_rb() +
annotate("text", x = 2014, y = 280, label = "SBC Avg: 171", size = 6, family = "font", color = "#0099B4FF") +
annotate("text", x = 2014, y = 250, label = "UMC Avg: 125", size = 6, family = "font", color = "#AD002AFF") +
labs(
x = "Year",
y = "",
title = "<span style = 'font-size:18pt'>Share of the Entire Sample Who are </span><span style = 'color:#0099B4FF; font-size:18pt'>Southern Baptists</span>
or <span style = 'color:#AD002AFF; font-size:18pt'>United Methodists",
caption = "@ryanburge\nData: General Social Survey, 1984-2018") +
theme(
text = element_text(family = "font"),
# plot.title.position = "plot",
plot.title = element_markdown(size = 14, lineheight = 1.2))
save("sample_size_two_denoms_gss.png")
gg1 <- gss %>%
filter(year >= 2008) %>%
ct(year) %>%
mutate(type = "GSS")
gg2 <- cces %>%
filter(year >= 2008) %>%
ct(year) %>%
mutate(type = "CES")
both <- bind_rows(gg1, gg2)
both_complete <- both %>%
tidyr::complete(year, type, fill = list(n = 0, pct = 0))
both_complete %>%
ggplot(aes(x =factor(year), y = n, fill = type)) +
geom_col(color = "black", position = "dodge") +
theme_rb(legend = TRUE) +
labs(
title = "Sample Sizes for GSS and CES by Year, 2008-2023",
x = "Year",
y = "Sample Size",
fill = "Survey Type"
) +
geom_text(
aes(y = n + 4800, label = n),
position = position_dodge(width = .9),
size = 5,
family = "font",
angle = 90, # Rotate the text vertically
vjust = 0.5 # Adjust vertical alignment for better readability
) +
scale_y_continuous(limits = c(0, 70000)) +
scale_fill_manual(values = c("steelblue", "tomato")) +
theme(
plot.title = element_text(size = 22, face = "bold"))
save("size_compare.png")
gg <- cces22 %>%
mutate(trad2 = frcode(religpew_baptist == 1 ~ "Southern Baptist",
religpew_baptist == 2 ~ "ABCUSA",
religpew_baptist == 3 ~ "Natl. Baptist Convention",
religpew_baptist == 4 ~ "Progressive Baptist Convention",
religpew_baptist == 5 ~ "Independent Baptist",
religpew_baptist == 6 ~ "Baptist General Conference",
religpew_baptist == 7 ~ "Baptist Miss. Association",
religpew_baptist == 8 ~ "Conservative Bapt. Association",
religpew_baptist == 9 ~ "Free Will Baptist",
religpew_baptist == 10 ~ "Gen. Assoc. of Reg. Bapt.",
religpew_methodist == 1 ~ "United Methodist",
religpew_methodist == 2 ~ "Free Methodist",
religpew_methodist == 3 ~ "African Methodist Episcopal",
religpew_methodist == 4 ~ "AME - Zion",
religpew_methodist == 5 ~ "Christian Methodist Episcopal",
religpew_nondenom == 1 ~ "Nondenom Evangelical",
religpew_nondenom == 2 ~ "Nondenom Fundamentalist",
religpew_nondenom == 3 ~ "Nondenom Charismatic",
religpew_nondenom == 4 ~ "Interdenominational",
religpew_nondenom == 5 ~ "Community Church",
religpew_nondenom == 90 ~ "Other Nondenom",
religpew_lutheran == 1 ~ "Evangelical Lutheran Church in America",
religpew_lutheran == 2 ~ "Lutheran - Missouri Synod",
religpew_lutheran == 3 ~ "Lutheran - Wisconsin Synod",
religpew_presby == 1 ~ "Presbyterian Church (USA)",
religpew_presby == 2 ~ "Presybterian Church of America",
religpew_presby == 3 ~ "Associated Reformed Presbyterian",
religpew_presby == 4 ~ "Cumberland Presbyterian",
religpew_presby == 5 ~ "Orthodox Presbyterian",
religpew_presby == 6 ~ "Evangelical Presbyterian",
religpew_pentecost == 1 ~ "Assemblies of God",
religpew_pentecost == 2 ~ "Church of God Cleveland TN",
religpew_pentecost == 3 ~ "Four Square Gospel",
religpew_pentecost == 4 ~ "Pentecostal Church of God",
religpew_pentecost == 5 ~ "Pentecostal Holiness Church",
religpew_pentecost == 6 ~ "Church of God in Christ",
religpew_pentecost == 7 ~ "Church of God of the Apostolic Faith",
religpew_pentecost == 8 ~ "Assembly of Christian Churches",
religpew_pentecost == 9 ~ "Apostolic Christian",
religpew_episcop == 1 ~ "The Episcopal Church",
religpew_episcop == 2 ~ "Church of England",
religpew_episcop == 3 ~ "Anglican Orthodox",
religpew_episcop == 4 ~ "Reformed Episcopal",
religpew_christian == 1 ~ "Church of Christ",
religpew_christian == 2 ~ "Disciples of Christ",
religpew_christian == 3 ~ "Christian Church",
religpew_congreg == 1 ~ "United Church of Christ",
religpew_congreg == 2 ~ "Conservative Cong. Christian",
religpew_congreg == 3 ~ "National Assoc. of Cons. Cong.",
religpew_holiness == 1 ~ "Church of the Nazarene",
religpew_holiness == 2 ~ "Wesleyan Church",
religpew_holiness == 4 ~ "Christian and Missionary Alliance",
religpew_holiness == 5 ~ "Church of God",
religpew_holiness == 6 ~ "Salvation Army",
religpew_reformed == 1 ~ "Reformed Church in America",
religpew_reformed == 2 ~ "Christian Reformed")) %>%
ct(trad2, wt = commonweight, show_na = FALSE)
gg <- gg %>% mutate(n = round(n))
# Create the plot
ggplot(gg, aes(x = reorder(trad2, n), y = n)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black") +
geom_text(
aes(
label = n,
hjust = ifelse(n < 100, -0.3, -0.1) # Adjust the hjust dynamically
),
size = 3,
family = 'font'
) +
coord_flip() +
theme_rb() +
labs(
title = "Total Number of Respondents of Each Denomination in the Cooperative Election Study, 2022",
subtitle = "The Survey Had a Total of 61,000 Respondents",
x = "Denomination",
y = "Count",
caption = "@ryanburge\nData: Cooperative Election Study, 2022"
) +
theme(plot.title = element_text(size = 12))
save("denom_counts22.png", ht = 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment