Skip to content

Instantly share code, notes, and snippets.

@epijim
Last active December 29, 2015 17:39
Show Gist options
  • Save epijim/7705943 to your computer and use it in GitHub Desktop.
Save epijim/7705943 to your computer and use it in GitHub Desktop.
Mapping Cambridge
library(ggmap)
getLineColor <- function(val) {
#pal <- colorRampPalette(c("#333333", "#ffffff", "#b5310c"))
pal <- colorRampPalette(c("#ffffff", "red"))
colors <- pal(80)
val.log <- log(val)
if (val > 50) {
col <- colors[80]
} else {
colindex <- max( 1, round( 80 * val / 50 ) )
col <- colors[colindex]
}
return(col)
}
distance <- read.csv("nearest-rest.csv", stringsAsFactors=FALSE)
# Calculate colors
pts0 <- mapproject(unlist(distance[, 'lng']), unlist(distance[, 'lat']))
pts1 <- mapproject(unlist(distance[, 'lngnear']), unlist(distance[, 'latnear']))
seg.colors <- sapply(distance$dist_miles, FUN=getLineColor)
#
# Map It
#
cam <- read.csv("nearest-rest.csv", stringsAsFactors=FALSE)
# The box that the data points are within, just top left (lat and long) and bottom right are fed in
# This box will be taken from google maps. You can also use other mapping sites - see ggmap help for full list.
bbox <- c(0.104994, 52.215382, 0.149626, 52.195868)
#get the map from google
basemap <- get_map(location=bbox, zoom=14, source='google', maptype="terrain", color="bw")
#distance to line map
ggmap(basemap) + geom_segment(aes(x=lng, xend=lngnear, y=lat, yend=latnear, colour=dist_miles), size=1, data=cam) + scale_colour_gradient(low="white", high="red", limits=c(0, 0.5))
#contour map
ggmap(basemap) + stat_density2d(aes(x=lngnear, y=latnear), data=cam, bins=30, color="red", alpha=0.8)
#density map
ggmap(basemap) + stat_density2d(aes( x=lngnear,y=latnear, fill=..level.. , alpha=..level.. ), data=cam, bins=8 ,alpha=0.3, geom = "polygon" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment