Skip to content

Instantly share code, notes, and snippets.

Created November 14, 2016 16:12
Show Gist options
  • Save anonymous/8a60a5d7e59815b3799377fe8e2acca3 to your computer and use it in GitHub Desktop.
Save anonymous/8a60a5d7e59815b3799377fe8e2acca3 to your computer and use it in GitHub Desktop.
FP - Figure 3
#FP Figure 3
#load packages
library(readr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(scales)
#read csv
csv <- read_csv("~/Desktop/usa_00028.csv")
#only women 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')
#create marriage status variable
edu_status <- race_grouped %>% mutate(Status=ifelse(MARST==6,'Unmarried','Married'))
#total number
total_num <- edu_status %>% group_by(YEAR, Race_2) %>% summarise(tnum=sum(PERWT))
#number married
married_number <- edu_status %>% group_by(YEAR, Race_2, Status) %>% summarise(mnum=sum(PERWT))
#join the two
joined <- left_join(total_num,married_number,by=c('YEAR'='YEAR','Race_2'='Race_2'))
#calculate percent
percentages <- joined %>% mutate(pct=mnum/tnum)
#graph
ggplot(data=percentages,aes(x=YEAR, y=pct, fill=Status)) +
geom_bar(stat='identity') +
labs(x='Year',y='Percent',fill='Marital Status', title='Marriage Rate by Race for Women over 20, 1940-2000') +
scale_y_continuous(labels=scales::percent) +
theme_bw(base_size=22) +
scale_fill_brewer(palette='Set2',guide=guide_legend(reverse=TRUE)) +
facet_wrap(~Race_2,ncol=6)+
scale_x_continuous(breaks=c(1950,1970,1990))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment