rm(list = ls())
if(!require(ggmap)) {install.packages("ggmap")}
## Loading required package: ggmap
## Warning: package 'ggmap' was built under R version 3.5.1
## Loading required package: ggplot2
if(!require(ggplot2)) {install.packages("ggplot2")}
library(ggmap)
library(ggplot2)

corvallis <- c(lon = -123.2620, lat = 44.5646)
# Get map at zoom level 5: map_5
map_5 <- get_map(corvallis, zoom = 5, scale = 1)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=5&size=640x640&scale=1&maptype=terrain&language=en-EN&sensor=false
# Plot map at zoom level 5
ggmap(map_5)

# Get map at zoom level 13: corvallis_map
corvallis_map <- get_map(corvallis, zoom = 13, scale = 1)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=13&size=640x640&scale=1&maptype=terrain&language=en-EN&sensor=false
# Plot map at zoom level 13
ggmap(corvallis_map)

#Read House sales rds file
sales <- readRDS("01_corv_sales.rds")
# Look at head() of sales
head(sales)
# Swap out call to ggplot() with call to ggmap()
ggmap(corvallis_map) +
  geom_point(aes(lon, lat), data = sales)

corvallis <- c(lon = -123.2620, lat = 44.5646)

# Add a maptype argument to get a satellite map
corvallis_map_sat <- get_map(corvallis, zoom = 13, maptype = "satellite")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=13&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false
# Edit to display satellite map
ggmap(corvallis_map_sat) +
  geom_point(aes(lon, lat, color = year_built), data = sales)

# Add source and maptype to get toner map from Stamen Maps
corvallis_map_bw <- get_map(corvallis, zoom = 13, source = "stamen", maptype = "toner")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=44.5646,-123.262&zoom=13&size=640x640&scale=2&maptype=terrain&sensor=false
## Map from URL : http://tile.stamen.com/toner/13/1289/2959.png
## Map from URL : http://tile.stamen.com/toner/13/1290/2959.png
## Map from URL : http://tile.stamen.com/toner/13/1291/2959.png
## Map from URL : http://tile.stamen.com/toner/13/1292/2959.png
## Map from URL : http://tile.stamen.com/toner/13/1289/2960.png
## Map from URL : http://tile.stamen.com/toner/13/1290/2960.png
## Map from URL : http://tile.stamen.com/toner/13/1291/2960.png
## Map from URL : http://tile.stamen.com/toner/13/1292/2960.png
## Map from URL : http://tile.stamen.com/toner/13/1289/2961.png
## Map from URL : http://tile.stamen.com/toner/13/1290/2961.png
## Map from URL : http://tile.stamen.com/toner/13/1291/2961.png
## Map from URL : http://tile.stamen.com/toner/13/1292/2961.png
## Map from URL : http://tile.stamen.com/toner/13/1289/2962.png
## Map from URL : http://tile.stamen.com/toner/13/1290/2962.png
## Map from URL : http://tile.stamen.com/toner/13/1291/2962.png
## Map from URL : http://tile.stamen.com/toner/13/1292/2962.png
# Edit to display toner map
ggmap(corvallis_map_bw) +
  geom_point(aes(lon, lat, color = year_built), data = sales)

# Use base_layer argument to ggmap() to specify data and x, y mappings
ggmap(corvallis_map_bw, 
      base_layer = ggplot(sales, aes(lon,lat)))+
  geom_point(aes(color = year_built))

# Use base_layer argument to ggmap() to specify data and x, y mappings
ggmap(corvallis_map_bw, 
      base_layer = ggplot(sales, aes(lon,lat)))+
  geom_point(aes(color = class))+facet_wrap(~class)

# Plot house sales using qmplot()
qmplot(lon, lat, data = sales, 
       geom = "point", color = bedrooms) +
  facet_wrap(~ month)
## Using zoom = 13...
## Map from URL : http://tile.stamen.com/toner-lite/13/1289/2959.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1290/2959.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1291/2959.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1292/2959.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1289/2960.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1290/2960.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1291/2960.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1292/2960.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1289/2961.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1290/2961.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1291/2961.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1292/2961.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1289/2962.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1290/2962.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1291/2962.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1292/2962.png
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

ward_sales <- readRDS("01_corv_wards.rds")

# Add a point layer with color mapped to ward
ggplot(ward_sales, aes(lon, lat)) + geom_point(aes(color = ward))

# Add a point layer with color mapped to group
ggplot(ward_sales, aes(lon, lat)) + geom_point(aes(color = group))

# Add a path layer with group mapped to group
ggplot(ward_sales, aes(lon, lat)) + geom_path(aes(group = group))

# Add a polygon layer with fill mapped to ward, and group to group
ggplot(ward_sales, aes(lon, lat))+geom_polygon(aes(fill = ward, group = group))

# Fix the polygon cropping
ggmap(corvallis_map_bw, 
      base_layer = ggplot(ward_sales, aes(lon, lat))) +
  geom_polygon(aes(group = group, fill = ward))

# Repeat, but map fill to num_sales
ggmap(corvallis_map_bw,
    base_layer = ggplot(ward_sales, aes(lon,lat))) +
    geom_polygon(aes(fill = num_sales, group = group))

# Repeat again, but map fill to avg_price
ggmap(corvallis_map_bw,
      base_layer = ggplot(ward_sales, aes(lon,lat)))+
      geom_polygon(aes(group = group,fill = avg_price))

preds <- readRDS("01_corv_predicted_grid.rds")

# Add a geom_point() layer
ggplot(preds, aes(lon, lat))+
geom_point()

# Add a tile layer with fill mapped to predicted_price
ggplot(preds, aes(lon, lat)) + 
geom_tile(aes(fill = predicted_price))

# Use ggmap() instead of ggplot()
ggmap(corvallis_map_bw)+
geom_tile( aes(lon, lat, fill = predicted_price), preds,alpha= 0.8)
## Warning: Removed 40 rows containing missing values (geom_tile).