z-scale for smoothScatter() function
smoothScatter() plots irregular 2D data with level of colors. However it does not show the z-scale. Here is a way to plot a z-scale:
library(graphics)
library(fields)
n <- 10000
x <- matrix(rnorm(n), ncol = 2)
y <- matrix(rnorm(n, mean = 3, sd = 1.5), ncol = 2)
# dans x, les coordonnées x
# dans y, les coordonnées y
par(mar=c(4, 4, 2, 6)+0.4)
smoothScatter(x, y)
n <- matrix(0, ncol=128, nrow=128)
xrange <- range(x)
yrange <- range(y)
for (i in 1:length(x)) {
posx <- 1+floor(127*(x[i]-xrange[1])/(xrange[2]-xrange[1]))
posy <- 1+floor(127*(y[i]-yrange[1])/(yrange[2]-yrange[1]))
n[posx, posy] <- n[posx, posy]+1
}
image.plot( legend.only=TRUE, zlim= c(0, max(n)), nlevel=128, col=colorRampPalette(c("white", blues9))(128))
Commentaires
Enregistrer un commentaire