library(ggplot2)
library(maps)
library(RCurl)
## Loading required package: bitops
library(jsonlite)
library(ggmap)

Basic Map using ggplot2

df <- data.frame(state.name, stringsAsFactors = FALSE)
df$state <- tolower(df$state.name)

us <- map_data('state') # using maps package

#create a simple map of the usa

map.g <- ggplot(df, aes(map_id = state))

map.g <- map.g + geom_map(map=us, fill='white',  color='black')

map.g <- map.g + expand_limits(x=us$long, y= us$lat)
map.g <- map.g + coord_map() + ggtitle('Basic map of USA')
#map.g <- map.g + geom_point(aes(x=-97, y =30))
map.g

– Read census data and display it on the map

Read the census data

fhand <- "http://www2.census.gov/programs-surveys/popest/tables/2010-2011/state/totals/nst-est2011-01.csv"

readCensus <- function(fdir){
  df <- read.csv(url(fdir), skip =3)
  df <- df[,c(1,2,3,4,5)]
  df <- df[6:56,]
  colnames(df) <- c('stateName',     'base2010',    'base2011','Jul2010', 'Jul2011')
  df$base2010 <- as.integer(gsub(',','',df$base2010))
  df$base2011 <- as.integer(gsub(',', '',df$base2011))
  df$Jul2010 <- as.integer(gsub(',','',df$Jul2010))
  df$Jul2011 <- as.integer(gsub(',','',df$Jul2011))
  df$stateName <- gsub('^[.]{1}','',df$stateName)
  
  return(df)
}

dfState <- readCensus(fhand) # read census data from url

dfState$state <- tolower(dfState$stateName)

head(dfState)
##     stateName base2010 base2011  Jul2010  Jul2011      state
## 6     Alabama  4779736  4779735  4785401  4802740    alabama
## 7      Alaska   710231   710231   714146   722718     alaska
## 8     Arizona  6392017  6392013  6413158  6482505    arizona
## 9    Arkansas  2915918  2915921  2921588  2937979   arkansas
## 10 California 37253956 37253956 37338198 37691912 california
## 11   Colorado  5029196  5029196  5047692  5116796   colorado
map.popColor <- ggplot(dfState, aes(map_id =state))
map.popColor <- map.popColor + geom_map(map=us, aes(fill= Jul2011))
map.popColor <- map.popColor + expand_limits(x=us$long, y=us$lat)
map.popColor <- map.popColor + coord_map() + ggtitle('State Population')
map.popColor

# #create a function to accept an address argument, insert it into the google geocode url
# 
# MakeGeoURL <- function(address){
#   root <- "https://maps.googleapis.com/maps/api/geocode/json"
#   url <- paste(root, "json?address=",address,"&client=",sep="")
#   return (URLencode(url))
#   
# }
# 
# 
# 
# #MakeGeoURL("4632 durness ct Antioch ,Ca")
# 
# #create a function that take addrress as imput and return lat, long of the address.
# 
# address <- "Syracuse university, syracuse,NY"
# 
# Addr2latlng <- function(address){
#   url <- MakeGeoURL(address)
#   apiResult <- getURL(url)
#   geoStruct <- fromJSON(apiResult,simplify = FALSE)
#   lat <- NA
#   lng <- NA
#   
#   try(lat <- geoStruct$results[[1]]$geometry$location$lat)
#   try(lng<- geoStruct$results[[1]]$geometry$location$lng)
#   
#   return (c(lat,lng))
# }
# latlon <- Addr2latlng(address, id)  #generate long and lat of the address point

latlon <- geocode("Syracuse university, syracuse,NY",output = "latlona", source = 'dsk')
## Information from URL : http://www.datasciencetoolkit.org/maps/api/geocode/json?address=Syracuse%20university,%20syracuse,NY&sensor=false
latlon
##         lon      lat                          address
## 1 -76.18122 43.04982 Syracuse university, syracuse,NY
map.popColor <- map.popColor + geom_point(aes(x=latlon$lon, y=latlon$lat), color='darkred',size=3)
map.popColor