Created
September 30, 2016 13:19
-
-
Save morganmelon/59e263c8217b3dffbd75ca8b03a1b96d to your computer and use it in GitHub Desktop.
Making column graphs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Lab3 | |
#Morgan Waterman | |
#Column graphs | |
library(dplyr) | |
library(readr) | |
library(ggplot2) | |
library(RColorBrewer) | |
data <- read_csv('./data/Lab3Newest.csv') %>% | |
filter(AGE>15 & AGE<65 & !(STATEFIP %in% c(2,15))) | |
table(data$RACE) | |
#Recode race | |
datab <-data %>% mutate(Race = factor(ifelse(RACE %in% c(1), 1, | |
ifelse(RACE %in% c(2), 2, | |
ifelse(RACE %in% c(3), 3, 4))), | |
labels=c('white', 'black', 'Native American', 'Asian'))) | |
#Recode sex | |
datac <- datab %>% mutate(Sex = ifelse(SEX==1, 'male', 'female')) | |
table(datac$Occupation) | |
#Create Occupation | |
datad <- datac %>% mutate(Occupation = factor(ifelse(OCC1950<100, 6, | |
ifelse(OCC1950>970, 1, | |
ifelse(OCC1950>900 | (OCC1950>500 & OCC1950<700), 3, | |
ifelse(OCC1950==100 | OCC1950==123 | OCC1950 > 800, 2, | |
ifelse(OCC1950<500, 4, 5))))), | |
labels = c("none", "farmers and farm laborers", "craftsmen/operatives/laborers", "managerial/clerical/sales", "service", "professional"))) | |
#Select for only the variables I want in Fig 2 | |
#YEAR, Race, Sex, PERWT | |
datae <- datad %>% select(YEAR, PERWT, Race, Occupation, Sex) | |
#Making 2 graphs | |
f2 <- datae %>% group_by(YEAR, Sex, Race) %>% summarise(Number = sum(PERWT)) | |
f4 <- datae %>% group_by(YEAR, Sex, Race, Occupation) %>% summarise(Number = sum(PERWT)) | |
#graphing fig 2 | |
ggplot(data = f2, aes(x = YEAR, y = Number, fill = Sex)) + | |
geom_bar(stat = 'identity') + | |
labs(x = 'Year', y = 'Population', fill = 'Sex', title = '2. Population Aged 15-65 by Race, Year, and Sex, 1870-1920') + | |
scale_y_continuous(labels=scales::comma) + | |
scale_x_continuous(breaks=c(1870, 1900, 1920)) + | |
scale_fill_brewer(palette='Set2',guide=guide_legend (reverse = TRUE)) + | |
facet_wrap(~Race, ncol=2, scales = 'free_y') + | |
theme_bw() | |
#graphing fig 3 | |
ggplot(data = f4, aes(x = YEAR, y = Number, fill = Occupation)) + | |
geom_bar(stat = 'identity', position = 'fill') + | |
labs(x = 'Year', y = 'Percent of Population', fill = 'Occupation', title = '4. Occupation of Persons Aged 15-65 by Sex, Race, and Year 1870-1920') + | |
scale_y_continuous(labels=scales::percent) + | |
scale_x_continuous(breaks=c(1870, 1900, 1920)) + | |
scale_fill_brewer(palette='Set1') + | |
facet_grid(Sex~.~Race) + | |
theme_bw() + theme(legend.position = 'bottom') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment