title: How to compute land cover area in R
body: <p>Basically, I computed a global distribution probability model in the form of ASCII, say:
  <code>gdpm</code>. <code>gdpm</code>'s values are all between 0 and 1.</p>
  
  <p>Then I imported a local map from shape file:</p>
  
  <pre><code>shape &lt;- file.choose()  
  map &lt;- readOGR(shape, basename(file_path_sans_ext(shape)))
  </code></pre>
  
  <p>The next step, I rasterized <code>gdpm</code>, and cropped using the local map:</p>
  
  <pre><code>ldpm &lt;- mask(gdpm, map)
  </code></pre>
  
  <p>Then, I reclassified this continuous model into a discrete model (I divided the model into 6 levels):</p>
  
  <pre><code>recalc &lt;- matrix(c(0, 0.05, 0, 0.05, 0.2, 1, 0.2, 0.4, 2, 0.4, 0.6, 3, 0.6, 0.8, 4, 0.8, 1, 5), ncol = 3, byrow = TRUE) 
  
  ldpmR &lt;- reclassify(ldpm, recalc)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/W7IAJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W7IAJ.jpg" alt="enter image description here"></a></p>
  
  <p>I've got a cropped and reclassified raster, now I need to summarize land cover, that is, <strong>to each level, I want to calculate its proportion of area in each region of the local map.</strong> (I don't know how to describe it in terminology). I found and followed an example (<a href="https://stackoverflow.com/questions/34143320/how-can-i-speed-up-extraction-of-the-proportion-of-land-cover-types-in-a-buffer">RobertH</a>):</p>
  
  <pre><code>ext &lt;- raster::extract(ldpmR, map)
  
  tab &lt;- sapply(ext, function(x) tabulate(x, 10))
  tab &lt;- tab / colSums(tab)
  </code></pre>
  
  <p>But I'm not sure if it works, since the output of <code>tab</code> is confusing. 
  <strong>So how to compute land cover area correctly? How can I apply the correct method within each polygon?</strong></p>
  
  <p>My original data is too large, I can only provide an alternative raster (I think this example should apply a different reclassify matrix):</p>
  
  <p><a href="http://biogeo.ucdavis.edu/data/climate/cmip5/2_5m/cc26bi50.zip" rel="nofollow noreferrer">Example raster</a></p>
  
  <p>Or you can generate a test raster (<a href="https://stackoverflow.com/questions/47591678/how-to-batch-process-geotiffs-in-r-with-lapply">RobertH</a>):</p>
  
  <pre><code>library(raster)
  s &lt;- stack(system.file("external/rlogo.grd", package="raster")) 
  writeRaster(s, file='testtif', format='GTiff', bylayer=T, overwrite=T)
  f &lt;- list.files(pattern="testtif_..tif")
  </code></pre>
  
  <p>I also have a question about plotting a raster:</p>
  
  <pre><code>r &lt;- as(r, "SpatialPixelsDataFrame")
  r &lt;- as.data.frame(r)
  colnames(r) &lt;- c("value", "x", "y")
  </code></pre>
  
  <p>I do this conversion to make a raster plot-able with ggplot2, is there a more concise method?</p>