Skip to content

Instantly share code, notes, and snippets.

@kalebr
Last active January 29, 2020 17:43
Show Gist options
  • Save kalebr/6832380 to your computer and use it in GitHub Desktop.
Save kalebr/6832380 to your computer and use it in GitHub Desktop.
Smoothed heatmap without chunkiness of stat_bin2d
library(ggplot2)
df <- data.frame(x=rnorm(1000), y=rnorm(1000))
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
ggplot(df[sample(1:nrow(df), 1000), ], aes(x, y)) +
stat_density2d(geom="tile", aes(fill=..density.., alpha=sqrt(sqrt(..density..))), contour=FALSE, n=100) +
scale_alpha(range = c(0.5, 1.0)) + scale_fill_gradientn(colours = jet.colors(10), trans="sqrt")
@stephanmg
Copy link

Great solution. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment