您可以使用scale_colour_continuous
对于这两项任务。
library(ggplot2)
qplot(mpg, wt, data = mtcars, colour = cyl, xlab = "MPG", ylab = "WT") +
scale_colour_continuous(name = "Cylinders", low = "white", high = "black")
在这里,name
参数是色标的标签。参数low
and high
表示连续色标的下限和上限。
如果要指定具有三种颜色的连续色阶,可以使用scale_colour_gradient2
:
qplot(mpg, wt, data = mtcars, colour = cyl, xlab = "MPG", ylab = "WT") +
scale_colour_gradient2(name = "Cylinders", midpoint = median(mtcars$cyl),
low = "red", mid = "green", high = "black")