title: Plot NetCDF variable-grid data file using ggplot2: "Vector is too large" error
body: <p>I need to plot some data from <a href="https://copy.com/ZZk2oJNXxCTVJ4eu" rel="nofollow noreferrer">this</a> NetCDF file (1.1MB).</p>
  
  <p>The file contains a 413x229 grid (94577 points). Every point has a precipitation value which I need to print in the correct LAT-LON. The grid is not necessarily constant, so that we have two additional 413x229 variables (xlat and xlon) containing the lat-lon values for each grid point.</p>
  
  <p>Thanks to the hints from these questions...</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16744375/how-to-plot-contours-on-a-map-with-ggplot2-when-data-is-on-an-irregular-grid">How to plot contours on a map with ggplot2 when data is on an irregular grid?</a></li>
  <li><a href="https://stackoverflow.com/questions/20520837/r-plotting-netcdf-climate-data">R - Plotting netcdf climate data</a></li>
  <li><a href="https://stackoverflow.com/questions/22301254/plotting-netcdf-in-r-with-correct-grid">Plotting netcdf in R with correct grid</a></li>
  <li><a href="https://stackoverflow.com/questions/22833647/how-to-plot-latitude-longitude-points-over-netcdf-map">How to plot latitude/longitude points over netcdf map</a></li>
  <li><a href="https://stackoverflow.com/questions/22301254/plotting-netcdf-in-r-with-correct-grid">Plotting netcdf in R with correct grid</a></li>
  <li><a href="https://stackoverflow.com/questions/14865507/how-to-display-a-projected-map-on-an-rlatticelayerplot">how to display a projected map on an R::lattice::layerplot?</a></li>
  </ul>
  
  <p>...plotting the data on an arbitrary grid using ggplot is fairly easy:</p>
  
  <pre><code>library(ncdf4)
  library(ggplot2)
  library(reshape)
  ncfile &lt;- nc_open(inputfile.nc)
  pr &lt;- ncvar_get(ncfile, "pr")
  pr &lt;- pr*86400
  mpr &lt;- melt(pr)
  ggplot(aes(x=X1, y=X2, fill=value), data=mpr) + geom_raster() + coord_equal()
  </code></pre>
  
  <p>This will produce a plot with a grid which is not lat-lon of course.
  However, when trying to plot the same data with the correct grid:</p>
  
  <pre><code>xlon &lt;- ncvar_get(ncfile, "xlon")
  xlat &lt;- ncvar_get(ncfile, "xlat")
  df &lt;- data.frame(as.vector(lat), as.vector(lon), as.vector(pr))
  ggplot(aes(x=lat, y=lon, fill=pr), data=df) + geom_raster() + coord_equal()
  </code></pre>
  
  <p>ggplot will return a "vector is too large" error and plot nothing.<br>
  pr, lat and lon are 413x229 arrays.</p>
  
  <p>So, questions:</p>
  
  <ol>
  <li>What am I doing wrong?</li>
  <li>How can I easily customize fixed non-continuous contour levels in ggplot?</li>
  <li>How can I overlay a political map of the plotted area? (Alps) I've not looked yet into this as it is of secondary importance. It may be answered in one of the questions listed above, so feel free to ignore it.</li>
  </ol>
  
  <p>EDIT:
  Answers suggest I should use the <code>raster</code> and <code>rasterVis</code> packages.
  However, I'm not sure on how to do that. The file does not provide any more info. I know it's a Lambert Conformal projection with :</p>
  
  <pre><code>latitude_of_projection_origin = 39.
  longitude_of_projection_origin = 14.
  standard_parallel = 35., 51. 
  grid_factor = 0.684241343018562
  </code></pre>
  
  <p>You can see this by "ncdump -h".</p>
  
  <p>However I don't understand how to plot this using <code>levelplot()</code> on the correct lat-lon grid. It really looks troublesome to me. I know how to do this in GrADS quite simply, but GrADS is very limiting and I'd prefer to avoid using it honestly.</p>