Density2d 使用另一个变量进行填充(类似于 geom_tile)?

2024-05-04

我正在尝试为我的最终项目绘制地图,并且正在尝试使用 BLock 绘制美国的犯罪热图。

对于每个街区,我都有纬度、经度和犯罪率预测。它遵循以下结构:

Lat           /      Lon          /         Prediction
-76.0         /     40.0          /        125   
-76.120       /      40.5          /       145
-75.98        /      41.001        /         95

等等。

有没有办法绘制将预测显示为填充的热图?

我认为这就是 geom_tiles 所做的,但是 geom 不起作用(可能是因为点间隔不均匀)

任何帮助将非常受欢迎。请!

EDIT
这是我到目前为止所尝试过的:
-geom_密度2d:

ggplot(ny2,aes(x=GEO_CENTROID_LON,y=GEO_CENTROID_LON,fill=prediction))+geom_density2d()  

给我错误:“单位错误(tic_pos.c,“mm”):“x”和“单位”的长度必须> 0”

-geom_tiles:

ggplot(ny2,aes(x=GEO_CENTROID_LON,y=GEO_CENTROID_LON,fill=prediction))+geom_tile()  

生成具有适当比例的绘图,但不显示地图上的数据。

关于等值线,如果我碰巧有整个美国的块级信息,它会起作用,但我找不到这样的数据。

可以找到数据的子样本here https://www.dropbox.com/s/nuvuv3423uduje5/NY%20Subsample.csv


首先,我们加载数据:

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

数据点

然后,让我们尝试绘制数据的基本位置和值:

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

分箱数据

然后,您可以尝试使用以下方法绘制二维区域中的平均值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)

这为我们提供了每个框中预测平均值的图。

分箱并具有自定义色彩图和正确的投影

接下来,我们可以设置 bin 大小、色阶并修复投影:

# 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)

这给了我们这个:

绘制在底图上

最后,这是地图上覆盖的数据。

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)

设置颜色图

最后,将颜色图设置为类似http://www.cadmaps.com/images/HeatMapImage.jpg http://www.cadmaps.com/images/HeatMapImage.jpg,我们可以猜测一下颜色图:

colormap <- c("Violet","Blue","Green","Yellow","Red","White")

并再次进行绘图:

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Density2d 使用另一个变量进行填充(类似于 geom_tile)? 的相关文章

随机推荐