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 the crime rate. It follows this structure:
Lat / Lon / Prediction
-76.0 / 40.0 / 125
-76.120 / 40.5 / 145
-75.98 / 41.001 / 95
And so on.
Is there a way to plot a heat map showing the Prediction as the fill?
I think this is what geom_tiles do, but that geom is not working (maybe because the points are not evenly spaced)
Any help would be more than welcome. Please!
EDIT
This is what I have tried so far:
-geom_density2d:
ggplot(ny2,aes(x=GEO_CENTROID_LON,y=GEO_CENTROID_LON,fill=prediction))+geom_density2d()
Gives me the error: "Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0"
-geom_tiles:
ggplot(ny2,aes(x=GEO_CENTROID_LON,y=GEO_CENTROID_LON,fill=prediction))+geom_tile()
Produces a plot with the proper scale, but not data shown on the map.
Regarding chloropeth, it would work if I happend to have block level information for the whole US, but I can't find such data.
SUBSAMPLE of data can be found here
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With