Legends for multiple fills in ggplot
<p>I am a beginner in <code>ggplot2</code>. So, I apologize if this question sounds too basic. I'd appreciate any guidance. I've spent 4 hours on this and looked at this SO thread <a href="https://stackoverflow.com/questions/18394391/r-custom-legend-for-multiple-layer-ggplot">R: Custom Legend for Multiple Layer ggplot</a> for guidance, but ended up nowhere.</p>

<p><strong>Objective:</strong> I want to be able to apply legend to different fill colors used for different layers. I am doing this example just for the sake of testing my understanding of applying concepts <code>ggplot2</code> concepts.</p>

<p>Also, I do <strong>NOT</strong> want to change the shape type; changing fill colors is fine--by "fill" I do not mean that we could change "color". So, I would appreciate if you can correct my mistakes in <em>my</em> work.</p>

<p><strong>Try 1:</strong>
Here's the bare bones code without any colors set manually.</p>

<pre><code>ggplot(mtcars, aes(disp,mpg)) +
geom_point(aes(fill = factor(vs)),shape = 23, size = 8, alpha = 0.4) +
geom_point (aes(fill = factor(cyl)),shape = 21, size = 2) +
geom_rect(aes(xmin = min(disp)-5, ymax = max(mpg) + 2,fill = "cyan"), 
xmax = mean(range(mtcars$disp)),ymin = 25, alpha = 0.02) ##region for high mpg
</code></pre>

<p>The output looks like this:
<a href="https://i.stack.imgur.com/UgRSr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UgRSr.png" alt="enter image description here"></a></p>

<p>Now, there are a few problems with this image:</p>

<p><strong>Issue 1)</strong> The cyan rectangle that shows "high mpg areas" has lost its legend.</p>

<p><strong>Issue 2)</strong> ggplot tries to combine the legend from the two <code>geom_point()</code> layers and as a result the legend for the two <code>geom_point()</code> are also mixed.</p>

<p><strong>Issue 3)</strong> The default color paleltte used by <code>ggplot2</code> makes the colors non-distinguishable for my eyes.</p>

<p>So, I took a stab at manually setting the colors i.e.start with fixing #3 above.</p>

<pre><code>ggplot(mtcars, aes(disp,mpg)) + 
       geom_point(aes(fill = factor(vs)),shape = 23, size = 8, alpha = 0.4)+
       geom_point(aes(fill = factor(cyl)),shape = 21, size = 2) +
       geom_rect(aes(xmin = min(disp)-5, ymax = max(mpg) + 2,fill = "cyan"),
                 xmax = mean(range(mtcars$disp)),ymin = 25, alpha = 0.02) + 
     scale_fill_manual(values = c("green","blue", "black", "cyan", "red", "orange"), 
labels=c("4 gears","6 gears","8 gears","High mpg","0","1"))
</code></pre>

<p>Here's the output:
<a href="https://i.stack.imgur.com/tZexy.png" rel="noreferrer"><img src="https://i.stack.imgur.com/tZexy.png" alt="enter image description here"></a>
Unfortunately, some of the problems highlighted above persist. There is new issue about ordering. </p>

<p><strong>Issue#4:</strong> It seems to me that <code>ggplot2</code> expects me to provide colors in the order the layers were set. i.e. first set the color for <code>mtcars$vs</code> fill, then <code>mtcars$cyl</code> fill and finally the rectangle with cyan color. I was able to fix it by modifying the code to:</p>

<pre><code>ggplot(mtcars, aes(disp,mpg)) + 
       geom_point(aes(fill = factor(vs)),shape = 23, size = 8, alpha = 0.4) +
       geom_point(aes(fill = factor(cyl)),shape = 21, size = 2) +
       geom_rect(aes(xmin = min(disp)-5, ymax = max(mpg) + 2,fill = "cyan"),
                 xmax = mean(range(mtcars$disp)),ymin = 25, alpha = 0.02) + 
     scale_fill_manual(values = c("red", "orange", "green", "blue", "black", "cyan"), 
labels=c("0","1","4 gears","6 gears","8 gears","High mpg")) #changed the order
</code></pre>

<p>So, I have two questions:</p>

<p><strong>Question 1:</strong> How do I fix the legends--I want three different legends--one for rectangle fill (which I call high mpg rectangle), another one for fill for <code>geom_point()</code> represented by <code>mtcars$vs</code> and the last one for fill for <code>geom_point()</code>represented by <code>mtcars$cyl</code></p>

<p><strong>Question2:</strong> Is my hypothesis about ordering of colors as per the layers correct (i.e. Issue#4 discussed above)? I am doubtful because what if there are a lot of factors--are we required to memorize them, then order them as per the layers drawn and finally remember to apply color palette manually in the order each <code>geom_*()</code> layers are created?</p>

<p>As a beginner, I have spent a lot many hours on this, googling everywhere. So, I'd appreciate your kind guidance.</p>