Skip to content

Instantly share code, notes, and snippets.

@jmcinerney14
Created March 3, 2016 21:02
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 jmcinerney14/2630b8b1da2926f68eb3 to your computer and use it in GitHub Desktop.
Save jmcinerney14/2630b8b1da2926f68eb3 to your computer and use it in GitHub Desktop.
Living Arrangements of the Elderly
#load packages
library(ggplot2)
library(RColorBrewer)
library(readr)
library(dplyr)
library(ggplot2)
library(scales)
#Import data
data<- read_csv("usa_00022.csv")
#Remove data from Alaska and Hawaii before 1960
a <- filter(data,STATEFIP < 60 & (YEAR >= 1960 | !(STATEFIP %in% c(2,15))))
#Remove anyone under 65 years old
b <- filter(a,AGE>=65)
#Select the listed variables
c <- select(b,YEAR,SERIAL,GQ,PERWT,FAMSIZE,SEX)
#Create a new variable, Household, and label each individual according to the identfied qualities
d <- mutate(c, Household=ifelse(GQ==1 & FAMSIZE==1,"Alone", ifelse(GQ==1 & FAMSIZE>1, "With Family", ifelse(GQ>=2, "Group Quarters", "Other"))))
#Create a new variable, SEXF, that labels individual male or female
e <- mutate(d,SEXF=factor(SEX,labels=c('Male','Female')))
#Select the listed variables
f <- select(e,YEAR,PERWT,Household,SEXF)
#Create a new column, Population, by summing PERWT for each unique year, sex, and type of household
g <- summarise(group_by(f,YEAR,SEXF,Household),Population=sum(PERWT))
#Graph population over time
h<-ggplot(g, aes(x=YEAR,y=Population,fill=Household)) +
geom_bar(stat='identity')+
facet_grid(.~SEXF)+
labs(title="Living Arrangements of Elderly Americans", x="Year", y='Population')+
scale_x_continuous(breaks=c(1920,1930,1940,1950,1960,1970))+
scale_y_continuous(labels=comma)+
guides(fill = guide_legend(reverse = TRUE,title='Type of Household'))+
scale_fill_brewer(palette="Set1")+
theme_bw()
print(h)
#Sum the population for each year and gender to get total population
i<-summarise(group_by(g,YEAR,SEXF),Total=sum(Population))
#Merge the two data sets to make one data set with population and total population
j<-merge(g,i,by.x=c('YEAR','SEXF'),by.y=c('YEAR','SEXF'))
#Graph percentage over time
k<-ggplot(j, aes(x=YEAR,y=Population/Total,fill=Household)) +
geom_bar(stat="identity")+
labs(title="Proportion of Various Living Arrangements of Elderly Americans", x="Year", y='Percentage')+
scale_x_continuous(breaks=c(1920,1930,1940,1950,1960,1970))+
scale_y_continuous(labels=scales::percent)+
guides(fill = guide_legend(reverse = TRUE,title='Type of Household'))+
scale_fill_brewer(palette="Set1")+
facet_grid(.~SEXF)+
theme_bw()
print(k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment