I am trying to plot a map for my final project, and I am trying to do a heat map of crime by BLock in the US.
For each block, I have Lat, Lon, and a prediction of th
First, let's load the data:
data<-read.csv(file = "NY subsample.csv")
Then, let's try just plotting the basic locations and values of the data:
require('ggplot2')
# start with points
pred.points <- ggplot(data = data,
aes(x = GEO_CENTROID_LON,
y = GEO_CENTROID_LAT,
colour = prediction)) +
geom_point()
print(pred.points)
ggsave(filename = "NYSubsamplePredPoints.png",
plot = p2,
scale = 1,
width = 5, height = 3,
dpi = 300)
which gives us this:
Then, you can try to plot the mean in a 2-D region using stat_summary2d()
:
pred.stat <- ggplot(data = data,
aes(x = GEO_CENTROID_LON,
y = GEO_CENTROID_LAT,
z = prediction)) +
stat_summary2d(fun = mean)
print(pred.stat)
ggsave(filename = "NYSubsamplePredStat.png",
plot = pred.stat,
scale = 1,
width = 5, height = 3,
dpi = 300)
which gives us this plot of the mean value of prediction in each box.
Next, we can set the bin size, color scales, and fix the projection:
# refine breaks and palette ----
require('RColorBrewer')
YlOrBr <- c("#FFFFD4", "#FED98E", "#FE9929", "#D95F0E", "#993404")
pred.stat.bin.width <- ggplot(data = data,
aes(x = GEO_CENTROID_LON,
y = GEO_CENTROID_LAT,
z = prediction)) +
stat_summary2d(fun = median, binwidth = c(.05, .05)) +
scale_fill_gradientn(name = "Median",
colours = YlOrBr,
space = "Lab") +
coord_map()
print(pred.stat.bin.width)
ggsave(filename = "NYSubsamplePredStatBinWidth.png",
plot = pred.stat.bin.width,
scale = 1,
width = 5, height = 3,
dpi = 300)
which gives us this:
And last of all, here's the data overlain on a map.
require('ggmap')
map.in <- get_map(location = c(min(data$GEO_CENTROID_LON),
min(data$GEO_CENTROID_LAT),
max(data$GEO_CENTROID_LON),
max(data$GEO_CENTROID_LAT)),
source = "osm")
theme_set(theme_bw(base_size = 8))
pred.stat.map <- ggmap(map.in) %+% data +
aes(x = GEO_CENTROID_LON,
y = GEO_CENTROID_LAT,
z = prediction) +
stat_summary2d(fun = median,
binwidth = c(.05, .05),
alpha = 0.5) +
scale_fill_gradientn(name = "Median",
colours = YlOrBr,
space = "Lab") +
labs(x = "Longitude",
y = "Latitude") +
coord_map()
print(pred.stat.map)
ggsave(filename = "NYSubsamplePredStatMap.png",
plot = pred.stat.map,
scale = 1,
width = 5, height = 3,
dpi = 300)
And finally, to set the colormap to something like http://www.cadmaps.com/images/HeatMapImage.jpg, we can take a guess at the colormap:
colormap <- c("Violet","Blue","Green","Yellow","Red","White")
and do the plotting again:
pred.stat.map.final <- ggmap(map.in) %+% data +
aes(x = GEO_CENTROID_LON,
y = GEO_CENTROID_LAT,
z = prediction) +
stat_summary2d(fun = median,
binwidth = c(.05, .05),
alpha = 1.0) +
scale_fill_gradientn(name = "Median",
colours = colormap,
space = "Lab") +
labs(x = "Longitude",
y = "Latitude") +
coord_map()
print(pred.stat.map.final)