Kernel density scatter plot in R -
i saw beautiful plot , i'd recreate it. here's example showing i've got far:
# kernel density scatterplot library(rcolorbrewer) library(mass) greyscale <- rev(brewer.pal(4, "greys")) x <- rnorm(20000, mean=5, sd=4.5); x <- x[x>0] y <- x + rnorm(length(x), mean=.2, sd=.4) z <- kde2d(x, y, n=100) plot(x, y, pch=".", col="hotpink") contour(z, drawlabels=false, nlevels=4, col=greyscale, add=t) abline(c(0,1), lty=1, lwd=2) abline(lm(y~x), lty=2, lwd=2)
i'm struggling fill contours colour. job smoothscatter
or package? suspect might down use of kde2d
and, if so, can please explain function or link me tutorial?
many thanks!
p.s. final image should greyscale
seems want filled contour rather jus contour. perhaps
library(rcolorbrewer) library(mass) greyscale <-brewer.pal(5, "greys") x <- rnorm(20000, mean=5, sd=4.5); x <- x[x>0] y <- x + rnorm(length(x), mean=.2, sd=.4) z <- kde2d(x, y, n=100) filled.contour(z, nlevels=4, col=greyscale, plot.axes = { axis(1); axis(2) #points(x, y, pch=".", col="hotpink") abline(c(0,1), lty=1, lwd=2) abline(lm(y~x), lty=2, lwd=2) })
which gives
Comments
Post a Comment