Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Last active March 12, 2016 01:43
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 anonymous/2bccd7e382ad1aeb7f61 to your computer and use it in GitHub Desktop.
Save anonymous/2bccd7e382ad1aeb7f61 to your computer and use it in GitHub Desktop.
#Code for Project 4
#install packages - this only needs to run once per machine
install.packages('readr')
install.packages('dplyr')
install.packages('ggplot2')
install.packages('scales')
install.packages('grid')
#load packages
library(readr)
library(dplyr)
library(ggplot2)
library(scales)
library(grid)
#set working directory
setwd('/Users/robertmoffitt/Desktop/R Code')
#read in data
a <- read.csv('usa_00013.csv')
#select year, perwt, sex, race and region
b <- select(a,YEAR,PERWT,AGE,RACE,REGION,MARST)
#Age categories using floor function
c <- mutate(b,AGECAT=ifelse(AGE >= 70,20,floor(AGE/50)))
#Create age categories
AGEC <- c('20-69')
#Attach labels fo AGECAT
d <- mutate(c,AGECAT=factor(AGECAT,labels=AGEC))
#factor RACE variable to just white and black
e <- mutate(d,Race=ifelse(RACE==1,'white',ifelse(RACE==2,'black','other')))
#create new variable for region
f <- mutate(e,Region=ifelse(REGION<=13,'Northeast',ifelse(REGION<=23,'Midwest',ifelse(REGION<=34,'South',ifelse(REGION<=43,'West','other')))))
#take out regions I am not looking at
g <- filter(f,Region!='other')
#Create categories for marital status
h <- mutate(g,MaritalStatus=ifelse(MARST<=2,'Married',ifelse(MARST<=4,'Separated or Divorced',ifelse(MARST<=5,'Widowed','Never Married'))))
#filter out the other race category
i <- filter(h,Race!='other')
#select new variables
j <- select(i,YEAR,PERWT,AGECAT,Race,Region,MaritalStatus)
#sum across unique combinations of year, sex, race, region and marital status
k <- summarise(group_by(j,YEAR,AGECAT,Race,Region,MaritalStatus),NUMBER=sum(PERWT))
#Graph with race as the filled variable, and by gender and region
l <- ggplot(k,aes(x=YEAR,y=NUMBER,fill=MaritalStatus)) +
geom_bar(stat='identity') +
facet_grid(Race~.~Region)
print(l)
#Graph as percents
l <- ggplot(k,aes(x=YEAR,y=NUMBER,fill=MaritalStatus)) +
geom_bar(stat='identity',position="fill") +
facet_grid(Race~.~Region) +
scale_y_continuous(labels = scales::percent)
print(l)
#Add labels to the Graphs
m <- l + labs(title='Population by Race, Region, and Marital Status',x='Year',y='Number')
print(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment