2014-01-22 192 views
4

我无法在R.与ggmap/GGPLOT2地图结合z值与填充等高线图R/ggplot/ggmap

我的数据被规则地间隔开的纬度/经度坐标的顶部绘制填充等高线图表明降雨

> 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后,我试图overplot雨

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

我一直努力遵循this postthis post,但我无法得到它适合我的问题。我对ggplot/R知之甚少,但到目前为止,我已经能够蹒跚过去。 ..level ..是什么意思?

我认为this post可能是相关的,但我不能概括修复工作的轮廓图。

回答

6

如果没有更具代表性的数据集进行测试是不可能的(您能否提供链接?)。

然而,尝试:

## 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"(而不是“行”)。

+0

谢谢,这工作完美。你能解释一下......level ..从哪里来,或者它是如何设置的? – jjardel

+3

您的z数据被分档以创建轮廓(每个圆柱1轮廓)。 '..levels..'指的是箱号,映射到该箱的z范围。 – jlhoward