R - density plot with R basic plot(), colorRampPalette(), densCols() functions

03 Jun 2018

Function to create density plot with R basic plot:

(use the following functions: plot, colorRampPalette, densCols)

densplot <- function(x,y, pch=19, cex=1){
	df <- data.frame(x,y)
	d <- densCols(x,y, colramp=colorRampPalette(c("black", "white")))
	df$dens <- col2rgb(d)[1,] + 1L
	cols <- colorRampPalette(c("#000099", "#00FEFF", "#45FE4F","#FCFF00", "#FF9400", "#FF3100"))(256)
	df$col <- cols[df$dens]
	df <- df[order(df$dens),]
	plot(df$x,df$y, pch=pch, col=df$col)
}

[ R  plot  ]