使用geom_tile清理地图 [英] Cleaning up a map using geom_tile

查看:145
本文介绍了使用geom_tile清理地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢本网站上一些用户的帮助,我能够使用geom_point为一些数据获得一个不错的地图图。 (获取边界通过状态)但是,现在我'我试图清理它,因为我有更多年的阴谋,并希望确保剧情工作,并提供良好的信息。经过一些进一步的研究,似乎geom_tile会更好,因为它会避开点并使用渐变。

我遇到的问题是让代码与geom_tile协同工作。这不是什么阴谋,我不知道为什么。



以下是数据集:

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



以下是geom_points的原始代码:

 <$ c (北达科他州,南达科他州,内布拉斯加州,北达科他州,北达科他州,堪萨斯州,俄克拉何马州,德克萨斯州,明尼苏达州,爱荷华州,密苏里州,阿肯色州,伊利诺伊州,印第安纳州,威斯康星州)
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 y =纬度,颜色= 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)



这里是

  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 =纬度,fill = APPT),alpha = 0.5,color = NA)
geom_polygon(data = subset(map_data(state),%region中的region%),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 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的大小,如果你喜欢的话。但正如你所看到的那样,通过在仓内取平均值来平滑数据。


Thanks to help from some users on this site, I was able to get a nice map plot for some data using geom_point. (Get boundaries to come through on states) However, now I'm trying to clean it up as I have more years to plot and want to make sure the plot is working and providing good information. After some further research, it seems the geom_tile would actually be better for this as it would shy away from points and use a gradient.

The problem I'm running into is getting the code to work with geom_tile. It isn't plot anything and I'm not sure why.

Here's the dataset :

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

Here's the original code with 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)

And here is the code I've been trying, but none of the data is showing up.

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 needs your x and y values to be sampled on an regular grid. It needs to be able to tile the surface in rectangles. So your data is irregularly sampled, it's not possible to divide up the raw data into a bunch of nice tiles.

One option is to use the stat_summary2d layer to divide your data into boxes and calculate the average APPT for all points in that box. This will allow you to create regular tiles. For example

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)

which produces

you can look at other options to control this bin sizes if you like. But as you can see it's "smoothing" out the data by taking averages inside bins.

这篇关于使用geom_tile清理地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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