首页 > 解决方案 > 使用 stat_density2d 显示计数而不是“级别”

问题描述

我的目标是描绘道路交叉口中具有不同数量交通冲突的位置。我的数据包含我们在给定时间段内观察到的所有冲突,这些冲突被编码为 .CSV 文件,其中包含以下字段“冲突时间”、“TTC”(表示碰撞时间)、“纬度”、“经度” ”和“冲突类型”。我认为最好的方法是在 R 中使用“ggmap+stat_density2d”函数。我正在使用以下代码:

df = read.csv(filename, header = TRUE)
int.map = get_map(location = c(mean.long, mean.lat), zoom = 20, maptype = "satellite")
int.map = ggmap(int.map, extent ="device", legend = "right")'''
int.map +stat_density2d(data = new_xdf, aes(x, y, fill = ..levels.., alpha = ..levels..),
                                 geom = "polygon")
int.map + scale_fill_gradientn(guide = "colourbar", colours = rev(brewer.pal(7,"Spectral")),
                                         name = "Conflict Density")

输出是一个非常漂亮的地图安全热图,它正确地描绘了冲突热点。我的问题是,在图例中,它给出了由“stat_density2d()”函数自动计算的“级别”值。我尝试寻找一种方法来显示图例栏上每个级别内所有冲突点的计数,但无济于事。

我确实找到了处理类似问题的以下链接,但问题在于它创建了一个新数据框(new_xdf),其点数比原始数据中的多得多。因此,该程序中确定的计数似乎对我没有用,因为我希望在图例栏中显示原始数据中冲突点的确切数量。

如何在R中的轮廓内找到点?

提前致谢。

编辑:链接到示例数据文件

https://docs.google.com/spreadsheets/d/11vc3lOhzQ-tgEiAXe-MNw2v3fsAqnadweVrvBdNyNuo/edit?usp=sharing

标签: rspatialggmapstat-density2d

解决方案


推荐阅读