Skip to content

Instantly share code, notes, and snippets.

@kszela24

kszela24/age.R Secret

Created May 1, 2016 16:58
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 kszela24/b52bd38e5d395ccbe46e1f0edaaf89e2 to your computer and use it in GitHub Desktop.
Save kszela24/b52bd38e5d395ccbe46e1f0edaaf89e2 to your computer and use it in GitHub Desktop.
#Subset census by those with full-time jobs and are 18 or above.
ratio_age_census = census[(census$full.or.part.time.employment.stat == "Full-time schedules" &
census$age >= 18 &
census$age <= 70),]
#Get percentage of each age with selected income.
#Both
ratio_age = group_by(ratio_age_census, age) %>%
summarise(.,
Both = (sum(instance.weight[X == "50000+." & age >= 18]) /
(sum(instance.weight[X == "50000+." & age >= 18]) +
sum(instance.weight[X == "-50000" & age >= 18]))) * 100)
#Just males
ratio_age_male = group_by(ratio_age_census, age) %>%
summarise(.,
Males = (sum(instance.weight[X == "50000+." & sex == "Male" & age >= 18]) /
(sum(instance.weight[X == "50000+." & sex == "Male" & age >= 18]) +
sum(instance.weight[X == "-50000" & sex == "Male" & age >= 18]))) * 100)
#Just females
ratio_age_female = group_by(ratio_age_census, age) %>%
summarise(.,
Females = (sum(instance.weight[X == "50000+." & sex == "Female" & age >= 18]) /
(sum(instance.weight[X == "50000+." & sex == "Female" & age >= 18]) +
sum(instance.weight[X == "-50000" & sex == "Female" & age >= 18]))) * 100)
#Inner join all three together.
ratio_age = inner_join(ratio_age, ratio_age_male, by = "age")
ratio_age = inner_join(ratio_age, ratio_age_female, by = "age")
#Stack them for ggplot2.
ratio_age_stacked = with(ratio_age,
data.frame(value = c(Both, Males, Females),
variable = factor(rep(c("Both", "Males", "Females"),
each = NROW(ratio_age))),
age = rep(age, 3)))
#Plot graph.
agePlot = ggplot(ratio_age_stacked, aes(age, value, colour = variable)) +
geom_line() +
theme(legend.title=element_blank()) +
ggtitle("Income by Age 94' - 95'") +
xlab("Age (years)") +
ylab("Percent at Age with Full-time Income >50k")
agePlot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment