I am trying to make a scatter plot in ggplot2 with a third variable size display and 2d density contours. The legend seems to be confused by the inclusion of 2d density loops.
For example, the following code works:
library('ggplot2')
set.seed(1)
x=rnorm(100); y=rnorm(100,sd=10); z=seq(1,10,length.out=100)
dd=data.frame(x=x,y=y,z=z)
ggplot(dd,aes(x,y,size=z))+geom_point()

But now notice that the legend behaves unusually when I add a challenge stat_density2d(). In particular, the plot legend shows blue blocks instead of black circles:
ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d()

source
share