Density2d 使用另一个变量进行填充(类似于 geom_tile)绘图? [英] Density2d Plot using another variable for the fill (similar to geom_tile)?

查看:8
本文介绍了Density2d 使用另一个变量进行填充(类似于 geom_tile)绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

对于每个街区,我都有 Lat、Lon 和犯罪率预测.它遵循以下结构:

纬度/经度/预测-76.0/40.0/125-76.120/40.5/145-75.98/41.001/95

等等.

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

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

欢迎任何帮助.请!

编辑
这是我迄今为止尝试过的:
-geom_密度2d:

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

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

-geom_tiles:

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

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

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

可以在,我们可以猜测一下颜色图:

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

并再次绘制:

pred.stat.map.final <- ggmap(map.in) %+% 数据 +aes(x = GEO_CENTROID_LON,y = GEO_CENTROID_LAT,z = 预测)+stat_summary2d(乐趣 = 中位数,binwidth = c(.05, .05),阿尔法 = 1.0) +scale_fill_gradientn(name = "Median",颜色 = 颜色图,空间 = "实验室") +实验室(x =经度",y = "纬度") +坐标图()打印(pred.stat.map.final)

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

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:

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.

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:

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)

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)

这篇关于Density2d 使用另一个变量进行填充(类似于 geom_tile)绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆