Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created January 9, 2025 17:24
Show Gist options
  • Save ryanburge/7b1380e99e81a60362efae07496aea19 to your computer and use it in GitHub Desktop.
Save ryanburge/7b1380e99e81a60362efae07496aea19 to your computer and use it in GitHub Desktop.
gg1 <- gss %>%
filter(year <= 2018) %>%
mutate(xmar = teensex) %>%
mutate(xmar = frcode(xmar == 4 ~ "Not Wrong",
xmar == 3 ~ "Sometimes",
xmar == 2 ~ "Almost Always",
xmar == 1 ~ "Always")) %>%
group_by(year) %>%
ct(xmar, wt = wtssall, show_na = FALSE)
gg2 <- gss %>%
filter(year > 2018) %>%
mutate(xmar = teensex) %>%
mutate(xmar = frcode(xmar == 4 ~ "Not Wrong",
xmar == 3 ~ "Sometimes",
xmar == 2 ~ "Almost Always",
xmar == 1 ~ "Always")) %>%
group_by(year) %>%
ct(xmar, wt = wtssnrps, show_na = FALSE)
both <- bind_rows(gg1, gg2)
both %>%
ggplot(., aes(x = year, y = pct, color = xmar, group = xmar)) +
geom_point(stroke = .75, shape = 21) +
geom_labelsmooth(aes(label = xmar), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .75) +
y_pct() +
scale_color_manual(values = c("Not Wrong" = "#1b9e77",
"Sometimes" = "#d95f02",
"Almost Always" = "#7570b3",
"Always" = "#e7298a")) +
theme_rb() +
theme(plot.title = element_text(size = 14)) +
labs(x = "", y = "", title = "How Do You Feel About Sex Relations Between Unmarried 14 to 16 Year Olds?",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 1986-2022")
save("teensex_gss22.png")
gg1 <- gss %>%
filter(year <= 2018) %>%
gss_reltrad6(reltrad) %>%
mutate(xmar = teensex) %>%
mutate(always = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
group_by(year, reltrad) %>%
mean_ci(always, wt = wtssall)
gg2 <- gss %>%
filter(year > 2018) %>%
gss_reltrad6(reltrad) %>%
mutate(xmar = teensex) %>%
mutate(always = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
group_by(year, reltrad) %>%
mean_ci(always, wt = wtssnrps)
both <- bind_rows(gg1, gg2) %>% na.omit()
labels <- both %>%
filter(year %in% c(1986, 2022)) %>%
mutate(label = scales::percent(mean, accuracy = 1))
both %>%
ggplot(., aes(x = year, y = mean, color = reltrad)) +
geom_point(stroke = .5, shape = 21, alpha = .75) +
geom_smooth(se = FALSE) +
facet_wrap(~ reltrad) +
geom_text(data = labels, aes(label = label, y = mean - 0.02),
color = "black", size = 4, vjust = 1, family = "font") +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(0, .90)) +
scale_color_manual(values = met.brewer("Java", 6)) +
labs(x = "", y = "", title = "Share Saying Sex Among Unmarried 14 to 16 Year Olds is Always Wrong",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 1986-2022")
save("teensex_gss22_reltrad.png")
gg <- gss %>%
filter(year >= 2021) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-\n35",
age >= 36 & age <= 44 ~ "36-\n44",
age >= 45 & age <= 54 ~ "45-\n54",
age >= 55 & age <= 64 ~ "55-\n64",
age >= 65 ~ "65+")) %>%
mutate(xmar = teensex) %>%
mutate(always = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
gss_reltrad6(reltrad) %>%
group_by(age2, reltrad) %>%
mean_ci(always, wt = wtssnrps, ci = .84) %>% na.omit()
gg %>%
ggplot(., aes(x = age2, y = mean, fill = reltrad)) +
geom_col(color = "black") +
theme_rb() +
facet_wrap(~ reltrad) +
error_bar() +
y_pct() +
lab_bar_white(type = mean, above = FALSE, pos = .05, sz = 6) +
scale_fill_manual(values = met.brewer("Java", 6)) +
labs(x = "Age of Respondent", y = "", title = "Share Saying Sex Among Unmarried 14 to 16 Year Olds is Always Wrong",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 2021-2022")
save("teensex_age2_gss22.png", ht = 8)
gg <- gss %>%
filter(year >= 2021) %>%
mutate(gender = frcode(sex == 1 ~ "Men",
sex == 2 ~ "Women")) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-\n35",
age >= 36 & age <= 44 ~ "36-\n44",
age >= 45 & age <= 54 ~ "45-\n54",
age >= 55 & age <= 64 ~ "55-\n64",
age >= 65 ~ "65+")) %>%
mutate(xmar = teensex) %>%
mutate(always = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
gss_reltrad6(reltrad) %>%
group_by(age2, reltrad, gender) %>%
mean_ci(always, wt = wtssnrps, ci = .84) %>% na.omit()
gg %>%
ggplot(., aes(x = age2, y = mean, fill = gender)) +
geom_col(color = "black", position = "dodge") +
theme_rb(legend = TRUE) +
facet_wrap(~ reltrad) +
error_bar() +
scale_y_continuous(labels = percent, limits = c(0, 1)) +
# lab_bar_white(type = mean, above = FALSE, pos = .05, sz = 4) +
scale_fill_calc() +
labs(x = "Age of Respondent", y = "", title = "Share Saying Sex Among Unmarried 14 to 16 Year Olds is Always Wrong",
caption = "@ryanburge + @religiondata\nData: General Social Survey, 2021-2022")
save("teensex_age2_gss22_gender.png", ht = 8)
gg1 <- gss %>%
filter(year <= 2018) %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode(birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(xmar = teensex) %>%
mutate(xmar = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
group_by(cohorts, year) %>%
mean_ci(xmar, wt = wtssall)
gg2 <- gss %>%
filter(year > 2018) %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode(birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(xmar = teensex) %>%
mutate(xmar = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
group_by(cohorts, year) %>%
mean_ci(xmar, wt = wtssnrps)
both1 <- bind_rows(gg1, gg2) %>% na.omit() %>% mutate(type = "Teen Sex")
gg1 <- gss %>%
filter(year <= 2018) %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode(birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(xmar = xmarsex) %>%
mutate(xmar = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
group_by(cohorts, year) %>%
mean_ci(xmar, wt = wtssall)
gg2 <- gss %>%
filter(year > 2018) %>%
mutate(birthyr = year - age) %>%
mutate(cohorts = frcode(birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
mutate(xmar = xmarsex) %>%
mutate(xmar = case_when(xmar == 1 ~ 1,
xmar == 2 | xmar == 3 | xmar == 4 ~ 0)) %>%
group_by(cohorts, year) %>%
mean_ci(xmar, wt = wtssnrps)
both2 <- bind_rows(gg1, gg2) %>% mutate(type = "Extramarital Sex")
both <- bind_rows(both1, both2)
both %>%
filter(cohorts != "NA") %>%
ggplot(., aes(x = year, y =mean, color = type, group = type)) +
geom_point(stroke = .5, shape = 21, alpha = 1) +
geom_smooth(se = FALSE, method = "lm", formula = y ~ poly(x, 2)) +
facet_wrap(~ cohorts) +
scale_color_calc() +
theme_rb(legend = TRUE) +
y_pct() +
theme(legend.text = element_text(size = 22)) +
labs(x = "", y = "", title = "Share Saying Each Type of Sexual Behavior is Always Wrong", caption = "@ryanburge + @religiondata\nData: General Social Survey, 1973-2022")
save("compare_sex_birth_cohort.png", ht = 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment