The following is a script file containing all R code of all sections in this slide set.

Spatio-Temporal Visualization

library(ggmap)
map <- get_map("Ljubljana",
               zoom=14)
ggmap(map)
library(ggmap)
map <- get_map("London",zoom=14,
               maptype="roadmap")
ggmap(map)
map <- get_map("Lisbon",zoom=14,
               maptype="hybrid")
ggmap(map,extent="device")
library(readr)
df <- read_csv("firesnew_25000_500m.txt")
df[1:3,]
library(tidyr)
dat <- gather(df[,3:14],year, burnt, ano1991:ano2000)
dat$year <- substr(dat$year,4,7)
head(dat,4)
library(ggmap)
pt <- get_map("Portugal",zoom=7)
data2plot <- dat[dat$year==1999 & dat$burnt == 1,]
ggmap(pt,extent="device") + 
    geom_point(data=data2plot,
               aes(x=x,y=y),
               color="orange",size=3)
library(ggmap)
rlat <- range(df$x)
rlon <- range(df$y)
bb <- c(left=rlat[1],bottom=rlon[1],right=rlat[2],top=rlon[2])
pt2 <- get_map(bb,source="stamen",zoom=7)
data2plot <- dat[dat$year==1999 & dat$burnt == 1,]
ggmap(pt2,extent="device") + 
    geom_point(data=data2plot,
               aes(x=x,y=y),
               color="orange",size=3)
ggmap(pt,extent="device",
      base_layer=ggplot(data2plot,aes(x=x,y=y)) 
      ) + 
    stat_density2d(aes(fill=..level..,
                       alpha=..level..),
                   bins=10,
                   geom="polygon") +
    scale_fill_gradient(low="black",high="red")
data2plot <- dat[dat$burnt == 1,]
ggmap(pt,extent="device",
      base_layer=ggplot(data2plot,aes(x=x,y=y)) 
      ) + 
    stat_density2d(aes(fill=..level.., alpha=..level..),
                   bins=10,
                   geom="polygon") +
    scale_fill_gradient(low="black",high="red") +
    facet_wrap( ~  year)
geocode("Ljubljana")
geocode("Ljubljana",output="more")
df[1000,c("x","y")]
revgeocode(as.numeric(df[1000,c("x","y")]))
from <- rep("New York",3)
to <- c("Los Angeles","San Francisco","Toronto")
mapdist(from,to)
mapdist("Chinatown","Times Square",mode="walking")
rtDat <- route("Porto","Lisbon",mode="driving",structure="route",alternatives=TRUE)
mp <- get_map("Portugal",zoom=7,maptype="hybrid")
ggmap(mp, 
      base_layer=ggplot(rtDat,aes(x=lon,y=lat,colour=route))) + 
      geom_path(size=1.5,lineend="round") + 
      facet_wrap(~ route) +
      labs(x = "Longitude", y = "Latitude", colour = "Route") +
      theme(legend.position = "top")
df <- read.csv("firesnew_25000_500m.txt")
library(sp)
spatialCoords <- cbind(long=df$x, lat=df$y)
coordRefSys <- CRS("+proj=longlat +ellps=WGS84")
fires1999 <- SpatialPointsDataFrame(spatialCoords,
                               df[, "ano1999", drop=FALSE],
                               proj4string=coordRefSys)
library(leaflet)
leaflet() %>% 
    addTiles() %>% 
    addCircleMarkers(data=fires1999[fires1999$ano1999==1,])
library(ggmap)
placesWithFires <- which(fires1999$ano1999 == 1)
coord <- coordinates(fires1999)[placesWithFires,]
## Note that the Google API imposes limits on the following...
adds <- apply(coord,1,function(cs) revgeocode(cs))
## Now the interactive map (try clicking on a dot)
leaflet() %>% 
    addTiles() %>% 
    addCircleMarkers(data=fires1999[fires1999$ano1999==1,], 
                     popup=adds,
                     clusterOptions = markerClusterOptions())
library(readr)
df <- read_csv("firesnew_25000_500m.txt")
library(sp)
spatialCoords <- cbind(long=df$x, lat=df$y)
coordRefSys <- CRS("+proj=longlat +ellps=WGS84")
fires1999 <- SpatialPointsDataFrame(spatialCoords,
                               df[, "ano1999", drop=FALSE],
                               proj4string=coordRefSys)
library(leaflet)
leaflet() %>% 
    addTiles() %>% 
    addCircleMarkers(data=fires1999[fires1999$ano1999==1,])
library(ggmap)
placesWithFires <- which(fires1999$ano1999 == 1)
coord <- coordinates(fires1999)[placesWithFires,]
## Note that the Google API imposes limits on the following...
adds <- apply(coord,1,function(cs) revgeocode(cs))
## Now the interactive map (try clicking on a dot)
leaflet() %>% 
    addTiles() %>% 
    addCircleMarkers(data=fires1999[fires1999$ano1999==1,], 
                     popup=adds,
                     clusterOptions = markerClusterOptions())