2014-07-15 9 views
0

많은 점이있는 산점도의 한 가지 일반적인 기술은 점의 크기를 줄이고 투명하게 만드는 것입니다. ggplot2 범례의 점 모양을 어떻게 변경합니까?

A scatterplot of the diamonds dataset with points reduced in size and opacity. The points in the legend are hard to see.

library(ggplot2) 
ggplot(diamonds, aes(x, y, colour = cut)) + 
    geom_point(alpha = 0.25, size = 0.5) + 
    ylim(0, 12) 

불행하게도, 전설의 포인트는 이제 제대로보고 너무 작고 희미하다.

주 플롯 패널의 플롯과 독립적으로 범례의 포인트를 변경하고 싶습니다. 그것은에 포함 된 설정 중 하나가되어야합니다 :

thm <- theme_get() 
thm[grepl("legend", names(thm))] 

나는 적절한 설정을 찾기 위해 고민 중입니다. 포인트 크기는 어떻게 변경합니까?

답변

1

당신은 당신의 효과를 달성하기 위해 패키지 scales의 기능 guide_legend()을 사용할 수 있습니다.

이 기능을 사용하면 플롯에서 안내선 (범례)의 값을 무시할 수 있습니다. 귀하의 경우 alphasize 값을 모두 colour 스케일로 무시하고 싶습니다.

이 시도 :

library(ggplot2) 
library(scales) 
ggplot(diamonds, aes(x, y, colour = cut)) + 
    geom_point(alpha = 0.25, size = 1) + 
    ylim(0, 12) + 
    guides(colour=guide_legend(override.aes=list(alpha=1, size=3))) 

enter image description here

1

범례에서만 형식을 변경해야하는 경우 override.aes=size=guide_legend (아래 참조)에 사용해야합니다. 이것은 플롯에 사용 된 크기보다 우선하며 범례에 대한 새로운 크기 값을 사용합니다.

가 될 점은 보이지 다음 guides()에 라인을 제거하는 linetype=0를 설정하고 size=3 큰 포인트를 얻을 수 있도록 geom_point(size=0)를 추가 플롯 해결에 전설의 점과 선을 얻으려면.

ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+ geom_point(size=0)+ guides(colour = guide_legend(override.aes = list(size=3,linetype=0))) enter image description here

관련 문제