title: plotting netcdf data for selected region
body: <p>I am finding it difficult to work with the netcdf data I have. I went through the examples here (<a href="https://stackoverflow.com/questions/47538582/plotting-netcdf-file-with-levels-in-r">Plotting netcdf file with levels in R</a>) but I think im missing something. </p>
  
  <p>I am trying to plot mixed layer depth for region south of 35 degrees. The data can be found here (the last file at the bottom of the page): <a href="http://www.ifremer.fr/cerweb/deboyer/mld/Surface_Mixed_Layer_Depth.php" rel="nofollow noreferrer">http://www.ifremer.fr/cerweb/deboyer/mld/Surface_Mixed_Layer_Depth.php</a></p>
  
  <p>the file has 7 variables each containing the lat, lon, time (12 months), and the value which is the mixed layer depth.</p>
  
  <p>So far I have: </p>
  
  <pre><code>    MLD &lt;- "mld_DReqDTm02_c1m_reg2.0.nc"
      MLD &lt;- nc_open(MLD)
      print(MLD)
  </code></pre>
  
  <p>there are 7 variables and I only want 'mld'</p>
  
  <pre><code>lon &lt;- ncvar_get(MLD, varid = "lon")
  lat &lt;- ncvar_get(MLD, varid = "lat")
  
  summary(lon)
  summary(lat)
  
  
  
  MLD$dim$time$units
  
  MLD_1.array &lt;- ncvar_get(MLD, "mld")
  dim(MLD_1.array)
  
  length(lon)
  length(lat)
  
  ndvi.slice &lt;- MLD_1.array[, , 12] 
  dim(ndvi.slice)
  
  mld.vec.long &lt;- as.vector(MLD_1.array)
  length(mld.vec.long)
  
  nlon &lt;- dim(lon)
  nlat &lt;- dim(lat)
  lonlat &lt;- expand.grid(lon, lat)
  
  t &lt;- ncvar_get(MLD, "time")
  tunits &lt;- ncatt_get(MLD, "time", "units")
  nt &lt;- dim(t)
  
  dname &lt;- "mld" 
  
  tmp.mat &lt;- matrix(mld.vec.long, nrow = nlon * nlat,    ncol = nt)
  dim(tmp.mat)
  
  head(na.omit(tmp.mat))
  
  lonlat &lt;- expand.grid(lon, lat)
  tmp.df02 &lt;- data.frame(cbind(lonlat, tmp.mat))
   names(tmp.df02) &lt;- c("lon", "lat", "Jan", "Feb", "Mar", "Apr", "May", 
  "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
  
  library(reshape)
  tmp.df03 &lt;- melt(tmp.df02, id=c("lat","lon"))
  
  tmp.df04 &lt;- subset(tmp.df03, lat &gt;= "-35" &amp; lat &lt;="-80")
  
   tmp.df04[tmp.df04 ==1.000e+09] &lt;- NA
   tmp.df04
   summary(tmp.df04)
  
  
   target = c(-180, 180, -90, -20)
   w &lt;- crop(rgeos::gBuffer(spTransform(countriesLow, CRS(pprj)), width = 0))
  </code></pre>
  
  <p>I can plot this using ggplot after extracting the data into a .csv, removing the mask values, reimporting it then replotting it (it takes a while...). </p>
  
  <p>Is there a way I can extract one variable (mld) then find the maximum value for each month (time) and plot that? I know my code is really messy and choppy...  </p>
  
  <p>any help would be much appreciated! thank you! </p>