Graphics Reference
In-Depth Information
Discussion
Mapping continuous values to a color scale requires a continuously changing palette of colors.
Table 12-2
lists the continuous color and fill scales.
Table 12-2. Continuous fill and color scales
Fill scale
Color scale
Description
scale_colour_gradient()
Two-color gradient
scale_fill_gradient()
scale_fill_gradient2() scale_colour_gradient2()
Gradient with a middle color and two colors that diverge
from it
scale_fill_gradientn() scale_colour_gradientn()
Gradient with
n
colors, equally spaced
Notice that we used the
muted()
function in the examples. This is a function from the scales
package that returns an RGB value that is a less-saturated version of the color chosen.
See Also
If you want use a discrete (categorical) scale instead of a continuous one, you can recode your
data into categorical values. See
Recoding a Continuous Variable to a Categorical Variable
.
Coloring a Shaded Region Based on Value
Problem
You want to set the color of a shaded region based on the yvalue.
Solution
Add a column that categorizes the yvalues, then map that column to
fill
. In this example, we'll
first categorize the values as positive or negative:
library(gcookbook)
# For the data set
cb
<-
subset(climate, Source
==
"Berkeley"
)
cb$valence[cb$Anomaly10y
>=
0
]
<-
"pos"
cb$valence[cb$Anomaly10y
<
0
]
<-
"neg"
cb