Skip to content

Instantly share code, notes, and snippets.

/Figure 2

Created Nov 14, 2016
Embed
What would you like to do?
FP Figure 2
#FP Figure 2
#load packages
library(readr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(scales)
#read csv
csv <- read_csv("~/Desktop/usa_00028.csv")
#only women and older than 22
csv_new <- csv %>% filter(SEX==2 & AGE>=22)
#recode race
race_grouped <- csv_new %>% mutate(Race_2=factor(ifelse(HISPAN>0,1,
ifelse(RACESING==1,2,
ifelse(RACESING==2,3,
ifelse(RACESING==3,4,
ifelse(RACESING==4,5,6)))))))
levels(race_grouped$Race_2) <- c('Hispanic','White','Black','Native American','Asian','Other')
#filter out 'other' category
race_grouped2 <- race_grouped %>% filter(Race_2!='Other')
#make marriage variable
mstatus <- race_grouped2 %>% mutate(MStatus=ifelse(MARST==6,'Unmarried','Married'))
#make degree/no degree variable
educ <- mstatus %>% mutate(EducLev=ifelse(EDUCD>=101 & EDUCD<=116, 'Degree', 'No Degree'))
#calculate number with degrees
num <- educ %>% group_by(YEAR, EducLev, MStatus, Race_2) %>% summarise(num=sum(PERWT))
#calculate total number
total_num <- educ %>% group_by(YEAR, MStatus, Race_2) %>% summarise(tnum=sum(PERWT))
#join
joined <- left_join(total_num,num)
#calc percent
percentages <- joined %>% mutate(pct=num/tnum*100)
# plot the percent with a college degree
percentages <- percentages %>% filter(EducLev=='Degree')
#plot it, line
ggplot(percentages,aes(x=YEAR,y=pct,color=Race_2)) +
geom_line() + geom_point() +
facet_grid(MStatus~.) +
labs(x='Year',y='Percent',fill='Sex',title='Percent of Women over 22 with a College Degree by Race, 1940-2000') +
scale_y_continuous(labels=scales::comma) +
scale_x_continuous(breaks=c(1940,1960,1980,2000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.