Friday, August 22, 2014

Code to Parse Census County and Block Group data

Code to Parse Census County and Block Group data. Political piece here and here.
# Code to Parse Census County and Block Group data from https://www.census.gov/rdo/data/voting_age_population_by_citizenship_and_race_cvap.html
jpeg_create <- function() {
 systime <- as.numeric(Sys.time())
 # dev.new()
 jpeg(filename = systime,
          width = 1024, height = 768, units = "px", pointsize = 12,
          quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
 Sys.sleep(2)
   }

setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
Groups <- as.matrix(unique(BlockGr$LNTITLE))

 Groups
      [,1]                                                            
 [1,] "Total"                                                         
 [2,] "Not Hispanic or Latino"                                        
 [3,] "American Indian or Alaska Native Alone"                        
 [4,] "Asian Alone"                                                   
 [5,] "Black or African American Alone"                               
 [6,] "Native Hawaiian or Other Pacific Islander Alone"               
 [7,] "White Alone"                                                   
 [8,] "American Indian or Alaska Native and White"                    
 [9,] "Asian and White"                                               
[10,] "Black or African American and White"                           
[11,] "American Indian or Alaska Native and Black or African American"
[12,] "Remainder of Two or More Race Responses"                       
[13,] "Hispanic or Latino"                    
 for(i in Groups) {print(i)}
[1] "Total"
[1] "Not Hispanic or Latino"
[1] "American Indian or Alaska Native Alone"
[1] "Asian Alone"
[1] "Black or African American Alone"
[1] "Native Hawaiian or Other Pacific Islander Alone"
[1] "White Alone"
[1] "American Indian or Alaska Native and White"
[1] "Asian and White"
[1] "Black or African American and White"
[1] "American Indian or Alaska Native and Black or African American"
[1] "Remainder of Two or More Race Responses"
[1] "Hispanic or Latino"
 for(i in Groups) {sum(subset(BlockGr,LNTITLE==i,select=CIT_EST))}
 for(i in Groups) {print(sum(subset(BlockGr,LNTITLE==i,select=CIT_EST)))}
[1] 190180
[1] 178280
[1] 5062
[1] 5535
[1] 1681
[1] 372
[1] 160085
[1] 1841
[1] 1855
[1] 949
[1] 29
[1] 847
[1] 11928

 for(i in Groups) {print(sum(subset(BlockGr,LNTITLE==i,select=CVAP_EST)))}
[1] 149235
[1] 142995
[1] 3654
[1] 4152
[1] 1248
[1] 372
[1] 130320
[1] 1255
[1] 1139
[1] 439
[1] 14
[1] 396
[1] 6255

setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
t1 <- NULL
t2 <- NULL
Groups <- as.matrix(unique(BlockGr$LNTITLE))
for(i in Groups) {t1 <- rbind(sum(subset(BlockGr,LNTITLE==i,select=CIT_EST)),t1)}
for(i in Groups) {t2 <- rbind(sum(subset(BlockGr,LNTITLE==i,select=CVAP_EST)),t2)}
t3 <- data.frame(cbind(rev(Groups),t1,t2),stringsAsFactors = FALSE)
colnames(t3) <- c("Race.Ethnicity","CIT_EST","CVAP_EST")
t4 <- data.frame(t3,"CIT.CVAP.DIFF"=(as.numeric(t3$CIT_EST) - as.numeric(t3$CVAP_EST)),"CVAP.CIT.PCT"=(as.numeric(t3$CVAP_EST) / as.numeric(t3$CIT_EST)))
t4$CIT_EST <- as.numeric(t4$CIT_EST)
t4$CVAP_EST <- as.numeric(t4$CVAP_EST)
arrange(t4,desc(CIT_EST))

                                                   Race.Ethnicity CIT_EST CVAP_EST CIT.CVAP.DIFF CVAP.CIT.PCT
1                                                           Total  190180   149235         40945    0.7847040
2                                          Not Hispanic or Latino  178280   142995         35285    0.8020810
3                                                     White Alone  160085   130320         29765    0.8140675
4                                              Hispanic or Latino   11928     6255          5673    0.5243964
5                                                     Asian Alone    5535     4152          1383    0.7501355
6                          American Indian or Alaska Native Alone    5062     3654          1408    0.7218491
7                                                 Asian and White    1855     1139           716    0.6140162
8                      American Indian or Alaska Native and White    1841     1255           586    0.6816947
9                                 Black or African American Alone    1681     1248           433    0.7424152
10                            Black or African American and White     949      439           510    0.4625922
11                        Remainder of Two or More Race Responses     847      396           451    0.4675325
12                Native Hawaiian or Other Pacific Islander Alone     372      372             0    1.0000000
13 American Indian or Alaska Native and Black or African American      29       14            15    0.4827586

library(sqldf)
# Block Group Post
groups <- noquote(sQuote(unique(BlockGr$LNTITLE)))
sqldf("Select GEONAME,GEOID,LNTITLE,Sum(CIT_EST) AS SUMCIT,Sum(CVAP_EST) AS SUMCVAP from BlockGr where LNTITLE = 'Total' Group By GEONAME,GEOID,LNTITLE ORDER By SUMCIT DESC LIMIT 10")

# Block Group Post
setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
x <- NULL
groups <- sort(rev(unique(BlockGr$LNTITLE)))
as.matrix(rev(groups))
for(i in groups) {x <- rbind(with((subset(BlockGr,LNTITLE == i)),(xtabs(CIT_EST ~ LNTITLE + GEOID ))),x)}
x <- as.data.frame(t(x)); colnames(x) <- c(1:13)

x <- data.frame(x, "Hispanic"=rowSums(x[c(6)]), "Hispanic.PCT"=rowSums(x[c(6)]) / rowSums(x[2]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$Hispanic,names.arg=c(1:nrow(x)),ylim=c(0,600),col=rgb(0,0,1,.95))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Hispanic or Latino' (in BLUE).",side=1,line=2)
mtext("Whatcom 2012 Census CVAP ACS (2010 Block Group Data).",side=1,line=3)
sqldf("Select GEONAME,GEOID,LNTITLE,Sum(CIT_EST) AS SUMCIT,Sum(CVAP_EST) AS SUMCVAP from BlockGr where LNTITLE = 'Hispanic or Latino' Group By GEONAME,GEOID,LNTITLE ORDER By SUMCIT DESC LIMIT 10")

x <- data.frame(x, NW=rowSums(x[c(3,5:13)]), "PCT_NW"=rowSums(x[c(3,5:13)]) / rowSums(x[2]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$NW,names.arg=c(1:nrow(x)),ylim=c(0,2200),col=rgb(1,0,0,.65))
mtext("Whatcom County Block Group distribution of citizens identifying with at least one race not 'White'(in Salmon).", side=1,line=2)
mtext("Whatcom 2012 Census CVAP ACS (2010 Block Group Data).",side=1,line=3)
sqldf("Select GEOID,GEONAME,Sum(CIT_EST) AS SUMCIT,Sum(CVAP_EST) AS SUMCVAP from BlockGr where (LNTITLE !=  'Total') AND (LNTITLE != 'White Alone') AND (LNTITLE != 'Not Hispanic or Latino') Group By GEOID,GEONAME having (SUM(CIT_EST) != 0) or (SUM(CVAP_EST) != 0) ORDER By SUMCIT DESC LIMIT 10")

# Block Group Post ; all four together
setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
x <- NULL
groups <- sort(rev(unique(BlockGr$LNTITLE)))
as.matrix(rev(groups))
for(i in groups) {x <- rbind(with((subset(BlockGr,LNTITLE == i)),(xtabs(CIT_EST ~ LNTITLE + GEOID ))),x)}
x <- as.data.frame(t(x)); colnames(x) <- c(1:13)

x <- data.frame(x, "Hispanic"=rowSums(x[c(6)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$Hispanic,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(1,0,0,.95))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Hispanic or Latino' (in RED).",side=1,line=4)
par(new=T)

x <- data.frame(x, "NotWhite"=rowSums(x[c(3,5:13)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$NotWhite,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(1,0,1,.65))
mtext("Whatcom County Block Group distribution of citizens identifying with at least one race not 'White'(in PINK).", side=1,line=3)
par(new=T)

x <- data.frame(x,"White"=rowSums(x[c(1)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$White,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(1,0,0,.35))
mtext("Whatcom County Block Group distribution of citizens identifying as 'White'(in SALMON).", side=1,line=2)
par(new=T)

x <- data.frame(x, "AllCitizens"=rowSums(x[c(2)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$AllCitizens,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(.1,0,0,.15))
mtext("Whatcom 2012 Census CVAP ACS Overlay (not stacked).",side=3,line=3)
mtext("Whatcom County Block Group distribution of citizens (in GREY).",side=3,line=2)
par(new=F)

# all four together Cumulative Distribution
x <- data.frame(x, "Hispanic"=rowSums(x[c(6)]))
x <- x[with(x,order(Hispanic,decreasing=TRUE)),]
barplot(x$X6,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(0,0,1,.95))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Hispanic or Latino' (in RED).",side=1,line=4)
par(new=T)

x <- data.frame(x, "NotWhite"=rowSums(x[c(3,5:13)]))
x <- x[with(x,order(NotWhite,decreasing=TRUE)),]
barplot(x$NotWhite,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(1,0,0,.65))
mtext("Whatcom County Block Group distribution of citizens identifying with at least one race not 'White'(in ORANGE).", side=1,line=3)
par(new=T)

x <- data.frame(x,"White"=rowSums(x[c(1)]))
x <- x[with(x,order(White,decreasing=TRUE)),]
barplot(x$X1,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(1,0,0,.45))
mtext("Whatcom County Block Group distribution of citizens identifying as 'White'(in SALMON).", side=1,line=2)
par(new=T)

x <- data.frame(x, "AllCitizens"=rowSums(x[c(2)]))
x <- x[with(x,order("AllCitizens",decreasing=TRUE)),]
barplot(x$X2,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(.1,0,0,.25))
mtext("Whatcom 2012 Census CVAP ACS Overlay (not stacked).",side=3,line=3)
mtext("Whatcom County Block Group distribution of citizens (in GREY).",side=3,line=2)
par(new=F)

# all five together "Alone" together plus "Not White Alone"
rev(groups)

setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
x <- NULL
groups <- sort(rev(unique(BlockGr$LNTITLE)))
as.matrix(rev(groups))
for(i in groups) {x <- rbind(with((subset(BlockGr,LNTITLE == i)),(xtabs(CIT_EST ~ LNTITLE + GEOID ))),x)}
x <- as.data.frame(t(x)); colnames(x) <- c(1:13)

x <- data.frame(x, "HispanicLatino"=rowSums(x[c(6)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$HispanicLatino,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(1,0,0,.95))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Hispanic or Latino' (in RED .95).",side=3,line=1)
par(new=T)

x <- data.frame(x, "AsianAlone"=rowSums(x[c(10)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$AsianAlone,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(1,1,0,.75))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Asian Alone' (in RED,GREEN .75).", side=1,line=2)
par(new=T)

x <- data.frame(x,"NativeAlone"=rowSums(x[13]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$NativeAlone,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(0,1,1,.55))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Native American Alone' (in GREEN,BLUE .55).", side=1,line=3)
par(new=T)

x <- data.frame(x,"BlackAlone"=rowSums(x[8]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$BlackAlone,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(0,0,1,.35))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Black Alone' (in BLUE .35).", side=1,line=4)
par(new=T)

x <- data.frame(x, "NotWhite"=rowSums(x[c(3,5:13)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$NotWhite,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(0,0,0,.15))
mtext("Whatcom 2012 Census CVAP ACS Overlay (transparent not stacked). Block Groups in RGB/Alpha ordered by descending All Citizen Totals.",side=3,line=3)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Not White Alone' (in WHITE .15).",side=3,line=2)
par(new=F)

# Testing with add=TRUE
setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
x <- NULL
groups <- sort(rev(unique(BlockGr$LNTITLE)))
as.matrix(rev(groups))
for(i in groups) {x <- rbind(with((subset(BlockGr,LNTITLE == i)),(xtabs(CIT_EST ~ LNTITLE + GEOID ))),x)}
x <- as.data.frame(t(x)); colnames(x) <- c(1:13)

plot.new()
x <- data.frame(x, "HispanicLatino"=rowSums(x[c(6)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$HispanicLatino,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(1,0,0,.95))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Hispanic or Latino' (in RED .95).",side=3,line=1)

x <- data.frame(x, "AsianAlone"=rowSums(x[c(10)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$AsianAlone,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(1,1,0,.75),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Asian Alone' (in RED,GREEN .75).", side=1,line=2)

x <- data.frame(x,"NAAlone"=rowSums(x[13]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$NAAlone,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(0,1,1,.55),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Native American Alone' (in GREEN,BLUE .55).", side=1,line=3)

x <- data.frame(x,"BlackAlone"=rowSums(x[8]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$BlackAlone,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(0,0,1,.35),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Black Alone' (in BLUE .35).", side=1,line=4)

x <- data.frame(x, "NotWhite"=rowSums(x[c(3,5:13)]))
x <- x[with(x,order(X2,decreasing=TRUE)),]
barplot(x$NotWhite,names.arg=c(1:nrow(x)),ylim=c(0,2500),col=rgb(0,0,0,.15),add=TRUE)
mtext("Whatcom 2012 Census CVAP ACS Overlay (transparent not stacked). Block Groups in RGB/Alpha ordered by descending All Citizen Totals.",side=3,line=3)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Not White Alone' (in WHITE .15).",side=3,line=2)

# Testing with add=TRUE
setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
BlockGr <- read.csv("BlockGr_WA_Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
x <- NULL
groups <- sort(rev(unique(BlockGr$LNTITLE)))
as.matrix(rev(groups))
for(i in groups) {x <- rbind(with((subset(BlockGr,LNTITLE == i)),(xtabs(CIT_EST ~ LNTITLE + GEOID ))),x)}
x <- as.data.frame(t(x)); colnames(x) <- c(1:13)
x$GEOID <- gsub('15000US53073','',row.names(x))

plot.new()
jpeg_create()
x <- data.frame(x, "HispanicLatino"=rowSums(x[c(6)]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$HispanicLatino,names.arg=c(1:nrow(x)),ylim=c(0,2000),col=rgb(1,0,0,.95))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Hispanic or Latino' (in RED .95).",side=3,line=1)

x <- data.frame(x, "AsianAlone"=rowSums(x[c(10)]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$AsianAlone,names.arg=c(1:nrow(x)),ylim=c(0,2000),col=rgb(1,1,0,.75),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Asian Alone' (in RED,GREEN .75).", side=1,line=2)

x <- data.frame(x,"NAAlone"=rowSums(x[13]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$NAAlone,names.arg=c(1:nrow(x)),ylim=c(0,2000),col=rgb(0,1,1,.55),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Native American Alone' (in GREEN,BLUE .55).", side=1,line=3)

x <- data.frame(x,"BlackAlone"=rowSums(x[8]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$BlackAlone,names.arg=c(1:nrow(x)),ylim=c(0,2000),col=rgb(0,0,1,.35),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'Black Alone' (in BLUE .35).", side=1,line=4)

x <- data.frame(x,"NoRaceAlone"=rowSums(x[c(3,7,9,11,12)]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$NoRaceAlone,names.arg=c(1:nrow(x)),ylim=c(0,2000),col=rgb(0,0,0,.15),add=TRUE)
mtext("Whatcom County Block Group distribution of citizens identifying as 'No One Race Alone' (in WHITE .15).", side=3,line=2)
mtext("Whatcom 2012 Census CVAP ACS Overlay (transparent not stacked). Block Groups in RGB/Alpha ordered by GEOID.",side=3,line=3)
graphics.off()

plot.new()
jpeg_create()
x <- data.frame(x, "NotWhite"=rowSums(x[c(3,5:13)]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$NotWhite,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(0,0,0,.15))
mtext("Whatcom County Block Group distribution of citizens identifying as 'Not White' or 'Not White Alone' (in WHITE .15).",side=3,line=2)

x <- data.frame(x, "WhiteAlone"=rowSums(x[c(1)]))
x <- x[with(x,order(GEOID,decreasing=TRUE)),]
barplot(x$WhiteAlone,names.arg=c(1:nrow(x)),ylim=c(0,5500),col=rgb(0,0,0,.05),add=TRUE)
mtext("Whatcom 2012 Census CVAP ACS Overlay (transparent not stacked). Block Groups in RGB/Alpha ordered by GEOID.",side=3,line=3)
mtext("Whatcom County Block Group distribution of citizens identifying as 'White Alone' (in WHITE .05).",side=3,line=1)

library(lattice)
jpeg_create()
barchart(~HispanicLatino + AsianAlone + NAAlone + BlackAlone | GEOID,data=x,col=c("orange","yellow","red","black"))
jpeg_create()
barchart(~HispanicLatino + AsianAlone + BlackAlone | GEOID,data=x,col=c("orange","yellow","black"))
jpeg_create()
barchart(~HispanicLatino + NAAlone | GEOID,data=x,col=c("orange","red"))
jpeg_create()
barchart(~HispanicLatino | GEOID,data=x,col=c("orange"))
jpeg_create()
barchart(~AsianAlone | GEOID,data=x,col=c("yellow"))
jpeg_create()
barchart(~NAAlone | GEOID,data=x,col=c("red"))
jpeg_create()
barchart(~BlackAlone | GEOID,data=x,col=c("black"))
jpeg_create()
barchart(~WhiteAlone + NotWhite | GEOID,data=x,col=c(rgb(0,0,0,.05),rgb(0,0,0,.15)))
graphics.off()



# mosaicplot of NW not working

x <- table(x, NW=rowSums(x[c(3,5:13)]), "PCT_NW"=rowSums(x[c(3,5:13)]) / rowSums(x[2]))
x <- x[with(x,order(NW,decreasing=TRUE)),]
mosaicplot(x$NW,names.arg=c(1:nrow(x)),col=rgb(1,0,0,.50))
mtext("Whatcom County Block Group distribution of citizens identifying with at least one race not 'White'.", side=1,line=3)

rev(groups)
 [1] "White Alone"                                                   
 [2] "Total"                                                         
 [3] "Remainder of Two or More Race Responses"                       
 [4] "Not Hispanic or Latino"                                        
 [5] "Native Hawaiian or Other Pacific Islander Alone"               
 [6] "Hispanic or Latino"                                            
 [7] "Black or African American and White"                           
 [8] "Black or African American Alone"                               
 [9] "Asian and White"                                               
[10] "Asian Alone"                                                   
[11] "American Indian or Alaska Native and White"                    
[12] "American Indian or Alaska Native and Black or African American"
[13] "American Indian or Alaska Native Alone"                        
 colSums(x[2,1])
 colSums(x[c(3,5:13)])
   X3    X5    X6    X7    X8    X9   X10   X11   X12   X13 
  847   372 11928   949  1681  1855  5535  1841    29  5062 
 colSums(x[c(2,1)])
    X2     X1 
190180 160085 
 sum(colSums(x[c(3,5:13)]))
[1] 30099




# From the 2012 ACS County File
jpeg_create <- function() {
 systime <- as.numeric(Sys.time())
 # dev.new()
 jpeg(filename = systime,
          width = 1024, height = 768, units = "px", pointsize = 12,
          quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
 Sys.sleep(2)
   }

library(plyr)
library(sqldf)
library(lattice)
setwd("C:/Politics/CVAP_CSV_Format_2008-2012_ACS")
County <- read.csv("County.csv", header = TRUE,stringsAsFactors = FALSE)
CountyWA <- sqldf("Select * from County where GEONAME LIKE '%Washington'")
write.csv(subset(County, GEONAME == "Whatcom County, Washington"),"Whatcom.csv")
c1 <- read.csv("Whatcom.csv", header = TRUE,stringsAsFactors = FALSE)
arrange(c1,desc(CIT_EST))

c2 <- arrange(subset(CountyWA,LNTITLE=="Total",select=c(1,5:12)),desc(CIT_EST))
c3 <- arrange(subset(CountyWA,LNTITLE=="White Alone",select=c(1,5:12)),desc(CIT_EST))
c4 <- arrange(subset(CountyWA,LNTITLE=="Hispanic or Latino",select=c(1,5:12)),desc(CIT_EST))
c5 <- arrange(subset(CountyWA,LNTITLE=="Asian Alone",select=c(1,5:12)),desc(TOT_EST))
c6 <- arrange(subset(CountyWA,LNTITLE=="Black or African American Alone",select=c(1,5:12)),desc(CIT_EST))
c7 <- arrange(subset(CountyWA,LNTITLE=="American Indian or Alaska Native Alone",select=c(1,5:12)),desc(CIT_EST))

cX0 <- colSums(arrange(subset(CountyWA,LNTITLE=="Total",select=c(5:12)),desc(CIT_EST)))
#cx1
#cx2
unique(CountyWA$GEONAME)
cX1 <- sqldf("Select Distinct(GEONAME) AS COUNTIES,Sum(CIT_EST),Sum(CVAP_EST) from CountyWA where LNTITLE='Total' Group By COUNTIES")
cX2 <- sqldf("Select Distinct(LNTITLE) AS TITLE,Sum(CIT_EST),Sum(CVAP_EST) from CountyWA Group By TITLE")
cX3 <- sqldf("Select Distinct(GEONAME) As Counties,LNTITLE,Sum(CIT_EST),Sum(CVAP_EST) from CountyWA Group By COUNTIES,LNTITLE")

jpeg_create()
with(c2,(barplot(CIT_EST,horiz=TRUE,names.arg=strsplit(GEONAME,"County, Washington"),width=.9,las=1,xpd=TRUE,xlim=c(0,1900000),ylim=c(1,39),col=rgb(0,0,1,.75))))
par(new=T)
with(c2,(barplot(CVAP_EST,horiz=TRUE,width=.9,xlim=c(0,1900000),ylim=c(1,39),xlab="2012 ACS WA Counties : CIT_EST (blue) vs. CVAP_EST (red)",col=rgb(1,0,0,.75))))
mtext("Totals",side=3, line=1)
par(new=F)

jpeg_create()
with(c3,(barplot(CIT_EST,horiz=TRUE,names.arg=strsplit(GEONAME,"County, Washington"),width=.9,las=1,xpd=TRUE,xlim=c(0,1300000),ylim=c(1,39),col=rgb(0,0,1,.75))))
par(new=T)
with(c3,(barplot(CVAP_EST,horiz=TRUE,width=.9,xlim=c(0,1300000),ylim=c(1,39),xlab="2012 ACS WA Counties : CIT_EST (blue) vs. CVAP_EST (red)",col=rgb(1,0,0,.75))))
mtext("White Alone",side=3, line=1)
par(new=F)

jpeg_create()
with(c4,(barplot(CIT_EST,horiz=TRUE,names.arg=strsplit(GEONAME,"County, Washington"),width=.9,las=1,xpd=TRUE,xlim=c(0,120000),ylim=c(1,39),col=rgb(0,0,1,.75))))
par(new=T)
with(c4,(barplot(CVAP_EST,horiz=TRUE,width=.9,xlim=c(0,120000),ylim=c(1,39),xlab="2012 ACS WA Counties : CIT_EST (blue) vs. CVAP_EST (red)",col=rgb(1,0,0,.75))))
mtext("Hispanic or Latino",side=3, line=1)
par(new=F)

jpeg_create()
with(c5,(barplot(CIT_EST,horiz=TRUE,names.arg=strsplit(GEONAME,"County, Washington"),width=.9,las=1,xpd=TRUE,xlim=c(0,200000),ylim=c(1,39),col=rgb(0,0,1,.75))))
par(new=T)
with(c5,(barplot(CVAP_EST,horiz=TRUE,width=.9,xlim=c(0,200000),ylim=c(1,39),xlab="2012 ACS WA Counties : CIT_EST (blue) vs. CVAP_EST (red)",col=rgb(1,0,0,.75))))
mtext("Asian Alone",side=3, line=1)
par(new=F)

jpeg_create()
with(c6,(barplot(CIT_EST,horiz=TRUE,names.arg=strsplit(GEONAME,"County, Washington"),width=.9,las=1,xpd=TRUE,xlim=c(0,120000),ylim=c(1,39),col=rgb(0,0,1,.75))))
par(new=T)
with(c6,(barplot(CVAP_EST,horiz=TRUE,width=.9,xlim=c(0,120000),ylim=c(1,39),xlab="2012 ACS WA Counties : CIT_EST (blue) vs. CVAP_EST (red)",col=rgb(1,0,0,.75))))
mtext("Black or African American Alone",side=3, line=1)
par(new=F)

jpeg_create()
with(c7,(barplot(CIT_EST,horiz=TRUE,names.arg=strsplit(GEONAME,"County, Washington"),width=.9,las=1,xpd=TRUE,xlim=c(0,13000),ylim=c(1,39),col=rgb(0,0,1,.75))))
par(new=T)
with(c7,(barplot(CVAP_EST,horiz=TRUE,width=.9,xlim=c(0,13000),ylim=c(1,39),xlab="2012 ACS WA Counties : CIT_EST (blue) vs. CVAP_EST (red)",col=rgb(1,0,0,.75))))
mtext("American Indian or Alaska Native Alone",side=3, line=1)
par(new=F)

No comments:

Post a Comment