-
-
Save SirSamAlot280/b6da56fcc8b2952e5fdc90c3a5d2b6ac to your computer and use it in GitHub Desktop.
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
#Load packages | |
library(readr) | |
library(dplyr) | |
library(ggplot2) | |
library(RColorBrewer) | |
library(ggmap) | |
library(maptools) | |
library(gtools) | |
library('gganimate') | |
#Read in selected IPUMS data | |
ipums <- read_csv('./data/Great_Migration.csv') | |
#Filter out 1950 as only MIGPLAC1 and not MIGPLAC5 is available for this data set | |
ipums2 <- ipums %>% filter(!YEAR %in% c(1950)) | |
#Determine which states are considered Southern | |
south <- c(1,5,10,11,12,13,21,22,24,28,37,40,45,47,48,51,54) | |
#Include only individuals who are racially identified as Black | |
black <- ipums2 %>% filter(RACESING %in% c(2) & STATEFIP %in% | |
south & MIGPLAC5>0 & MIGPLAC5<=56 & !(MIGPLAC5 %in% south)) | |
#Create a variable to distinguish between Males and Females | |
blackgendered <- black %>% mutate(Sex=ifelse(SEX==1, 'Male','Female')) | |
#Distinguish between education levels for individuals over the age of 18. | |
educationlvl <-blackgendered %>% select(YEAR,EDUCD,AGE,PERWT) %>% filter(AGE>=18) %>% mutate(Education=factor(ifelse(EDUCD>=0 & EDUCD<=59,'Less than high school', | |
ifelse(EDUCD %in% c(60:64),'High school diploma or GED', | |
ifelse(EDUCD %in% c(65,70,71,80,81,82,83,90,100,110,111,112,113),'Some College', | |
ifelse(EDUCD %in% c(101),'Bachelor Degree', | |
ifelse(EDUCD %in% c(114,115,116),'Advanced Degree','Missing'))))))) %>% filter(Education != 'Missing') | |
educationlvl$Education <- factor(educationlvl$Education,levels=c('Less than high school','High school diploma or GED','Some College','Bachelor Degree', 'Advanced Degree')) | |
#Group for only the informaton necessary to see the education attainment for each year | |
#and weigh. | |
educationlvl2 <- educationlvl %>% group_by(YEAR,Education) %>% | |
summarise(People=sum(PERWT)) | |
#Distinguish between family size, focus on only households, and filter out for non-movers | |
familysize <- blackgendered %>% select(YEAR,SERIAL,AGE,NCHILD,HHWT,GQ,PERWT) %>% | |
filter(GQ==1) %>% mutate(n=1) | |
# (DO I NEED HHWT?) Include the necessary variables to create data frames for number of children and familysize | |
familysize2 <- familysize %>% group_by(YEAR,SERIAL,HHWT) %>% mutate(FAMSIZE=sum(n)) %>% | |
mutate(Family=ifelse(FAMSIZE==1,'Single','Family')) | |
#Group only the information needed to see the family size for blacks migrants. | |
familysize3 <- familysize2 %>% group_by(YEAR,Family) %>% summarise(Household=sum(PERWT)) | |
#Distinguish if a family consists of children | |
familywchild <- familysize %>% filter(AGE>=18) %>% | |
mutate(Children=ifelse(NCHILD==0,'No children','One or more children')) | |
#Group only the information needed to see the number of children black migrants had in their housheold. | |
familywchild2 <- familywchild %>% group_by(YEAR,Children) %>% | |
summarise(Household=sum(PERWT)) | |
#Establish age categories and floor for population pyramid | |
agecat <- blackgendered %>% select(YEAR,AGE,Sex,PERWT) %>% | |
mutate(Age=ifelse(AGE>=80,8,floor(AGE/10))) | |
#Assign specific ranges for the age categories | |
rangeagecat <- agecat %>% mutate(Age=factor(Age,labels=c('0-9','10-19','20-29', | |
'30-39','40-49','50-59','60-69','70-79','80+'))) | |
#Separate Male and Female for the population Pyramid | |
agesexsep1 <- rangeagecat %>% group_by(Age,Sex,YEAR) %>% summarise(Number=sum(PERWT)) | |
#Separate Male and Female for the population Pyramid | |
agesexsep1$Population <- ifelse(agesexsep1$Sex=='Male',-1*agesexsep1$Number,agesexsep1$Number) | |
#Aggregate to focus on blacks living in a non-southern state five years ago | |
blackbpl <- black %>% mutate(Birth=ifelse(BPL %in% south, 'South','Non-South')) %>% | |
group_by(YEAR,Birth) %>% summarise(Number=sum(PERWT)) | |
#Aggregate for birthplace and year for black population migrating to the SOuth | |
blackgroup <- black %>% filter(BPL<=56) %>% group_by(YEAR,BPL) %>% | |
summarise(Number=sum(PERWT)) | |
#Aggregate to create new categories | |
blackgroup2 <- blackgroup %>% mutate(Population=factor(ifelse(Number<250,1, | |
ifelse(Number<1000,2, | |
ifelse(Number<4500,3, | |
ifelse(Number<20000,4,5)))))) | |
levels(blackgroup2$Population) <- c('1-250','251-999','1,000-4,499','4,500-19,999','20,000+') | |
#Create Population Pyramid for Black Population | |
png('Black_Population_Pyramids.png',height=750,width=2000) | |
ggplot(data=agesexsep1,aes(x=Age,y=Population,fill=Sex)) + | |
geom_bar(data=agesexsep1[agesexsep1$Sex=='Male',],stat='identity') + | |
geom_bar(data=agesexsep1[agesexsep1$Sex=='Female',],stat='identity') + | |
coord_flip() + | |
facet_wrap(~YEAR) + | |
scale_y_continuous(breaks=c(-50000,-25000,0,25000,50000), | |
labels=c('0.05','0.025','0','0.025','0.05')) + | |
labs(y='Population in Thousands',title='Population Pyramids for Southern-born African-Americans Migrating Back to the South, 1940,1960-2000') + | |
scale_fill_brewer(palette ='Set1',guide=guide_legend(reverse=TRUE))+ | |
theme_bw() + theme(legend.position='bottom') | |
dev.off() | |
#Create bar graph for blacks moving to the South who where born in Southern | |
#and non-Southern states | |
png('Black_Population_Birthplace.png',height=500,width=1000) | |
graph1 <- ggplot(blackbpl, aes(x=YEAR,y=Number,fill=Birth))+ | |
geom_bar(stat='identity') + | |
labs(fill='Where People are Born',title='Number of African-Americans Born in a Southern or Non-Southern State Migrating to the South, 1940,1960-2000',x='YEAR',y='Number of African-Americans') + | |
scale_fill_brewer(palette ='Set1')+ | |
scale_y_continuous(labels=scales::comma) | |
print(graph1) | |
dev.off() | |
#Create bar graph for black migrating by themselves or with family | |
png('Black_Population_Family.png',height=500,width=1000) | |
graph2 <- ggplot(familysize3, aes(x=YEAR,y=Household,fill=Family)) + | |
geom_bar(stat='identity') + | |
labs(fill='',title='The Number of African-American Households Migrating to the South as Single or a Family, 1940,1960-2000',x='YEAR',y='Number of African-Americans Households') + | |
scale_fill_brewer(palette ='Set1')+ | |
scale_y_continuous(labels=scales::comma) | |
print(graph2) | |
dev.off() | |
#Create bar graph for households with and without children | |
png('Black_Population_Children.png',height=500,width=1000) | |
graph3 <- ggplot(familywchild2, aes(x=YEAR,y=Household,fill=Children))+ | |
geom_bar(stat='identity') + | |
labs(fill='',title='Number of African-Americans Families Moving to the South With and Without Children, 1940,1960-2000',x='YEAR',y='Number of African-American Households') + | |
scale_fill_brewer(palette ='Set1')+ | |
scale_y_continuous(labels=scales::comma) | |
print(graph3) | |
dev.off() | |
#Create bar graph for educational attainment for African-Americans | |
png('Black_Population_Education.png',height=500,width=1000) | |
educationlvl2$Education <- factor(educationlvl2$Education, levels = c('Advanced Degree','Bachelor Degree','Some College','High school diploma or GED','Less than high school')) | |
graph4 <- ggplot(educationlvl2, aes(x=YEAR,y=People,fill=Education))+ | |
geom_bar(stat='identity') + | |
labs(fill='Education Level',title='Educational Attainment of African-Americans Moving to the South, 1940,1960-2000',x='YEAR',y='Number of African-American Households') + | |
scale_fill_brewer(palette ='Set1')+ | |
scale_y_continuous(labels=scales::comma) | |
print(graph4) | |
dev.off() | |
#Create bar graph for the number of children in migrating black households | |
#Read in maps | |
mapdata <- read_csv('data/map.csv',col_types=cols(STATEFIP=col_integer())) | |
#Create a map of the United States | |
map1 <- ggplot() + theme_nothing(legend=TRUE) + | |
geom_polygon(data=mapdata, aes(x=long,y=lat,group=group),fill='white',color='black') | |
png('map.png',width=1500,height=1000) | |
print(map1) | |
dev.off() | |
#Join data to the map | |
blackgroupjoin <- left_join(blackgroup2,mapdata, by=c('BPL'='STATEFIP')) | |
#Combine every map year produce and animate | |
map2 <- map1 + scale_fill_brewer(palette = 'Blues') + | |
geom_polygon(data=blackgroupjoin,aes(x=long,y=lat,group=group,fill=Population,frame=YEAR),color='black') + | |
labs(title='The Birthplace of African-Americans Moving to the South, ') | |
gg_animate(map2,ani.width=1500,ani.height=1000,'blackmigmap1940,1960-2000.gif') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment