问题
I am an absolute beginner in ggplot2
and trying to draw beautiful figures using this package. I am going through the manual and I didn't understand the difference between scale_colour_gradient()
vs. scale_fill_gradient()
In my analysis, I will use "hypothesis" for observations because I am unsure about my conclusions.
Here's the original code that worked for me:
Experiment 1
erupt <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster()
erupt
Hypothesis: It plots the density of eruptions using the default color (i.e. blue)
I wanted to go further to experiment with ggplot2
's scale_colour_gradient()
and scale_fill_gradient()
Experiment 2
erupt <- ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill=density)) +
scale_colour_gradient(low = "white", high = "black")
erupt
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.
Experiment 3
erupt <- ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill=density)) +
scale_fill_gradient(low = "white", high = "black")
erupt
Here, the only thing I have changed is using scale_fill_gradient
instead of scale_color_gradient
. However, the graph is completely different. It ranges from Black to White.
Can someone please help me understand what's going on? Specifically, I have two questions:
a) When we set fill = xyz, does it set color or the fill?
b) What is the difference between scale_fill_gradient
and scale_color_gradient
?
I apologize if my question sounds too basic for you. I am not an expert and have a long way to go.
Update: After discussion, here's what the conclusion is:
In Experiment 1: the plot simplys fills using the continuous variable "density".
In Experiment 2, 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.
In Experiment 3, I am filling and overriding so I see white and black plot.
To fix this, I did two things:
Experiment 2: fix #1 :
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")
OR
fix #2:
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster(aes(colour=density)) +
scale_colour_gradient(low = "white", high = "black")
Also, to add to axeman's point, Here's what I did:
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")
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.
回答1:
In ggplot2
, color
and fill
are mapped separately. color
refers to point and line color, whereas fill
refers to bar fill (i.e. inner color), density fill, etc. If you map fill=xyz
within aes()
and then use scale_color_...
, it won't do anything since you have to control fill
with scale_fill_...
. If you map color
in this case, it will alter the color of the line surrounding the density; then you could call scale_color_...
to alter it.
来源:https://stackoverflow.com/questions/39128590/scale-colour-gradient-vs-scale-fill-gradient-in-ggplot2