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 points
Then, let's try just plotting the basic locations and values of the data:
which gives us this:
Binned data
Then, you can try to plot the mean in a 2-D region using
stat_summary2d()
:which gives us this plot of the mean value of prediction in each box.
Binned and with custom colormap and correct projection
Next, we can set the bin size, color scales, and fix the projection:
which gives us this:
Plotted over a base map
And last of all, here's the data overlain on a map.
Setting the colormap
And finally, to set the colormap to something like http://www.cadmaps.com/images/HeatMapImage.jpg, we can take a guess at the colormap:
and do the plotting again:
I'm still not really sure what you're aiming at, but if you want a map with some points and contours plotted on it, that is possible. The output looks like the screenshot below; obviously there are many ways that could be tweaked. (The state shown is Connecticut.)
Code follows: