Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 25, 2024 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/710852da23c34f3c1cff08850eaa248d to your computer and use it in GitHub Desktop.
Save ryanburge/710852da23c34f3c1cff08850eaa248d to your computer and use it in GitHub Desktop.
df <- import("E://data/understanding.sav") %>% clean_names()
graph <- df %>%
mutate(ivf = scimoral_7) %>%
mutate(ivf = frcode(ivf == 5 ~ "Always Morally Wrong",
ivf == 4 ~ "Morally Wrong in Most Cases",
ivf == 3 ~ "Not a Moral Issue",
ivf == 2 ~ "Morally Acceptable in Most Cases",
ivf == 1 ~ "Always Morally Acceptable")) %>%
ct(ivf, wt = weight1, show_na = FALSE)
graph %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = ivf, y = pct, fill = ivf)) +
geom_col(color = "black") +
theme_rb() +
y_pct() +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97", "#5f2680")) +
scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) +
theme(plot.title = element_text(size = 13)) +
geom_text(aes(y = pct + .02, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 12, family = "font") +
labs(x = "", y = "", title = "How morally acceptable do you personally believe each of the following is? \nUsing in-vitro fertilization", caption = '@ryanburge + @religiondata\nData: Religious Understandings of Science, 2013-2014')
save("ivf_general.png", wd = 6)
fun <- function(var, name){
df %>%
mutate(ivf = {{var}}) %>%
mutate(ivf = frcode(ivf == 5 ~ "Always Morally Wrong",
ivf == 4 ~ "Morally Wrong in Most Cases",
ivf == 3 ~ "Not a Moral Issue",
ivf == 2 ~ "Morally Acceptable in Most Cases",
ivf == 1 ~ "Always Morally Acceptable")) %>%
ct(ivf, wt = weight1, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("scimoral_1", "scimoral_2", "scimoral_3", "scimoral_4","scimoral_5", "scimoral_6", "scimoral_7", "scimoral_8", "scimoral_9", "scimoral_10"))
name <- c("Destroying human embryos if doing so helps scientists find cures for diseases", "Creating a baby that is a clone of another person", "Changing a person's DNA or genes, to create a baby that is smarter, stronger, or better looking",
"Medical research that uses stem cells from sources that do NOT involve human embryos", "Use of reproductive technologies to identify diseases in utero", "Euthanasia or physician-assisted suicide",
"Using in-vitro fertilization", "Having an abortion for any reason", "Having an abortion if the mother's life is in danger", "Having an abortion if there is a strong chance of serious defect in the baby")
gg <- map2_df(var, name, fun, .progress = TRUE) %>% mutate(type = as.factor(type))
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(ivf))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = rev(c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97"))) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
guides(fill = guide_legend(reverse=T, ncol = 1)) +
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 = 6, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & ivf == "Morally Wrong in Most Cases", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & ivf == "Always Morally Wrong", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.x.top = element_text(size = 16)) +
theme(legend.text = element_text(size = 10)) +
guides(fill = guide_legend(reverse=T, ncol = 5)) + # Adjust 'ncol' to the number of your legend items or as needed for a horizontal layout
labs(x = "", y = "", title = "How morally acceptable do you personally believe each of the following is?", caption = "@ryanburge + @religiondata\nData: Religious Understandings of Science, 2013-2014")
save("ivf_compare.png", wd = 9, ht = 8)
fun <- function(var, name){
df %>%
mutate(id3 = frcode(polview == 1 | polview == 2 | polview == 3 ~ "Liberal",
polview == 4 ~ "Moderate",
polview == 5 | polview == 6 | polview == 7 ~ "Conservative")) %>%
mutate(ivf = {{var}}) %>%
mutate(ivf = case_when(ivf == 5 | ivf == 4 ~ 1,
ivf == 3 | ivf == 2 | ivf == 1 ~ 0)) %>%
group_by(id3) %>%
mean_ci(ivf, ci = .84) %>%
mutate(type = name)
}
var <- syms(c("scimoral_1", "scimoral_2", "scimoral_3", "scimoral_4","scimoral_5", "scimoral_6", "scimoral_7", "scimoral_8", "scimoral_9", "scimoral_10"))
name <- c("Destroying human embryos if doing so helps scientists find cures for diseases", "Creating a baby that is a clone of another person", "Changing a person's DNA or genes, to create a baby that is smarter, stronger, or better looking",
"Medical research that uses stem cells from sources that do NOT involve human embryos", "Use of reproductive technologies to identify diseases in utero", "Euthanasia or physician-assisted suicide",
"Using in-vitro fertilization", "Having an abortion for any reason", "Having an abortion if the mother's life is in danger", "Having an abortion if there is a strong chance of serious defect in the baby")
gg <- map2_df(var, name, fun, .progress = TRUE) %>% filter(id3 != "NA")
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = id3, y = mean, fill = id3)) +
geom_col(color = "black") +
facet_wrap(~ type, ncol = 2, labeller = label_wrap_gen(width = 45)) +
pid3_fill() +
theme_rb() +
y_pct() +
error_bar() +
lab_bar(top = FALSE, type = lab, pos = .10, sz = 8) +
theme(plot.title = element_text(size = 12)) +
labs(x = "", y = "", title = "How morally acceptable do you personally believe each of the following is?", caption = "@ryanburge + @religiondata\nData: Religious Understandings of Science, 2013-2014")
save("id3_ivf_compare.png", wd = 6, ht = 12)
graph <- df %>%
mutate(ivf = scimoral_7) %>%
mutate(ivf = frcode(ivf == 5 ~ "Always Morally Wrong",
ivf == 4 ~ "Morally Wrong in Most Cases",
ivf == 3 ~ "Not a Moral Issue",
ivf == 2 ~ "Morally Acceptable in Most Cases",
ivf == 1 ~ "Always Morally Acceptable")) %>%
mutate(rel = rlgexcl) %>%
mutate(rel = frcode(rel == 1 ~ "There is very little truth in any religion",
rel == 2 ~ "There are basic truths in many religions",
rel == 3 ~ "There is truth in only one religion")) %>%
group_by(ivf) %>%
ct(rel, wt = weight1, show_na = FALSE) %>% filter(rel != "NA")
graph %>%
filter(ivf != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(rel))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ ivf, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = rev(c("#033f63", "#e1e1e1", "#FEDC97"))) +
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") +
geom_text(aes(label = ifelse(pct >.05 & rel == "There is very little truth in any religion", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 7, family = "font", color = "white") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(legend.text = element_text(size = 8)) +
labs(x = "", y = "", title = "Based on Respondent's View of IVF - What is Your View of Religion?", caption = "@ryanburge + @religiondata\nData: Religious Understandings of Science, 2013-2014")
save("ivf_view_religion.png", wd = 9, ht = 4)
heat <- df %>%
mutate(ivf = scimoral_7) %>%
mutate(ivf = frcode(ivf == 5 ~ "Always\nMorally\nWrong",
ivf == 4 ~ "Morally Wrong\nin Most Cases",
ivf == 3 ~ "Not a\nMoral Issue",
ivf == 2 ~ "Morally\nAcceptable\nin Most Cases",
ivf == 1 ~ "Always\nMorally\nAcceptable")) %>%
mutate(ab = scimoral_8) %>%
mutate(ab = frcode(ab == 5 ~ "Always\nMorally\nWrong",
ab == 4 ~ "Morally Wrong\nin Most Cases",
ab == 3 ~ "Not a\nMoral Issue",
ab == 2 ~ "Morally\nAcceptable\nin Most Cases",
ab == 1 ~ "Always\nMorally\nAcceptable")) %>%
filter(ivf != "NA") %>%
filter(ab != "NA") %>%
group_by(ab) %>%
ct(ivf, wt = weight1, show_na = FALSE) %>%
mutate(ivf = fct_rev(ivf)) %>%
mutate(ab = fct_rev(ab))
heat %>%
ungroup(ivf) %>%
summarise(sum = sum(n))
heat %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x= ivf, y = ab)) +
geom_tile(aes(fill = pct), color = "black") +
scale_fill_gradient2(low = "#033f63", mid = "#e1e1e1", high = "#FEDC97", midpoint = .2) +
theme_rb() +
theme(plot.subtitle = element_text(size = 24)) +
geom_text(aes(x= ivf, y = ab, label = paste0(lab*100, '%')), size = 9, family = "font") +
theme(plot.title = element_text(size = 15)) +
labs(x= "View of IVF", y = "Views of Abortion for Any Reason", title = "View of IVF Broken Down by Views of Abortion", caption = "@ryanburge + @religiondata\nData: Religious Understandings of Science, 2013-2014")
save('heat_ab_ivf.png', wd = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment