packages

library(ggplot2)
library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------------ tidyverse 1.2.1 --
## v tibble  1.4.2     v purrr   0.2.4
## v tidyr   0.8.0     v dplyr   0.7.4
## v readr   1.1.1     v stringr 1.3.0
## v tibble  1.4.2     v forcats 0.3.0
## -- Conflicts --------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(forcats)
library(labeling)
library(ggmap)

Pokemon in San Francisco

Here’s a dataset taken from Kaggle. Let’s put everything together. We’ll be using ggmap (not the qmap – I still don’t see a difference).

sfmap=ggmap(get_googlemap(center=c(lon=-122.2913, lat=37.8272),zoom=10, size=c(640,640),scale=2,maptype='terrain')) #'satellite','roadmap'
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.8272,-122.2913&zoom=10&size=640x640&scale=2&maptype=terrain&sensor=false
poke=read_csv('C:/Users/Aco/Desktop/Stat744/Stat744/Data/pokemon-spawns.csv') #Apologies for using my desktop to set the wd
## Parsed with column specification:
## cols(
##   s2_id = col_double(),
##   s2_token = col_character(),
##   num = col_integer(),
##   name = col_character(),
##   lat = col_double(),
##   lng = col_double(),
##   encounter_ms = col_double(),
##   disppear_ms = col_double()
## )
#no Idea why it wouldn't work when trying to knit, typically, everything works fine when I did the following

# poke=read_csv('Data/pokemon-spawns.csv')
# This is the preferred method and works in R normally, just knit isn't able to find it.

#location of all pokemon (legend excluded because there are 151)
sfmap+
  geom_point(data=poke, aes(x=lng,y=lat,colour=name),size=0.02)+
  theme(legend.position = "none")
## Warning: Removed 147314 rows containing missing values (geom_point).

That’s a lot of pokeymen. Let’s optimize our time, and try to find one of the rarest ones.

#find snorlax
snorlax=poke%>%filter(name=="Snorlax")

sfmap+
  geom_point(data=snorlax, aes(x=lng,y=lat),colour="red",size=2)+
  theme(legend.position = "none")
## Warning: Removed 10 rows containing missing values (geom_point).

Can’t do many useful things with such small amounts of data points. Let’s pick something uncommon (sweetspot so we don’t flood the map again with the common trash).

Let’s compare the differences in the maps, similarly to how we started with the crime.

#snorlax too rare, too stronk, how about Growlithe (something not too common, but still won't spam the map)
grow=poke%>%filter(name=="Growlithe")

sfmap+
  geom_point(data=grow, aes(x=lng,y=lat),colour="red",size=1)+
  theme(legend.position = "none")
## Warning: Removed 3481 rows containing missing values (geom_point).

sfmap+
  geom_density2d(data=grow, aes(x=lng,y=lat),colour="red",size=1)+
  theme(legend.position = "none")
## Warning: Removed 3481 rows containing non-finite values (stat_density2d).

sfmap+
  geom_bin2d(data=grow, aes(x=lng,y=lat))+
  theme(legend.position = "none")
## Warning: Removed 3481 rows containing non-finite values (stat_bin2d).
## Warning: Removed 7 rows containing missing values (geom_tile).

Let’s create a gradiant and compare two different bin sizes.

sfmap+
  stat_density2d(aes(x = lng, y = lat, fill = ..level.., alpha = ..level..),
                 bins = 5, geom = "polygon",
                 data = grow) +
  scale_fill_gradient(low = "black", high = "red")+
  theme(legend.position= "none")
## Warning: Removed 3481 rows containing non-finite values (stat_density2d).

#we've lost all the growlithes on the san mateo side, let's add more bins (clearly san leandro is where all the growlithes are at).

sfmap+
  stat_density2d(aes(x = lng, y = lat, fill = ..level.., alpha = ..level..),
                 bins = 25, geom = "polygon",
                 data = grow) +
  scale_fill_gradient(low = "black", high = "red")+
  theme(legend.position= "none")
## Warning: Removed 3481 rows containing non-finite values (stat_density2d).

Now we’ll have our Arcanine in no time. Now back to Leaflet.