Unusual legend using 2d sizing

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()

plot looks normal

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()

plot legend shows blue blocks instead of black circles

+5
source share
1 answer

size= - , stat_density2d(), ​​ ggplot(), ( geom_point() stat_density2d()). , size=0.5 ( - ) stat_density2d(), .

ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d(size=0.5)

enter image description here

+6

All Articles