使用 geom_tile 清理地图
Cleaning up a map using geom_tile
感谢此站点上一些用户的帮助,我能够使用 geom_point 为某些数据获得漂亮的地图。 () 但是,现在我正在尝试清理它,因为我有更多年的时间来绘制并希望确保该图有效并提供良好的信息。经过进一步研究,似乎 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)
这是我一直在尝试的代码,但是显示了 none 的数据。
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)
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)
产生
如果您愿意,可以查看其他选项来控制此 bin 大小。但是正如您所看到的,它是 "smoothing" 通过在 bin 内取平均值来得出数据的。
感谢此站点上一些用户的帮助,我能够使用 geom_point 为某些数据获得漂亮的地图。 (
我 运行 遇到的问题是让代码与 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)
这是我一直在尝试的代码,但是显示了 none 的数据。
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)
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)
产生
如果您愿意,可以查看其他选项来控制此 bin 大小。但是正如您所看到的,它是 "smoothing" 通过在 bin 内取平均值来得出数据的。