title: ggplot2 - how to fill nested polygons with colour?
body: <p>I have > 100 nested polygons in a unique <code>SpatialPolygonsDataFrame</code>. I desire to plot them with <code>ggplot2</code> and <strong>all of them need to be visible in the map</strong>, i.e. overlaying larger polygons have to be in the background.</p>
  
  <p>I found that I can achieve this by using <code>alpha = 0</code> within the <code>geom_polygon</code> function but <strong>how can I assign a fill colour to each polygon?</strong></p>
  
  <p>Here an example of my code with just 2 polygons:</p>
  
  <pre><code>library(ggplot2)
  </code></pre>
  
  <p>Read csv file with two shapefiles merged and then converted to data.frame with <code>fortify</code> from <code>maptools</code>.</p>
  
  <pre><code>#read csv file shape_1_2.csv
  shape_1_2 = read.csv('shape_1_2.csv', stringsAsFactors = FALSE)
  
  #plot
  ggplot() +
  geom_polygon(data = shape_1_2, aes(x = long, y = lat, group = group), 
               colour = 'black', size = 1, linetype = 'solid', alpha = 0)
  </code></pre>
  
  <p>And relative map:</p>
  
  <p><a href="https://i.stack.imgur.com/iddNR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iddNR.jpg" alt="enter image description here"></a></p>
  
  <p><strong>How can I fill with colour these two polygons?</strong></p>
  
  <p>I tried to add <code>fill='black'</code> in both <code>aes</code> and <code>geom_polygon</code> but it doesn't work.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>Update</p>
  
  <p>I am sorry but I realised that my example data contains NO nested polygons.</p>
  
  <p>So starting from the following data.frame as per <a href="https://gis.stackexchange.com/questions/280671/r-create-multipolygon-from-overlapping-polygons-using-sf-package">https://gis.stackexchange.com/questions/280671/r-create-multipolygon-from-overlapping-polygons-using-sf-package</a> :</p>
  
  <pre><code>shape_df = data.frame(
      lon = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2, 2, 0.8, 1, 1, 2, 2, 1, 1),
      lat = c(0, 0, 1, 1.5, 0, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 0, 0, 1, 1, 0),
      var = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3 ,3 ,3 ,3 ,3, 4 ,4 ,4, 4, 4)
  )
  </code></pre>
  
  <p>And my plotting code (with <code>alpha=0</code>):</p>
  
  <pre><code>ggplot() +
      geom_polygon(data = shape_df, aes(x = lon, y = lat, group = var), 
                   colour = 'black', size = 1, linetype = 'solid', alpha = 0)
  </code></pre>
  
  <p>With relative map:</p>
  
  <p><a href="https://i.stack.imgur.com/mG7C0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mG7C0.jpg" alt="enter image description here"></a></p>
  
  <p><strong>How can I fill the different areas present in the map with one OR up to 4 colours so that the larger polygons remains in the background of the smaller?</strong></p>