使用geom_tile清理地图

时间:2015-01-20 18:35:44

标签: r ggplot2

感谢本网站上一些用户的帮助,我能够使用geom_point为一些数据获得一个漂亮的地图。 (Get boundaries to come through on states)但是,现在我正在努力清理它,因为我有更多年的情节,并希望确保情节有效并提供良好的信息。经过一些进一步的研究,看起来geom_tile实际上会更好,因为它会避开点并使用渐变。

我遇到的问题是让代码与geom_tile一起使用。这不是任何事情,我不知道为什么。

这是数据集:

https://www.dropbox.com/s/0evuvrlm49ab9up/PRISM_1895_db.csv?dl=0

以下是geom_points的原始代码:

PRISM_1895_db <- read.csv("/.../PRISM_1895_db.csv")

regions<- c("north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas", "illinois", "indiana", "wisconsin")
ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
  geom_point(data = PRISM_1895_db, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) +
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)

enter image description here

这是我一直在尝试的代码,但没有数据出现。

ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
  geom_tile(data = PRISM_1895_db, aes(x = longitude, y = latitude, fill = APPT), alpha = 0.5, color = NA)
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)

enter image description here

1 个答案:

答案 0 :(得分:4)

geom_tile需要在常规网格上对x和y值进行采样。它需要能够以矩形平铺表面。因此,您的数据会被不规则地采样,因此无法将原始数据划分为一堆漂亮的图块。

一种选择是使用stat_summary2d图层将数据划分为多个框,并计算该框中所有点的平均APPT。这将允许您创建常规切片。例如

ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
  stat_summary2d(data=PRISM_1895_db, aes(x = longitude, y = latitude, z = APPT)) +
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)

产生

enter image description here

如果您愿意,可以查看其他选项来控制此bin尺寸。但正如你所看到的那样,它通过取出箱内的平均值来“平滑”数据。

相关问题