How can I make a plot in R with ggplot2 that is darker where there are more points and more transparent where there are less points? I tried making a geom_hex plot with a gradient but it is ignoring alpha values.
答案 0 :(得分:2)
set.seed(101)
dd <- data.frame(x=rnorm(3000),y=rnorm(3000))
library(ggplot2); theme_set(theme_bw())
Set alpha on points, natural overlap:
ggplot(dd,aes(x,y))+geom_point(alpha=0.1,size=8)
(made points larger to get overlap)
Or:
ggplot(dd,aes(x,y))+stat_density_2d(geom="raster",
aes(alpha = ..density..), contour = FALSE)+
scale_x_continuous(expand=c(0,0))+
scale_y_continuous(expand=c(0,0))
Still working on geom_hex
... I can't actually figure out how to do this ... aes(alpha=..count..)
seems as though it should work based on R ggplot geom_hex alpha transparency , but ??
## fails with ggplot 2.1.0 ... ?
ggplot(dd,aes(x,y))+
geom_hex(aes(alpha=log(..count..)))