1

I would like to plot a netcdf file using correct longitude and latitude (solved in this question) and the function ggplot of R.

The data depend on x,y,sigma. Therefore, a certain sigma (='depth') level was chosen (sigma=20) for which the plot will be made (saved as an x*y double matrix in the variable sigma_plot). Each x and y correspond to a certain longitude (plon) and latitude (plat).

The data are now available in three ways (have a look at above question if you want details):

  • A: This is an array of length x*y*3 with 3 representing plon, plat and the data
  • B: An x*y*3 double matrix
  • C: is a list[3] that contains for every longitude and latitude the corresponding value to plot, interpolated to a standard grid (xo=seq(-180,180,1),yo=seq(-90,90,by=1)).

However, I don't know what ggplot needs to correctly visualize these. I tried the following: (I am new to ggplot)

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

which returns:

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

And:

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

which returns

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

And, which seems to be the most logical to me:

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

Which returns:

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

I am not fully happy with the interpolation used to create C, since it may not conserve mass/energy/volume/etc.. So the latter approach would have my preference (using the plon and plat and the sigma_plot surface).

I would greatly appreciate some help with this.

Community
  • 1
  • 1
user2003479
  • 121
  • 4

0 Answers0