R地图在地图上填充经度 - 纬度网格单元格 [英] R Plot Filled Longitude-Latitude Grid Cells on Map
问题描述
我有一个数据框,它包含一些(x,y,z)
数据点,(x,y)
是大小 w
(例如1度网格)的经度纬度单元的右下角坐标。
我想在R中绘制这些点,这样整个网格单元填充了一些来自 z
的颜色。
结果看起来像这些之一images:
投影本身(例如朗伯共形圆锥曲线,equirectangular)并不重要,只是网格单元格绘图。我的数据很稀疏:不是每一个纬度经度细胞将有数据与它相关联。
我的希望将会是一个类似于
<
map(map)
map(state)
grid_points(my_data,c(x,y,z),0.5)
其中0.5是上面的网格分辨率,表示0.5度单元格。
有什么想法?
谢谢!
使用 spplot
或 image
是使用 ggplot2
。相关的几何图形是 geom_raster
和 geom_tile
。第一个应该表现更好,产生的文件更小,第二个更标准。以下示例调用:
ggplot(aes(x = x,y = y,fill = value),data = dat_grid) + geom_tile()+
geom_path(data = ant_ggplot)
orginates from 我的这篇博客。另外, ggplot2
支持一系列通过 mapproj
包,请参阅 coord_map
了解更多详情。
YOUR_DATA
定义为x,y,z列): <$ c $ (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),color =black,fill =white,alpha = 0)
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.
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:
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.
My hope would be a solution similar to
library(maps)
map("state")
grid_points(my_data,c("x","y","z"),0.5)
where 0.5 is the grid resolution above, indicating a 0.5-degree cell.
Any thoughts?
Thanks!
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)
orginates from this blogpost of mine. In addition, ggplot2
supports a range of projections through the mapproj
package, see coord_map
for more details.
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屋!