在 qplot 中设置颜色标签和改变调色板

Setting the color label and varying the color palette in qplot

  1. 使用下面的代码,我可以设置 x 轴和 y 轴的标签,但不能设置 cyl 颜色的标签。 documentation 没有办法解决。

    qplot(mpg, wt, data=mtcars, colour=cyl,xlab="MPG",ylab="WT")
    

  1. 如何改变此处的调色板 qplot?所以,我希望在下面的 中做一些事情:

    x <- runif(100)
    y<-runif(100)
    time<-runif(100)  
    pal <- colorRampPalette(c('white','black'))
    cols <- pal(10)[as.numeric(cut(time,breaks = 10))]
    plot(x,y,pch=19,col = cols)
    

您可以使用 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参数是色标的标签。参数lowhigh表示连续色标的下限和上限。


如果要指定三种颜色的连续色标,可以使用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")