---
title: "Lab 04 - Adding Points to a Map"
author: "Jesse Lecy"
date: "Friday, February 06, 2015"
output: pdf_document
---

# Adding Points to a Map

So far you have learned how to download a base map from one of the various public map archives like Google maps or OpenStreetMap. The base map serves as context for our analysis. We now need to add datat to the map.

GIS maps present data in two ways. Individual observations can be plotted as points on the map. This includes things like location data such as a museum or the location of a robbery, or it can include things like physical addresses of households. Points are added to a map using the same convention as a scatterplot where longitudinal coordinates comprise the x-axis and latitude coordinates comprise the y-axis.

Alternatively, data can be aggregated and summarized by levels per geographic units. We might report average income for each census tract, number of crimes per county, or high school graduation rates per state. It could also be binary, such as whether a state voted republican or democrat in a specific election. In each case the data needs to be aggregated by a specific geographic unit, then the geography is shaded to represent a specific level. These are called choropleth maps. They will be covered in Lab 07.


We will use the *geom_point* function in the *ggmap* package to add points to our plot. It requires the following arguments:

Argument | Values
-------- | -------
data=    | Name of the data set that contains the latitude and longitude coordinates.
aes=     | The name of your latitude and longitude variables in the data set.
size=    | The size of your points with 1 being default, 2 being double that, etc.
col=     | The color of your points. For options type *colors()*.
alpha=   | THe level of transparency of your points. Accepts values between 0 (invisible) and 1 (solid).

```{r}

library( ggmap )

lat <-  43.03915
lon <- -76.13512

lat.long <- data.frame( lat, lon )

# lat.long <- geocode( 'syracuse university' ) creates the same results, see below

ggmap( get_map(  location='syracuse university', zoom = 12, color="bw" ), extent="device" ) +

geom_point( data=lat.long, aes(x=lon, y=lat), size=2, col="red", alpha=1 )



ggmap( get_map(  location='syracuse university', zoom = 12, color="bw" ), extent="device" ) +

geom_point( data=lat.long, aes(x=lon, y=lat), size=50, col="blue", alpha=0.3 )



```


# Data that includes latitude and longitude coordinates

Some data sets come with pre-set location coordinates. These data sets are easy to work with because all we need to do is use the correct variable names in our *geom_point* function and we are good to go.

One example is the New York City database of 311 service calls:

https://data.cityofnewyork.us/Social-Services/311-Service-Requests-2009/3rfa-3xsf

Note that this is a large dataset (1,783,133 observations). It will take awhile if you try to download it!


# Geocoding address data

Most datasets, however, do not come with pre-set location coordinates. The most typical kind of location data is a mailing address or a street intersection. If we want to add these data as points on a map we must first translate the address information into a coordinate system. This is called geocoding.

For this example let's use the list of farmers markets active in NYC in 2012:

https://data.cityofnewyork.us/Business/2012-NYC-Farmers-Market-List/b7kx-qikm

We will read this data into R, process the address information, and geocode it to create latitude and longitude coordinates.

First, let's see how the *paste* function works. We will use it to add city and state fields to the address data.

```{r}
# We use the paste function to combine separate text fields into a single string.

paste( "Marlon", "Brando", sep=" " )

paste( "Brando", "Marlon", sep=", " )

paste( "houses", "Price", sep="$" )

# It also works with columns of data

x <- 1:3

y <- c("a","b","c")

paste( x, y, sep="" )

```

Ok, now let's see a geocoding example.

```{r}

# install RCurl if you have not done this already using install.packages("RCurl")

library( RCurl ) # package to read data from html files

# copy the download link by right-clicking on the 'download as csv' link

url1 <- "http://data.cityofnewyork.us/api/views/b7kx-qikm/rows.csv?accessType=DOWNLOAD"

farmers.markets <- read.csv( file=url1, stringsAsFactors=F )

names( farmers.markets )

dim( farmers.markets ) # 137 markets in our data set

# We will use the 'Street.Address' field to create a full address

head( farmers.markets$Street.Address )

addresses <- paste( farmers.markets$Street.Address, "New York", "NY", sep=", " )

head( addresses )


```

Now that we have a vector of addresses, let's geocode them using the *ggmap* package.

```{r}

# I am suppressing messages here so the document is not full of non-informative output from Google.
#
# To see warnings you would simply run:
#
# market.coords <- geocode( addresses )

options( warn=-1 )

suppressMessages( market.coords <- geocode( addresses, messaging=F ) )
  
ggmap( get_map("Queens, NY", col="bw", zoom=11 ), extent="device" ) +

  geom_point( data=market.coords, aes(x=lon, y=lat), size=5, col="red", alpha=0.5 )

# To add the location data back to your original dataset use cbind()

farmers.markets <- cbind(farmers.markets, market.coords)

```

Note that the Google API only allows you to map 2,500 addresses each day, so there are limits to how much data you can geocode at any one time. 

If you were doing a larger project (many thousands of observations) you would have them geocoded through a commercial application, add the location data to your dataset, and then plot the points.



# Looking Ahead - Heat Maps

Heat maps are a nice way to summarize point data by highlight areas with dense clusters of points.

For this example we will use public art projects funded by NYC:

https://data.cityofnewyork.us/Recreation/Completed-Percent-for-Art-projects-with-artist-inf/gzdv-qiga

We will create a heat map of projects to show areas with the highest concentation of public art, then add yellow points for the actual projects over the top. 


```{r}

url2 = "http://data.cityofnewyork.us/api/views/gzdv-qiga/rows.csv?accessType=DOWNLOAD"

my.data = read.csv(file=url2, stringsAsFactors=F )

library( ggmap )

full.address <- paste( my.data$Address, my.data$City, "NY", sep=", " )

head( full.address )

suppressMessages( location <- geocode( full.address ) )

nyc <- ggmap( get_map( "Queens, NY", color="bw", zoom=11 ), extent="device" )

h2 <- nyc + stat_density2d( data=location, aes(x=lon,y=lat, fill=..level..), bins=50, geom="polygon", alpha=0.1 )

h3 <- h2 + geom_point(data=location, aes(x=lon,y=lat),size = 2, alpha=0.3, col="goldenrod" )

h3


```




