Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created August 31, 2019 00:08
Show Gist options
  • Save ryanburge/bf1cc335d4e956f9acac4ab0d1058141 to your computer and use it in GitHub Desktop.
Save ryanburge/bf1cc335d4e956f9acac4ab0d1058141 to your computer and use it in GitHub Desktop.
Life Cycle Effect
gg <- gss %>%
mutate(trad = frcode(nofaith == 1 ~ "Nones",
TRUE ~ "Others")) %>%
group_by(trad, year) %>%
mean_ci(attend, wt = wtssall)
gg %>%
filter(trad == "Others") %>%
ggplot(., aes(x = year, y = mean)) +
geom_point(size=4, color="white") +
geom_point(size=3, shape=1, alpha=.5) +
geom_point(size=2, shape=19) +
geom_line() +
geom_smooth(method = lm, alpha = .1, size = 0, span = .5) +
geom_line(stat = "smooth", method = lm, linetype = "dashed", show.legend = FALSE, color = "darkorchid", alpha = .45) +
theme_gg("Abel") +
scale_y_continuous(breaks = c(3,4,5), labels = c("Several Times a Year", "Once a Month", "2-3x a Month"), limits = c(3,5)) +
labs(x = "", y = "", title = "Average Church Attendance", subtitle = "Excluding the Nones", caption = "@ryanburge\nData: GSS 1972-2018") +
ggsave("E://attend_year_gss.png", type = "cairo-png")
fff1 <- gss %>%
filter(year >= 1972 & year <= 1982) %>%
group_by(age) %>%
mean_ci(attend) %>%
mutate(years = "1972-1982")
fff2 <- gss %>%
filter(year >= 1982 & year <= 1992) %>%
group_by(age) %>%
mean_ci(attend) %>%
mutate(years = "1982-1992")
fff3 <- gss %>%
filter(year >= 1992 & year <= 2002) %>%
group_by(age) %>%
mean_ci(attend) %>%
mutate(years = "1992-2002")
fff4 <- gss %>%
filter(year >= 2002 & year <= 2018) %>%
group_by(age) %>%
mean_ci(attend) %>%
mutate(years = "2002-2018")
graph <- bind_df("fff")
graph %>%
filter(age <= 80) %>%
ggplot(., aes(x = age, y = mean, group = years, color = years)) +
scale_color_tableau() +
# scale_color_manual(values = c("#2C3531", "#116466", "#D9B08C", "#5D001E")) +
geom_smooth(linetype = "twodash", se = FALSE) +
theme_gg("Abel") +
theme(legend.position = c(0.8, 0.2)) +
guides(color=guide_legend(title="New Legend Title")) +
annotate("text", x= 69.4, y = 3.42, label = "Survey Years", size = 6, family = "font") +
scale_y_continuous(breaks = c(3,4,5), labels = c("Several Times a Year", "Once a Month", "2-3x a Month")) +
labs(x= "Age of Respondent", y = "Average Church Attendance", title = "Has the Life Cycle Effect Diminished?", caption = "@ryanburge\nData: GSS 1972-2018") +
ggsave("E://life_cycle.png")
gss <- gss %>%
mutate(cohorts = car::recode(birthyr, "1883:1899 = '1883-1899';
1900:1904 = '1900-1904';
1905:1909 = '1905-1909';
1910:1914 = '1910-1914';
1915:1919 = '1915-1919';
1920:1924 = '1920-1924';
1925:1929 = '1925-1929';
1930:1934 = '1930-1934';
1935:1939 = '1935-1939';
1940:1944 = '1940-1944';
1945:1949 = '1945-1949';
1950:1954 = '1950-1954';
1955:1959 = '1955-1959';
1960:1964 = '1960-1964';
1965:1969 = '1965-1969';
1970:1974 = '1970-1974';
1975:1979 = '1975-1979';
1980:1984 = '1980-1984';
1985:1989 = '1985-1989';
1990:1994 = '1990-1994';
1995:2000 = '1995-2000'; else = NA")) %>%
mutate(decades = frcode(age >= 18 & age <= 25 ~ "18\nto\n25",
age >= 26 & age <= 35 ~ "26\nto\n35",
age >= 36 & age <= 45 ~ "36\nto\n45",
age >= 46 & age <= 55 ~ "46\nto\n55",
age >= 56 & age <= 65 ~ "56\nto\n65",
age >= 66 ~ "66\nand\n Older")) %>%
mutate(cohorts = as.factor(cohorts)) %>% mutate(years = frcode(year >= 1972 & year <= 1979 ~ "1970's",
year >= 1980 & year <= 1989 ~ "1980's",
year >= 1990 & year <= 1999 ~ "1990's",
year >= 2000 & year <= 2009 ~ "2000's",
year >= 2010 & year <= 2019 ~ "2010's"))
graph <- gss %>%
group_by(cohorts, decades) %>%
mean_ci(attend, wt = wtssall) %>%
ungroup(cohorts, decades)
graph %>%
filter(cohorts != "NA") %>%
filter(cohorts != "1883-1899") %>%
ggplot(., aes(x = decades, y = mean, group = 1)) +
geom_point(shape = 21) +
geom_line(aes(color = cohorts)) +
theme_gg("Abel") +
scale_colour_tableau(palette = "Tableau 20") +
scale_fill_tableau(palette = "Tableau 20") +
facet_wrap(~ cohorts) +
geom_ribbon(aes(ymin = lower, ymax = upper, color = cohorts, fill = cohorts), alpha = .4) +
scale_y_continuous(breaks = c(0,1,2,3,4,5,6,7,8), labels = c("Never","Less than Once a Year", "Once a Year","Several Times a Year","Once a Month","2-3x a Month","Nearly Every Week", "Every Week","More than Once a Week")) +
labs(x = "Age of Respondents", y = "Average Church Attendance", title = "Cohort Analysis of the Life Cycle Effect", caption = "GSS 1972-2018") +
ggsave("E://lifecycle_effect.png", type = "cairo-png", width = 10, height = 10)
life <- tribble(
~age, ~attend, ~group,
"Under 18", 7, "Option 1",
"Young Adulthood", 3, "Option 1",
"Married w/Children", 7, "Option 1",
"Empty Nest", 4.5, "Option 1",
"Retirement", 3.5, "Option 1",
"Under 18", 7, "Option 2",
"Young Adulthood", 3, "Option 2",
"Married w/Children", 7, "Option 2",
"Empty Nest", 8, "Option 2",
"Retirement", 9, "Option 2",
)
life <- life %>%
mutate(age = as.factor(age))
life$age <- factor(life$age, levels = c("Under 18", "Young Adulthood", "Married w/Children", "Empty Nest", "Retirement"))
life %>%
ggplot(., aes(x = age, y = attend, group = group, color = group)) +
geom_point(size=4, color="white") +
geom_point(size=3, shape=1, alpha=.5) +
geom_point(size=2, shape=19) +
theme_gg("Abel") +
geom_line(size = 1) +
scale_color_npg() +
theme(axis.text.y = element_blank(),
axis.ticks.y=element_blank()) +
scale_y_continuous(limits = c(1, 10)) +
labs(x = "", y = "Church Attendance", title = "Visualizing the Life Cycle Effect", caption = "@ryanburge") +
geom_vline(xintercept = 1.5, linetype = "dashed") +
geom_vline(xintercept = 2.5, linetype = "dashed") +
geom_vline(xintercept = 3.5, linetype = "dashed") +
geom_vline(xintercept = 4.5, linetype = "dashed") +
add_text(x = 5.1, y = 4.1, word = "Drifting\nAway", sz = 4) +
add_text(x = 5.1, y = 9.7, word = "Drawing\nCloser", sz = 4) +
add_text(x = 1, y = 9.7, word = "Parents Make\nYou Go", sz = 4) +
add_text(x = 2, y = 9.7, word = "Rebelling +\nFinding Your Way", sz = 4) +
add_text(x = 3, y = 9.7, word = "Want Children\nTo Have\nMoral Foundation", sz = 4) +
add_text(x = 4, y = 9.7, word = "Do I Really\nLike Church?", sz = 4) +
ggsave("E://life_example.png", width = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment