[英]R plot grid value on maps
I have several file with the following format: lat,lon,value. 我有几个文件,格式如下:lat,lon,value。 I would like to plot a colored grid map with such value for each one of the lat,lon point and overlapping it over the EU map.
我想绘制一个彩色的网格地图,每个纬度,经度点都具有这样的值,并将其重叠在欧盟地图上。
Thanks for support. 感谢你的支持。
There are roughly two options: one using the lattice and sp package and the other using the ggplot2 package: 大致有两种选择:一种使用grid和sp软件包,另一种使用ggplot2软件包:
First you have to transform your data to a SpatialPixelsDataFrame
(provided by the sp-package), assuming your data is called df
: 首先,假设您的数据称为
df
,则必须将数据转换为SpatialPixelsDataFrame
(由sp-package提供)。
require(sp)
gridded(df) = c("lat","lon")
and then plotting: 然后绘制:
spplot(df, "value")
overlaying additional information such as country boundaries can be done using the sp.layout
argument: 可以使用
sp.layout
参数覆盖其他信息(例如国家边界):
spplot(df, "value", sp.layout = list("sp.lines", cntry_boundaries))
where cntry_boundaries
is a SpatialLines(DataFrame)
, or SpatialPolygons(DataFrame)
. 其中
cntry_boundaries
是SpatialLines(DataFrame)
或SpatialPolygons(DataFrame)
。 Reading a polygonset into R can be done using the rgdal
package, or the maptools
package ( readShapeLines
). 可以使用
rgdal
包或maptools
包( readShapeLines
)将多边形集读入R中。
I personally prefer using ggplot2
over spplot
. 我个人更喜欢使用
ggplot2
不是spplot
。 ggplot2
is more flexible and has a more clear syntax. ggplot2
更灵活,语法更清晰。 Note that ggplot2
works with normal data.frame
's, and not with the spatial objects of the sp-package. 请注意,
ggplot2
适用于普通的data.frame
,不适用于sp-package的空间对象。
A minimal example would look something like: 一个最小的示例如下所示:
ggplot(aes(x = lat, y = lon), data = df) + geom_tile(aes(fill = value)) +
geom_path(data = cntry_boundaries)
For more information see these earlier answers of mine: 有关更多信息,请参见我的以下较早答案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.