r - 使用 R/ggplot/ggmap 填充等高线图

标签 r ggplot2 ggmap

我在 R 中使用 ggmap/ggplot2 在 map 顶部绘制填充轮廓图时遇到问题。

我的数据是规则间隔的纬度/经度坐标,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 获取 basemap 后,我正在尝试绘制填充的雨等高线
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 post
this post ,但我无法解决我的问题。我对 ggplot/R 知之甚少,但到目前为止我已经能够偶然发现它。 ……级别……是什么意思?

我想 this post可能是相关的,但我不能概括修复等高线图的工作。

最佳答案

没有更具代表性的数据集就无法进行测试(您能提供链接吗?)。

不过,请尝试:

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

关于r - 使用 R/ggplot/ggmap 填充等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21290230/

相关文章:

r - data.table 的 'j' 新添加的列应该在范围内可用

R graph 仅针对指定级别按级别重新排序因子

r - 在 R 中使用对数刻度上的 stat_function 绘图

r - 使用 ggmap 绘制 GPS 坐标

r - 在 Shiny 应用程序中使用函数渲染 UI(selectInput())

r - 将scale_color与类日期变量一起使用会产生错误: as. Date.numeric(value):必须提供 'origin'

r - 找不到函数 "CreateSinglerObject"

r - 使用 ggplot2 制作带有分类轴的散点图

r - 在 map 上绘制坐标