Skip to content

Instantly share code, notes, and snippets.

@kszela24
Created May 1, 2016 16:01
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/c55fe428f614f790d2405e7612098426 to your computer and use it in GitHub Desktop.
Save kszela24/c55fe428f614f790d2405e7612098426 to your computer and use it in GitHub Desktop.
#Subset the census data by full-time, sector, and sex.
ratio_class_of_worker = census[!(census$class.of.worker == "Not in universe" |
census$class.of.worker == "Without pay"),]
ratio_class_of_worker = ratio_class_of_worker[(ratio_class_of_worker$full.or.part.time.employment.stat == "Full-time schedules"),]
ratio_class_of_worker = group_by(ratio_class_of_worker, class.of.worker, sex) %>%
summarise(.,
count_geq50 = sum(instance.weight[X == "50000+." & age >= 18]),
count_l50 = sum(instance.weight[X == "-50000" & age >= 18]),
pct_geq50 = (count_geq50 / (count_l50 + count_geq50)) * 100)
#Plot graph.
classPlot_sex = ggplot(ratio_class_of_worker, aes(x = class.of.worker, y = pct_geq50)) +
geom_bar(stat = "identity", aes(fill = class.of.worker)) +
scale_fill_brewer(name = "Class of Worker", palette = "Set1") +
#theme_bw() +
ggtitle("Income by Sector 94' - 95'") +
xlab("") +
ylab("Percent of Sector with Full-time Income >50k") +
theme(axis.ticks = element_blank(), axis.text.x = element_blank()) +
facet_wrap( ~ sex)
classPlot_sex
######
#Subset data by similarly, but for population percentage measurements.
ratio_class_of_worker_population = census[!(census$class.of.worker == "Not in universe" |
census$class.of.worker == "Without pay"),]
ratio_class_of_worker_population = ratio_class_of_worker_population[(ratio_class_of_worker_population$full.or.part.time.employment.stat == "Full-time schedules"),]
#Tally the total of males and females.
sector_totals = summarise(ratio_class_of_worker_population,
Both = sum(instance.weight),
Males = sum(instance.weight[sex == "Male"]),
Females = sum(instance.weight[sex == "Female"]))
#Get percentages of population per sector.
sector_numbers_males = group_by(ratio_class_of_worker_population, class.of.worker, sex) %>%
summarise(.,
pct = (sum(instance.weight[sex == "Male"]) / sector_totals$Males) * 100)
sector_numbers_females = group_by(ratio_class_of_worker_population, class.of.worker, sex) %>%
summarise(.,
pct = (sum(instance.weight[sex == "Female"]) / sector_totals$Females) * 100)
sector_numbers_males$pct = sector_numbers_males$pct + sector_numbers_females$pct
#Male/female side by side plot.
sectorPlot_mf = ggplot(sector_numbers_males, aes(x = class.of.worker, y = pct)) +
geom_bar(stat = "identity", aes(fill = class.of.worker)) +
scale_fill_brewer(name = "Class of Worker", palette = "Set1") +
#theme_bw() +
ggtitle("Sector of Population 94' - 95'") +
xlab("") +
ylab("Percent of Population in Sector") +
theme(axis.ticks = element_blank(), axis.text.x = element_blank()) +
facet_wrap( ~ sex)
sectorPlot_mf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment