R 在地图上绘制填充的经纬度网格单元 [英] R Plot Filled Longitude-Latitude Grid Cells on Map
问题描述
我有一个包含多个 (x,y,z)
数据点的数据框,(x,y)
是经度的右下坐标- 大小为 w
的纬度单元(例如 1 度网格).z
值已在此单元格上取平均值.
I have a data frame containing a number of (x,y,z)
data points, (x,y)
is the lower-right coordinate of a longitude-latitude cell of size w
(e.g. a 1-degree grid). The z
value has been averaged over this cell.
我想在 R 中绘制这些点,以便整个网格单元都填充一些源自 z
的颜色.
I'd like to plot these points in R so that the entire grid cell is filled with some colour derived from z
.
结果将类似于以下图像之一:
The result would look something like one of these images:
投影本身(例如 Lambert 等角圆锥曲线、等距柱状投影)并不重要,重要的是网格单元绘制.
The projection itself (e.g. Lambert conformal conic, equirectangular) isn't important, just the grid cell plotting.
我的数据很稀疏:并非每个经纬度单元格都会有与之关联的数据.
My data is sparse: not every longitude-latitude cell will have data associated with it.
我希望是一个类似于
library(maps)
map("state")
grid_points(my_data,c("x","y","z"),0.5)
其中 0.5 是上面的网格分辨率,表示 0.5 度的单元格.
where 0.5 is the grid resolution above, indicating a 0.5-degree cell.
有什么想法吗?
谢谢!
推荐答案
使用 spplot
或 image
的替代方案是使用 ggplot2
.相关的几何图形是 geom_raster
和 geom_tile
一>.第一个应该表现更好并产生更小的文件,第二个更标准.以下示例调用:
An alternative to using either spplot
or image
is to use ggplot2
. The relevant geometries are geom_raster
and geom_tile
. The first is supposed to perform better and yield smaller files, and the second is more standard. The following example call:
ggplot(aes(x = x, y = y, fill = value), data = dat_grid) + geom_tile() +
geom_path(data = ant_ggplot)
源自这篇博文 我的.此外,ggplot2
通过 mapproj
包,见coord_map
了解更多详情.
orginates from this blogpost of mine. In addition, ggplot2
supports a range of projections through the mapproj
package, see coord_map
for more details.
以下是一个工作示例(假设您已将 YOUR_DATA
定义为具有 x,y,z 列):
The following is a working example (provided you've defined YOUR_DATA
to have x,y,z columns):
library(ggplot2)
library(maps)
us_states <- map_data("state")
(ggplot(aes(x=x,y=y,fill=z),data=YOUR_DATA) + geom_tile())+geom_polygon(data=us_states,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0)
这篇关于R 在地图上绘制填充的经纬度网格单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!