## state.R
#############################
## require(choroplethr)
?df_pop_state
data(df_pop_state)
head(df_pop_state)
dat10 <- merge(dat9, df_pop_state, by.x="states", by.y="region")
dat10$perc <- dat10$cust/dat10$value
percent <- function(x, digits = 2, format = "f", ...) {
paste0(formatC(100 * x, format = format, digits = digits, ...), "%")
}
dat10$statelabel <- paste(dat10$demand_state, "\n", percent(dat10$perc,2,"f"), sep="")
head(dat10)
p9 <- ggplot() +
geom_map(data=states, map=states,
aes(x=long, y=lat, map_id=region),
fill="#ffffff", color="#ffffff", size=0.15) +
geom_map(data=dat10, map=states,
aes(fill=perc, map_id=states),
color="#ffffff", size=0.15) +
coord_fixed(1.3) +
scale_fill_continuous(low = "thistle2", high = "darkred", guide="colorbar") +
geom_text(data=dat10, aes(x=long, y=lat, label=statelabel), colour="black", size=4 ) +
ggtitle("Kohl's Customers from 11/1/2014 to 10/31/2016") + ylab("") + xlab("") +
theme(plot.title = element_text(face = "bold", size = 20)) +
theme(axis.text.x = element_text(face = "bold", size = 14)) +
theme(axis.text.y = element_text(face = "bold", size = 14)) +
theme(axis.title.x = element_text(face = "bold", size = 16)) +
theme(strip.text.x = element_text(face = "bold", size = 16)) +
theme(axis.title.y = element_text(face = "bold", size = 16, angle=90)) +
guides(fill=FALSE)
## collect acs table names and column names
install.packages("acs")
require(acs)
acs.lookup(endyear=2013, span=5, table.name="Age by Sex")
acs.lookup(endyear=2013, span=5, table.number="B01001")
write.csv(, "tmp.csv")
#############################
## demo.py
#############################
#############################
# https://github.com/CommerceDataService/census-wrapper
# https://www.socialexplorer.com/data/ACS2013_5yr/documentation/12e2e690-0503-4fb9-a431-4ce385fc656a
# http://www.census.gov/geo/maps-data/data/relationship.html
import csv
import pandas as pd
from census import Census
from us import states
# Datasets
# acs5: ACS 5 Year Estimates (2013, 2012, 2011, 2010)
# acs1dp: ACS 1 Year Estimates, Data Profiles (2012)
# sf1: Census Summary File 1 (2010, 2000, 1990)
# sf3: Census Summary File 3 (2000, 1990)
API_KEY = '0c9d762f43fe6dd071ec0f5a3bfdd19b478c2381'
c = Census(API_KEY, year=2013)
# example: convert names and fips
#c.acs5.get(('NAME', 'B25034_010E'),{'for': 'state:{}'.format(states.MD.fips)})
# Total number for all states:
records=['B01001_031E', 'B01001_032E','B01001_033E','B01001_034E','B01001_035E', 'B01001_036E']
flag = 0
for record in records:
if flag==0:
tmp=c.acs5.get(record, {'for': 'state:*'})
df=pd.DataFrame(tmp)
flag=1
else:
tmp1=c.acs5.get(record, {'for': 'state:*'})
df1=pd.DataFrame(tmp1)
df = df.merge(df1, on='state', how='outer')
df.index=df['state']
df.sort_index(inplace=True)
del df['state']
df1=df.applymap(int)
df1['tot']=df1.apply(sum, axis=1)/2
df1['tot']=df1['tot'].astype(int)
# Get State label
statelabel=[]
for state in df1.index:
statelabel.append(states.lookup(state).abbr)
df1['statelable']=statelabel
df1.to_csv('~/Desktop/output.csv')
print 'Done'
# https://www.socialexplorer.com/data/ACS2013_5yr/documentation/12e2e690-0503-4fb9-a431-4ce385fc656a
# http://www.census.gov/geo/maps-data/data/relationship.html
import csv
import pandas as pd
from census import Census
from us import states
# Datasets
# acs5: ACS 5 Year Estimates (2013, 2012, 2011, 2010)
# acs1dp: ACS 1 Year Estimates, Data Profiles (2012)
# sf1: Census Summary File 1 (2010, 2000, 1990)
# sf3: Census Summary File 3 (2000, 1990)
API_KEY = '0c9d762f43fe6dd071ec0f5a3bfdd19b478c2381'
c = Census(API_KEY, year=2013)
# example: convert names and fips
#c.acs5.get(('NAME', 'B25034_010E'),{'for': 'state:{}'.format(states.MD.fips)})
# Total number for all states:
records=['B01001_031E', 'B01001_032E','B01001_033E','B01001_034E','B01001_035E', 'B01001_036E']
flag = 0
for record in records:
if flag==0:
tmp=c.acs5.get(record, {'for': 'state:*'})
df=pd.DataFrame(tmp)
flag=1
else:
tmp1=c.acs5.get(record, {'for': 'state:*'})
df1=pd.DataFrame(tmp1)
df = df.merge(df1, on='state', how='outer')
df.index=df['state']
df.sort_index(inplace=True)
del df['state']
df1=df.applymap(int)
df1['tot']=df1.apply(sum, axis=1)/2
df1['tot']=df1['tot'].astype(int)
# Get State label
statelabel=[]
for state in df1.index:
statelabel.append(states.lookup(state).abbr)
df1['statelable']=statelabel
df1.to_csv('~/Desktop/output.csv')
print 'Done'
No comments:
Post a Comment