Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

ggplot2 - R plot grid value on maps

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...