In this document, I scanned several ggmap feature to create map-related maps.

Utility

Create a mosaic map with different map types.

#' create map slices
make.map<-function(map, frame){
  mname <- names(map)
  stopifnot(frame[1]*frame[2]==length(mname))
  sq <- as.numeric(attr(map[[mname[1]]],"bb"))
  sq <- t(matrix(sq,2,2))
  sq <- data.frame(sq)
  colnames(sq)<-c("lon","lat")
  width = (sq[2,1]-sq[1,1])/frame[2]
  height = (sq[2,2]-sq[1,2])/frame[1]
  wsize = floor(ncol(map[[mname[1]]])/frame[2])
  hsize = floor(nrow(map[[mname[1]]])/frame[1])
  p <- ggplot(aes(x=lon,y=lat),data=sq) + geom_point(alpha=0)
  ii <- 0
  for (i in 1:frame[2]-1)
    for (j in 1:frame[1]-1){
      ii <- ii+1
      p <- p + annotation_raster(map[[mname[ii]]][(1+j*hsize):((j+1)*hsize),(1+i*wsize):((i+1)*wsize)],xmin=sq[1,1]+i*width,xmax=sq[1,1]+(i+1)*width,ymin=sq[1,2]+height*j,ymax=sq[1,2]+(j+1)*height)
    p <- p + annotate("text",x=sq[1,1]+(i+0.35)*width, y=sq[1,2]+(j+0.1)*height,label=mname[ii],col="red",size=6)
    #p <- p + geom_text(data = NULL,x=sq[1]+(i-0.7)*width, y=sq[2],label=mname[i])
  }
  p+xlab("Long.") + ylab("Lat.")
}

Basic map types

Here we preview the five types of map from google map api.

map <- list()
source = "google"
map$base <- get_map("New York City", source=source)
map$roadmap <- get_map("New York City",maptype='roadmap', source=source)
map$hybird <- get_map("New York City",maptype='hybrid', source=source)
map$satellite <- get_map("New York City",maptype='satellite', source=source)
map$terrain <- get_map("New York City",maptype='terrain', source=source)
make.map(map,c(1,5))

Add another layer

We use the citi bike 2013 July data. We select just bikes with duration<100. We use the start.station.longitude, start.station.latitude, and tripduration.

load("temp.RData")
df <- data.frame(x=data$start.station.longitude, y=data$start.station.latitude,duration=data$tripduration)

Quick way

To quickly plot data over a map, use qmplot.

qmplot(start.station.longitude, start.station.latitude, data=data, source='google')

#qmplot(data$start.station.longitude, data$start.station.latitude)

Fancier way - points

Exploit the full power of ggplot, we could take ggmap object as an ggmap layer. Here we present two methods.

m <- get_map("New York City",zoom=14,maptype="toner",source="stamen")

ggmap(m, base_layer = ggplot(aes(x = x, y = y, size = duration), data = df))  + geom_point(color="yellow",alpha=0.3)

Note that the axes in the above figure are labeled as x and y. This is caused by replacing the base_layer. If we add geom_ objects directly, it will show lon and lat instead. We show the example in below.

ggmap(m) + geom_point(aes(x=x,y=y,color=duration),data=df) + geom_point(size=3,alpha=0.3)

Somehow, the following lines doesn’t work. It is not clear why we got into this problem.

ggmap(m, base_layer = ggplot(aes(x = start.station.longitude, y = start.station.latitude), data = data)) +geom_point()
ggmap(m) +geom_point(aes(x = start.station.longitude, y = start.station.latitude), data = data)

More effect - shades

Here by using stat_density2d, we could add shade effect.

g <- ggmap(m)
g <- g+stat_density2d(aes(x = x, y = y, fill=..level..), data=df,geom="polygon", alpha=0.2)
g + scale_fill_gradient(low = "yellow", high = "red")

And annotate!

Also just adding annotation to other ggplot figures, we could add text too.

g <- ggmap(m)
g + annotate("text",x=-74.01, y=40.71,label="New York",color="Red")