GGPlot Map - Not returning intended results

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:
image

Altered Code Map Result:

thank you all for your considerations.

Here's my best stab at replicating your first plot,


library(ggplot2)
library(ggmap)
library(readr)
library(stringr)
library(usmap)

{
  urlToRead<-"http://www2.census.gov/programs-surveys/popest/tables/2010-2011/state/totals/nst-est2011-01.csv"
tempFrame<-read_csv(urlToRead) %>% 
  as_tibble
tempFrame<-tempFrame[-1:-8,1:5]
tempFrame<-tempFrame[-52:-58,]

tempFrame <- tempFrame %>% 
  dplyr::rename(
    "statename" = 1,
    "april10census" = 2,
    "april10base" = 3, 
    "july10pop" = 4, 
    "july11pop" = 5
  ) %>% 
  mutate(state = str_remove(statename , ".")) %>% 
  mutate_at(.vars = c("april10census","april10base", "july10pop", "july11pop"),
            funs(str_remove_all(string = ., ",") %>% as.integer())
  )
}
#> Warning: Missing column names filled in: 'X2' [2], 'X3' [3], 'X4' [4],
#> 'X5' [5], 'X6' [6], 'X7' [7], 'X8' [8], 'X9' [9], 'X10' [10]
#> Parsed with column specification:
#> cols(
#>   `table with row headers in column A and column headers in rows 3 through 4. (leading dots indicate sub-parts)` = col_character(),
#>   X2 = col_character(),
#>   X3 = col_character(),
#>   X4 = col_character(),
#>   X5 = col_number(),
#>   X6 = col_character(),
#>   X7 = col_character(),
#>   X8 = col_character(),
#>   X9 = col_character(),
#>   X10 = col_character()
#> )

plot_usmap(data = tempFrame, values = "april10base", lines = "white") + 
  scale_fill_continuous(name = "Population (april10base)", label = scales::comma) + 
  theme(legend.position = "right")

Created on 2019-02-17 by the reprex package (v0.2.1)


The example code provided seems to be missing a few steps. For example, the population data april10base was a number with comma every third digit (note the warning messages). So a few of my steps handled converting those into integers. Once those numbers are treated as numbers, the map plot will treat them as a continuous number, hence my continuous legend.
If you don't do this, these population numbers are treated as factor values, and that's how you got 51 values in your second plot's legend.

I'd encourage you to check out a good intro to mapping with R and ggplot. For just the US, check out the vignettes with the usmap package CRAN - Package usmap

For a larger introduction to mapping with R, maybe check out something like

http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html

5 Likes

Thank you it was actually a couple of issues, the first was that there was a typo in my deployment of states secondarily there was a datatype issue with my numberize function. I was removing the spaces and the commas from the data but than was still returning it as a string. Thus it was giving me 50 states of string values. Thank you all for your help in trying to diagnose this.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.