使用R中的ggplot绘制netcdf gridfile

时间:2014-03-14 16:00:49

标签: r plot ggplot2 maps netcdf

我想使用正确的经度和纬度(在this question中解析) R的函数 ggplot 来绘制netcdf文件。

数据取决于x,y,sigma。因此,选择了某个sigma(=' depth')级别(sigma = 20),将对其进行绘图(在变量sigma_plot中保存为x * y双矩阵)。每个x和y对应于某个经度(plon)和纬度(plat)。

现在可以通过三种方式获取数据(如果需要详细信息,请查看上面的问题):

  • 答:这是一个长度为x * y * 3的数组,其中3代表plon,plat和数据
  • B:x * y * 3双矩阵
  • C:是一个列表[3],包含每个经度和纬度的相应绘图值,插值到标准网格(xo = seq(-180,180,1),yo = seq(-90,90,by = 1))。

但是,我不知道ggplot需要正确想象这些内容。我尝试了以下方法:(我是ggplot的新手)

ggplot(aes(x=B[1],y=B[2]),data=B[3]) +
  geom_raster() +
  scale_fill_gradient()

返回:

Error: ggplot2 doesn't know how to deal with data of class numeric

并且:

ggplot(aes(x=data.frame(C[1]),y=data.frame(C[2])),data=data.frame(C[3])) +
  geom_raster() +
  scale_fill_gradient()

返回

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Error in data.frame(x = list(x = c(-180, -179, -178, -177, -176, -175,  : 
  arguments imply differing number of rows: 361, 181

而且,似乎对我来说最合乎逻辑

ggplot(aes(x=plon,y=plat),data=data.frame(sigma_plot)) +
  geom_raster() +
  scale_fill_gradient()

返回:

Error in `$<-.data.frame`(`*tmp*`, "xmin", value = numeric(0)) : 
  replacement has 0 rows, data has 180
In addition: Warning messages:
1: In min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
2: In max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf
3: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf
4: In min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
5: In max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf
6: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf

我对用于创建C的插值感到不满意,因为它可能无法保存质量/能量/体积等。所以后一种方法会有我的偏好(使用plon和plat以及sigma_plot表面)。

我非常感谢对此有所帮助。

0 个答案:

没有答案