http://bcb.dfci.harvard.edu/~aedin/courses/R/CDC/maps.html
#########################################################
## Geographic Information of Customers
#########################################################
# Returns centroids
getLabelPoint <- function(county) {
Polygon(county[c('long', 'lat')])@labpt}
df <- map_data("state")
centroids <- by(df, df$region, getLabelPoint) # Returns list
centroids <- do.call("rbind.data.frame", centroids) # Convert to Data Frame
names(centroids) <- c('long', 'lat') # Appropriate Header
centroids$states <- rownames(centroids)
dat8<-dbGetQuery(conn,"select demand_state,
count(distinct mstr_persona_key) cust
from eipdb_sandbox.ling_sls_brnd_demog
where new_ind=1 and trn_sls_dte between '2014-11-01' and '2016-10-31'
group by 1
order by 1;
")
## Join with States
dat8$states <- tolower(state.name[match(dat8$demand_state, state.abb)])
states <- map_data("state")
head(states)
dat9 <- merge(dat8, centroids, by="states")
dat9$statelabel <- paste(dat9$demand_state, "\n", format(dat9$cust, big.mark = ",", scientific = F), sep="")
# ggplot(data = Total) +
# geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") +
# coord_fixed(1.3) +
# guides(fill=FALSE) +
# geom_text(data=statelable, aes(x=long, y=lat, label = demand_state), size=2)
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=dat8, map=states,
aes(fill=cust, map_id=states),
color="#ffffff", size=0.15) +
coord_fixed(1.3) +
scale_fill_continuous(low = "thistle2", high = "darkred", guide="colorbar") +
#scale_fill_distiller(name="Customers", palette = "YlGn", breaks=pretty_breaks(n=5)) +
#geom_text(data=dat9, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, label=statelabel), colour="black", size=4 ) +
geom_text(data=dat9, aes(x=long, y=lat, label=statelabel), colour="black", size=4 ) +
ggtitle("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)
## Plot2
## American Community Survey (ACS) Data
## Join with States
## access population estimates for US States in 2012
?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("Customers from 11/1/2015 to 10/31/2016") + ylab("") + xlab("") +
theme(plot.title = element_text(face = "bold", size = 20, hjust = 0.5)) +
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)
#########################################################
## World Maps
#########################################################
## regi_geo
data <- ddply(user_regi, .(regi_geo), summarise, tot=length(user_id))
summary(data$tot)
tmp=joinCountryData2Map(data, joinCode = "ISO2"
, nameJoinColumn = "regi_geo"
, verbose='TRUE'
)
tmp$tot[is.na(tmp$tot)]=0
# catMethod='categorical'
mapCountryData(tmp, nameColumnToPlot="tot",catMethod="fixedWidth")
#getting class intervals
classInt <- classIntervals(tmp[["tot"]], n=5, style = "jenks")
catMethod = classInt[["brks"]]
#getting colours
colourPalette <- brewer.pal(5,'RdPu')
#plot map
mapParams <- mapCountryData(tmp
,nameColumnToPlot="tot"
,addLegend=FALSE
,catMethod = catMethod
,colourPalette=colourPalette )
#adding legend
do.call(addMapLegend
,c(mapParams
,legendLabels="all"
,legendWidth=0.5
,legendIntervals="data"
,legendMar = 2))
tmp2=data.frame(tmp[['regi_geo']], tmp[['tot']], tmp[['NAME']])
tmp2=tmp2[order(tmp2$tmp...tot...,decreasing = T),]
write.csv(tmp2, "junk.csv")
No comments:
Post a Comment