Gradient of N Colors Ranging from Color 1 and Color 2

Gradient of n colors ranging from color 1 and color 2

colorRampPalette could be your friend here:

colfunc <- colorRampPalette(c("black", "white"))
colfunc(10)
# [1] "#000000" "#1C1C1C" "#383838" "#555555" "#717171" "#8D8D8D" "#AAAAAA"
# [8] "#C6C6C6" "#E2E2E2" "#FFFFFF"

And just to show it works:

plot(rep(1,10),col=colfunc(10),pch=19,cex=3)

Sample Image

One colour gradient according to value in column scatterplot in R

dat <- read.delim(text='"value"; "avg.sal"; "avg.temp"
2,5698; 34,27254; 4,44
5,4361; 34,30686; 4,64
2,27; 34,3538; 8,05
5,6015; 34,50136; 5,01
2,27; 34,37596; 7,4"', sep=";", dec=",")

bluefunc <- colorRampPalette(c("lightblue", "darkblue"))
plot( dat$avg.sal, dat$avg.temp,
col=bluefunc(5)[findInterval(dat$value, seq(2:6))] )

To respond to the followup question. When cex is specified inside a vector it does control size of the "points":

dat$size <- 1:5
bluefunc <- colorRampPalette(c("lightblue", "darkblue"))
plot( dat$avg.sal, dat$avg.temp,
cex=dat$size,
col=bluefunc(5)[findInterval(dat$value, seq(2:6))] )

How to add gradient color to a surface3d in R when Z axis is between 0 and 1

I found the answer. I needed to order the cut values and then remap the values of the range with the colors. Not elegant, but working...

 data.col = as.data.frame(matrix(as.factor(cut(s$z,ordered_result = T,
include.lowest = TRUE,
right = TRUE,
breaks = nb.div)),
dim(s$z)[1],
dim(s$z)[2],byrow = FALSE))

range = levels(cut(s$z,ordered_result = T,
include.lowest = TRUE,
right = TRUE,
breaks = nb.div))
library(plyr)
for(i in 1:ncol(data.col)){
data.col[,i] <- mapvalues(data.col[,i],
from=range,
to=rbPal(nb.div),
warn_missing = FALSE)

}

Sample Image



Related Topics



Leave a reply



Submit