| scale_gradient2 {ggplot2} | R Documentation |
Smooth gradient between three colours (high, low and midpoints)
scale_colour_gradient2(name=NULL, low=muted("red"), mid="white", high=muted("blue"), midpoint=0, space="rgb", breaks=NULL, labels=NULL, limits=NULL, trans="identity", alpha=1, ...)
scale_fill_gradient2(name=NULL, low=muted("red"), mid="white", high=muted("blue"), midpoint=0, space="rgb", breaks=NULL, labels=NULL, limits=NULL, trans="identity", alpha=1, ...)
name |
name of scale to appear in legend or on axis |
low |
colour at low end of scale |
mid |
colour at mid point of scale |
high |
colour at high end of scale |
midpoint |
position of mid point of scale, defaults to 0 |
space |
colour space to interpolate through, rgb or Lab, see ?colorRamp for details |
breaks |
numeric vector indicating where breaks should lie |
labels |
character vector giving labels associated with breaks |
limits |
numeric vector of length 2, giving the extent of the scale |
trans |
a transformer to use |
alpha |
alpha value to use for colours |
... |
other arguments |
This page describes scale_gradient2, see layer and qplot for how to create a complete plot from individual components.
A layer
Hadley Wickham, http://had.co.nz/
scale_gradient: continuous colour scale
## Not run:
dsub <- subset(diamonds, x > 5 & x < 6 & y > 5 & y < 6)
dsub$diff <- with(dsub, sqrt(abs(x-y))* sign(x-y))
(d <- qplot(x, y, data=dsub, colour=diff))
d + scale_colour_gradient2()
# Change scale name
d + scale_colour_gradient2(expression(sqrt(abs(x - y))))
d + scale_colour_gradient2("Difference\nbetween\nwidth and\nheight")
# Change limits and colours
d + scale_colour_gradient2(limits=c(-0.2, 0.2))
# Using "muted" colours makes for pleasant graphics
# (and they have better perceptual properties too)
d + scale_colour_gradient2(low="red", high="blue")
d + scale_colour_gradient2(low=muted("red"), high=muted("blue"))
# Using the Lab colour space also improves perceptual properties
# at the price of slightly slower operation
d + scale_colour_gradient2(space="Lab")
# About 5
# idea to avoid that combination
d + scale_colour_gradient2(high=muted("green"))
# We can also make the middle stand out
d + scale_colour_gradient2(mid=muted("green"), high="white", low="white")
# or use a non zero mid point
(d <- qplot(carat, price, data=diamonds, colour=price/carat))
d + scale_colour_gradient2(midpoint=mean(diamonds$price / diamonds$carat))
# Fill gradients work much the same way
p <- qplot(letters[1:5], 1:5, fill= c(-3, 3, 5, 2, -2), geom="bar")
p + scale_fill_gradient2("fill")
# Note how positive and negative values of the same magnitude
# have similar intensity
## End(Not run)