I am attempting to replicate what I see in a lecture for this weeks course on data visualizations and maps. When i copy down what is put forth in the lecture I get an error that I don't understand its meaning. The map shown in the lecture is a really clean US map colored blue hues with one gradient legend for the US census base 2010 data.
Below i've included my minimum code to reproduce the results
...r
#this function installs my packages
EnsurePackage<-function(x){
x<-as.character(x)
if (!require(x,character.only=TRUE)){
install.packages(pkgs=x, repos="http://cran.r-project.org")
require(x, character.only=TRUE)
}
}
#this package reads and cleans the census data
readCensus<-function(){
urlToRead<-"http://www2.census.gov/programs-surveys/popest/tables/2010-2011/state/totals/nst-est2011-01.csv"
tempFrame<-data.frame(read.csv(urlToRead))
tempFrame<-tempFrame[-1:-8,1:5]
tempFrame<-tempFrame[-52:-58,]
colnames(tempFrame)<-c("statename", "april10census","april10base", "july10pop", "july11pop")
row.names(tempFrame)<- NULL
tempFrame$statename<-gsub("\.","",tempFrame$statename)
tempFrame$april10census<-Numberize(tempFrame$april10census)
tempFrame$april10base<-Numberize(tempFrame$april10base)
tempFrame$july10pop<-Numberize(tempFrame$july10pop)
tempFrame$july11pop<-Numberize(tempFrame$july11pop)
return(tempFrame)
}
#these are the two called packages
EnsurePackage("ggplot2")
EnsurePackage("ggmap")
#this set of code works for calling a white map
us <-map_data("state")
sevensix<-data.frame(state.name, stringsAsFactors=FALSE)
sevensix$state<-tolower(sevensix$state.name)
map.simple<-ggplot(sevensix, aes(map_id=state))
map.simple<- map.simple+geom_map(map=us, fill="white", color="black")
map.simple<-map.simple+ expand_limits(x=us$long, y=us$lat)
map.simple<- map.simple+coord_map() + ggtitle("basic map of USA")
#this set of code is to activate the colored map supposed to return the attached map
us <-map_data("state")
dfStates<-readCensus()
dfStates$statename<-tolower(dfStates$statename)
#if i change this line to map_id=statename than it at least returns a map but it looks like the second attachment
map.popColor<-ggplot(dfStates,aes(map_id=state))
map.popColor<-map.popColor+ geom_map(map=us, aes(fill=april10base))
map.popcolor<-map.popColor+expand_limits(x=us$long, y=us$lat)
map.popColor<-map.popcolor+coord_map()+ggtitle("State Population")
map.popcolor
...
Desired Result Map Example:
Altered Code Map Result:
thank you all for your considerations.