library(ggplot2)
library(scales)
library(plyr)
library(maps)
## map of the states (part of the map package)
states_map <- map_data("state")
states_map$region <- factor(states_map$region)
## got fips form here and saved it as txt; http://www.epa.gov/enviro/html/codes/state.html
fips <- read.table("states.txt",sep="\t",header=T)
fips$State.Name <- tolower(as.character(fips$State.Name))
## build the graphs
filenames <- paste("bmi",1984:2010,".rdata",sep="")
for(file in filenames){
load(file)
year <- substr(file,4,7)
x <- get(paste("x",year,sep=""))
## for adding the year to the plot
testdf <- data.frame(x2=-70,y2=49,year=year)
## for the first 4 years was no bmi in the data set
## I named my computed one "bmi" so I need another "bmi2" for the loop, not very sophisticated,
## but it works
if(!("bmi2" %in% names(x))){
print(file)
x$bmi2 <- x$bmi
}
## bmi groups
x$bmi2gr <- cut(x$bmi2,breaks=c(0,25,30,300),include.lowest=T,labels=c("1","2","3"))
## count
x <- ddply(x,.(State),transform,perstate=sum(!is.na(bmi2)))
x <- ddply(x,.(State,bmi2gr),transform,perstate.gr=sum(!is.na(bmi2)))
dats <- unique(x[,c("State","bmi2gr","perstate","perstate.gr")])
dats <- dats[!is.na(dats$bmi2gr),]
## percents
dats$perc <- dats$perstate.gr/dats$perstate
dats$ow <- as.numeric(dats$bmi2gr) > 1
## I just want the obese and overweight
## >= 25
dats2 <- dats[dats$ow==T,]
dats2 <- ddply(dats2,.(State),summarize,perc=sum(perc))
dats2$gr <- "ow"
## >= 30
dats3 <- dats[dats$bmi2gr=="3",c("State","perc")]
dats3$gr <- "obese"
dats <- rbind(dats2,dats3)
## identify the states in the data set using the region names in the map (fips coded)
dats <- merge(dats,fips[,2:3],by.x="State",by.y="FIPS.Code",all=T)
dats$gr[is.na(dats$gr)] <- "obese"
dats$State.Name <- factor(dats$State.Name)
## graph
ggplot(dats[dats$gr=="obese",],aes(map_id = State.Name)) +
geom_map(aes(fill=perc),colour="black",map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat) +
scale_fill_gradientn(limits=c(0.1,0.7),colours=cols,guide = guide_colorbar(),na.value="grey50") +
geom_text(data=testdf,aes(x=x2,y=y2,label=year),inherit.aes=F)
## save image
ggsave(file=paste("obese",substr(file,4,7),".png",sep=""))
}
output
[1] "bmi1984.rdata"
Saving 12.7 x 7.01 in image
[1] "bmi1985.rdata"
Saving 12.7 x 7.01 in image
[1] "bmi1986.rdata"
Saving 12.7 x 7.01 in image
[1] "bmi1987.rdata"
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
Saving 12.7 x 7.01 in image
No comments :
Post a Comment