Created
April 22, 2013 11:42
-
-
Save oscarperpinan/5434116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
data(movies, package='ggplot2') | |
library(lattice) | |
movies <- data.table(movies) | |
nBins <- 500 | |
## 2D Binning | |
movies[, c('ratBin', 'votesBin'):=list(floor(rating/nBins), floor(votes/nBins))] | |
setkey(movies, ratBin, votesBin) | |
## Cross Join (as expand.grid) ... | |
bins <- CJ(0:nBins, 0:nBins) | |
## ... used to extract records from movies corresponding to each 2D bin | |
movies2 <- movies[bins, list(.N, rating, votes), nomatch=0] | |
## Finally aggregate with mean | |
movies3 <- movies2[,lapply(.SD, mean), | |
by=list(ratBin, votesBin)] | |
xyplot(votes ~ rating, data=movies3, cex=asinh(movies3$N)/5, | |
alpha=0.6, pch=21, fill='blue', col='black') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment