scale_colour_gradient vs. scale_fill_gradient in ggplot2
<p>I am an absolute beginner in <code>ggplot2</code> and trying to draw beautiful figures using this package. I am going through the manual and I didn't understand the difference between <code>scale_colour_gradient()</code> vs. <code>scale_fill_gradient()</code> In my analysis, I will use "hypothesis" for observations because I am unsure about my conclusions.</p>

<p>Here's the original code that worked for me:</p>

<p><strong>Experiment 1</strong></p>

<pre><code>  erupt &lt;- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
    geom_raster() 
  erupt
</code></pre>

<p><strong>Hypothesis:</strong> It plots the density of eruptions using the default color (i.e. blue)</p>

<p>I wanted to go further to experiment with <code>ggplot2</code>'s <code>scale_colour_gradient()</code> and <code>scale_fill_gradient()</code></p>

<p><strong>Experiment 2</strong></p>

<pre><code> erupt &lt;- ggplot(faithfuld, aes(waiting, eruptions)) +
    geom_raster(aes(fill=density)) +
    scale_colour_gradient(low = "white", high = "black") 
  erupt
</code></pre>

<p>Now, I got the same graph as Experiment 1. Does it mean that if I use fill= xyz in ggplot's aesthetics, I am setting the color gradient and NOT fill gradient? To understand why I am asking this, let's see Experiment 3.</p>

<p><strong>Experiment 3</strong></p>

<pre><code>  erupt &lt;- ggplot(faithfuld, aes(waiting, eruptions)) +
    geom_raster(aes(fill=density)) +
    scale_fill_gradient(low = "white", high = "black") 
  erupt
</code></pre>

<p>Here, the only thing I have changed is using <code>scale_fill_gradient</code> instead of <code>scale_color_gradient</code>. However, the graph is completely different. It ranges from Black to White. </p>

<p>Can someone please help me understand what's going on? Specifically, I have two questions:</p>

<p>a) When we set fill = xyz, does it set color or the fill?</p>

<p>b) What is the difference between <code>scale_fill_gradient</code> and <code>scale_color_gradient</code>?</p>

<p>I apologize if my question sounds too basic for you. I am not an expert and have a long way to go.</p>

<hr>

<p>Update: After discussion, here's what the conclusion is:</p>

<p>In <strong>Experiment 1</strong>: the plot simplys fills using the continuous variable "density". </p>

<p>In <strong>Experiment 2</strong>, I am filling "density" but then overriding "color". So, ggplot tries to shade the border of the raster graph (because I am overriding color) and has nothing to do with fill because I am not overriding anything. </p>

<p>In <strong>Experiment 3</strong>, I am filling and overriding so I see white and black plot.</p>

<p>To fix this, I did two things:</p>

<p><strong>Experiment 2</strong>: 
fix #1 : </p>

<pre><code>    ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + 
geom_raster(aes(colour=density)) + 
scale_colour_gradient(low = "white", high = "red") + 
scale_fill_gradient(low = "white", high = "green") 
</code></pre>

<p><strong>OR</strong> </p>

<p>fix #2: </p>

<pre><code>    ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + 
geom_raster(aes(colour=density)) + 
scale_colour_gradient(low = "white", high = "black")
</code></pre>

<p>Also, to add to axeman's point, Here's what I did:</p>

<pre><code>  ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + 
    geom_tile(aes(fill=density, col=density)) +
    scale_colour_gradient(low = "white", high = "red") +
    scale_fill_gradient(low = "white", high = "green") 
</code></pre>

<p>In the output, we will see that the borders of the tiles get shaded as per density (i.e. red color) and the fill colors get shaded as per green color. So, this way, we are able to demonstrate effects of fill and color.</p>