Skip to content

Instantly share code, notes, and snippets.

@prodhimanisha
Created November 15, 2016 20:10
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 prodhimanisha/27543123e721d522b7d5910d797adb1d to your computer and use it in GitHub Desktop.
Save prodhimanisha/27543123e721d522b7d5910d797adb1d to your computer and use it in GitHub Desktop.
#Loading packages
library(magrittr)
library(dplyr)
library(readr)
library(plotly)
library(ggplot2)
library(RColorBrewer)
library(circlize)
library(tidyr)
#Reading in IPUMS data, males and those identified as white excluded
chord <- read.csv('network.csv')
#Recoding race variable
label <- chord %>% mutate(Race=factor(ifelse(RACE==2,1,
ifelse(RACE==3,2,
ifelse(RACE %in% c(4,5,6),3,
ifelse(RACE %in% c(8,9),4,5)))),
labels=c('Black','Native American','Asian or Pacific Islander','Multiple Major Races','Other')))
#reading in birthplace codebook data from IPUMS and keeping only variables relevant for the visualization
bpl <- read.csv('bpl.csv')
bpl <- bpl %>% select(BPL,Birth)
#Making crosswalk between birthplace codebook labels and IPUMS codes
dat <- left_join(label,bpl)
#narrowing down dataset further
data <- dat %>% select(YEAR,CITIZEN,PERWT,Race,Birth)
#Making separate datsets for those with and without immigrant status
idat <- data %>% filter(CITIZEN %in% c(3,4,5))
inon <- data %>% filter(!(CITIZEN %in% c(3,4,5)))
#Grouping by race and birthplace to aggregate
ib <- idat %>% group_by(Race,Birth) %>% summarise(Num1=sum(PERWT))
ib <- ib %>% na.omit(Num1)
#Creating adjacency matrix by processing data in the correct format
ib <- spread(ib,Birth,Num1)
imat <- data.matrix(ib)
#Assigning matrix row labels and removing redundant column with levels in matrix
rownames(imat)=c('Black','Native American','Asian or Pacific Islander','Multiple Major Races','Other')
imat <- imat[,-1]
#Same as above, but for non-immigrant dataset
nb <- inon %>% group_by(Race,Birth) %>% summarise(Num2=sum(PERWT))
nb <- nb %>% na.omit(Num2)
nb <- spread(nb,Birth,Num2)
nmat <- data.matrix(nb)
rownames(nmat)=c('Black','Native American','Asian or Pacific Islander','Multiple Major Races','Other')
nmat <- nmat[,-1]
#Creating chord diagram for immigrant non-white females
circos.clear()
circos.par(gap.degree=c(rep(2,nrow(imat)-1),5,rep(2,ncol(imat)-1),5))
chordDiagram(imat,directional=1,direction.type=c('diffHeight','arrows'),link.arr.type='big.arrow',annotationTrack='grid',preAllocateTracks=list(track.height=0.3))
circos.trackPlotRegion(track.index = 1, panel.fun =function(x, y) {
xlim =get.cell.meta.data('xlim')
ylim =get.cell.meta.data('ylim')
sector.name =get.cell.meta.data('sector.index')
circos.text(mean(xlim), ylim[1], sector.name, facing = 'clockwise', niceFacing = TRUE, adj =c(0, 0.5),cex=0.55)
}, bg.border = NA)
#Creating chord diagram for non-immigrant non-white females
circos.clear()
circos.par(gap.degree=2.48)
chordDiagram(nmat,directional=1,direction.type=c('diffHeight','arrows'),link.arr.type='big.arrow',annotationTrack='grid',preAllocateTracks=list(track.height=0.3))
circos.trackPlotRegion(track.index = 1, panel.fun =function(x, y) {
xlim =get.cell.meta.data('xlim')
ylim =get.cell.meta.data('ylim')
sector.name =get.cell.meta.data('sector.index')
circos.text(mean(xlim), ylim[1], sector.name, facing = 'clockwise', niceFacing = TRUE, adj =c(0, 0.5),cex=0.55)
}, bg.border = NA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment