jor*_*ran 107
这是使用基础R图形的解决方案:
#Some sample data
x <- runif(100)
dat <- data.frame(x = x,y = x^2 + 1)
#Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('red','blue'))
#This adds a column of color values
# based on the y values
dat$Col <- rbPal(10)[as.numeric(cut(dat$y,breaks = 10))]
plot(dat$x,dat$y,pch = 20,col = dat$Col)
Run Code Online (Sandbox Code Playgroud)
ROL*_*OLO 20
使用ggplot2的解决方案:
library(ggplot2)
#Some sample data
x <- sort(runif(100))
dat <- data.frame(x = x,y = x^2 + 1)
# Some external vector for the color scale
col <- sort(rnorm(100))
qplot(x, y, data=dat, colour=col) + scale_colour_gradient(low="red", high="blue")
Run Code Online (Sandbox Code Playgroud)
pur*_*mac 17
要在基础R中为joran的答案添加图例:
legend("topleft",title="Decile",legend=c(1:10),col =rbPal(10),pch=20)
Run Code Online (Sandbox Code Playgroud)
这个例子只是为了美观而添加",cex = 0.8":