用R/ggplot/ggmap绘制填充等高线图

5

我在使用R中的ggmap/ggplot2绘制地图上带填充等高线图时遇到了问题。

我的数据是经纬度坐标,间隔规则,并且具有表示降雨量的z值。

> head( flood )
   lat       lon         rain
1 22.51916 -105.9318 1.486188e-05
2 22.59956 -105.9318 1.735962e-05
3 22.67996 -105.9318 2.024598e-05
4 22.76037 -105.9318 2.357599e-05
5 22.84077 -105.9318 2.741153e-05
6 22.92117 -105.9318 3.182212e-05

在使用ggmap获取底图之后,我正在尝试叠加填充的降雨等值线。

map = ggmap( baseMap ) + 
    geom_contour( data = flood, aes( x = lon, y = lat, z = rain ) ) +
    scale_fill_continuous( name = "Rainfall (inches)", low = "yellow", high = "red" ) 

这让我出现了一个错误,错误信息为:
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0

如果我执行

map = ggmap( baseMap ) + 
    geom_contour( data = flood, aes( x = lon, y = lat, z = rain, fill = ..level.. ) ) +
    scale_fill_continuous( name = "Rainfall (inches)", low = "yellow", high = "red" ) 

我没有实际的填充,但得到了这个图。 enter image description here

我一直在试着遵循这篇文章这篇文章,但是对于我的问题我无法搞定。我对ggplot/R不太熟悉,但我迄今为止已经能够应付它了。"..level.."是什么意思?

我认为这篇文章可能是相关的,但我无法将修复内容推广到等高线图。

1个回答

8

如果您能提供一个更具代表性的数据集(您可以提供链接吗?),那么进行测试就会变得可行。

不过,您也可以尝试以下方法:

## not tested..
map = ggmap( baseMap ) + 
    stat_contour( data = flood, geom="polygon", 
                  aes( x = lon, y = lat, z = rain, fill = ..level.. ) ) +
    scale_fill_continuous( name = "Rainfall (inches)", low = "yellow", high = "red" ) 

问题在于geom_contour不支持 "fill=..."。你需要使用stat_contour(...),其中geom="polygon"(而不是 "line")。

谢谢,这个完美地解决了。你能解释一下..level..是从哪里来的或者它是如何被设置的吗? - jjardel
3
将你的 z 数据进行分组以创建等高线图(每个组一个等高线),..levels.. 指的是组编号,映射到该组的 z 值范围。注意不要改变原意,使翻译内容通俗易懂,但不要添加解释或其他信息。 - jlhoward

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接