R plot grid value on maps

拥有回忆 提交于 2019-12-18 04:27:26

问题


I have several file with the following format: 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.


回答1:


There are roughly two options: one using the lattice and sp package and the other using the ggplot2 package:

lattice / sp

First you have to transform your data to a SpatialPixelsDataFrame (provided by the sp-package), assuming your data is called df:

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:

spplot(df, "value", sp.layout = list("sp.lines", cntry_boundaries))

where cntry_boundaries is a SpatialLines(DataFrame), or SpatialPolygons(DataFrame). Reading a polygonset into R can be done using the rgdal package, or the maptools package (readShapeLines).

ggplot2

I personally prefer using ggplot2 over spplot. ggplot2 is more flexible and has a more clear syntax. Note that ggplot2 works with normal data.frame's, and not with the spatial objects of the 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:

  • Plotting interpolated data on map
  • Average values of a point dataset to a grid dataset


来源:https://stackoverflow.com/questions/11081707/r-plot-grid-value-on-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!