2013-04-17 2 views
5

gagplot2에서 제 3의 변수와 density2d 등고선에 대한 크기 매핑을 사용하여 scatterplot을 만들려고합니다. 전설이 density2d 등고선의 포함에 혼란스러워하는 것 같습니다.크기 맵핑과 density2d를 사용하는 특이한 범례

예를 들어, 다음 코드는 작동 :

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

하지만 지금은, 내가 stat_density2d()에 대한 호출에 추가 할 때 전설이 비정상적으로 동작 확인합니다. 특히, 플롯 범례 대신 검은 색 동그라미의 파란색 블록을 보여줍니다 size=으로

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

plot legend shows blue blocks instead of black circles

답변

6

당신이 stat_density2d()과는 ggplot() 호출 설정되어있는 경우에 설정할 수있는 미학 중 하나입니다, 전설은 라인과 포인트 모두를 위해 만들어진다 (포인트는 geom_point()stat_density2d() 전에 호출 됨). 범례에서 파란색 선을 제거하려면 stat_density2d() 안에 수동으로 size=0.5 (또는 다른 값)을 설정하면 범례가 정확합니다. `ggplot (일, AES :이 도움이되지만 순서를 교환하는 것은 모두 (기본적으로 채워진 원이 사각형 뒤에 숨어있는) 전설을 표시하는 경우

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

enter image description here

+2

(+1) 잘 모르겠어요 (x, y, size = z)) + stat_density2d() + geom_point()' – Arun

+1

@ Arun 네, 맞습니다 - 두 범례가 그려져 있습니다. 솔루션은 전설에서 선 너비를 없애는 방법을 보여줍니다 (이 경우 의미가 없으므로) –

+0

감사합니다! – js86