Tuesday, March 27, 2012

how to draw density plot in R


When there are many data points and significant overlap, scatterplots become less useful. There are several approaches that be used when this occurs. The hexbin(x, y) function in the hexbin package provides bivariate binning into hexagonal cells (it looks better than it sounds).
# High Density Scatterplot with Binning
library(hexbin)
x <- rnorm(1000)
y <- rnorm(1000)
bin<-hexbin(x, y, xbins=50) 
plot(bin, main="Hexagonal Binning")
Reference: http://www.statmethods.net/graphs/scatterplot.html

Another option is to use transparent color:

# define color with transparency
col="#0000ff22"  
plot(x,y, col=col, pch=16, cex=2)

The 3rd option is to use smoothScatter(). 

No comments:

Post a Comment