Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

r - Density2d Plot using another variable for the fill (similar to geom_tile)?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, let's load the data:

data<-read.csv(file = "NY subsample.csv")

Data points

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: enter image description here

Binned data

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.

enter image description here

Binned and with custom colormap and correct projection

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:

enter image description here

Plotted over a base map

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)

enter image description here

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:

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...