You might consider using slightly transparent colors
#helper function to make transparent ramps
alpharamp<-function(c1,c2, alpha=128) {stopifnot(alpha>=0 & alpha<=256);function(n) paste(colorRampPalette(c(c1,c2))(n), format(as.hexmode(alpha), upper.case=T), sep="")}
And then we can overplot the two graphs with
smoothScatter(x1,y1,nrpoints=length(x1),cex=3, colramp=alpharamp("white",blues9))
par(new=T)
smoothScatter(x2,y2,nrpoints=length(x2),cex=3,colramp= alpharamp("white","red"), axes=F, ann=F)
Here's that this code produces.
If, you still want to get to the actual color values in the plot, that's actually a bit tricky. You'd have to call grDevices:::.smoothScatterCalcDensity
directly with your data. Then you'd have to transform the returned fhat
values by taking 4th root and rescaling to 0-1. Then you convert to color by taking those values and then those values (let's call them z
are converted to indexes using the formula floor((256 - 1e-05) * z + 1e-07)+1
. Then those indexes are used to find a value from the 256 colors generated from the ramp you supply. It's all a bit crazy but you can read the source to smoothScatter
and image.default
to see how it really happens.