2012-03-26 4 views
9

또 다른 ggplot 범례 질문!ggplot에서 레이어 범례 제거

나는 형태

test <- data.frame(
    cond = factor(rep(c("A", "B"), each=200)), 
    value = c(rnorm(200), rnorm(200, mean=0.8)) 
) 

그래서 두 그룹 및 일부 값의 데이터 집합을하고 난 밀도를 플롯합니다.

test.cdf <- ddply(test, .(cond), summarise, value.mean=mean(value)) 

그런 ggplot 통화 : 나는 또한 I 있도록 플롯에 각 그룹의 평균을 나타내는 선을 추가 할

ggplot(test, aes(value, fill=cond)) + 
    geom_density(alpha=0.5) + 
    labs(x='Energy', y='Density', fill='Group') + 
    opts(
    panel.background=theme_blank(), 
    panel.grid.major=theme_blank(), 
    panel.grid.minor=theme_blank(), 
    panel.border=theme_blank(), 
    axis.line=theme_segment() 
) + 
    geom_vline(data=test.cdf, aes(xintercept=value.mean, colour=cond), 
    linetype='dashed', size=1) 

위의 코드를 실행하면, 당신이 나타내는 전설을 얻을 각 그룹뿐만 아니라 평균 지표 vline에 대한 것입니다. 제 질문은 geom_vline()에 대한 전설을 없애려면 어떻게해야합니까?

+1

'cond'를 채우기 및 색상에 매핑했습니다. 이러한 매핑 중 하나를 제거하면 문제가 해결됩니다. – Andrie

+0

내 대답은 당신에게 어떤 용도로 사용됩니까? –

답변

15

사용중인 ggplot2의 버전에 따라이 문제가 발생합니다.

enter image description here

vline에 대한 범례를 포함하지 않습니다 R2.14.1에 0.9.0 대 ggplot2를 사용하여 나는이 그래프를 얻을.

ggplot(test, aes(value, fill=cond)) + 
    geom_density(alpha=0.5) + 
    labs(x='Energy', y='Density', fill='Group') + 
    opts(
    panel.background=theme_blank(), 
    panel.grid.major=theme_blank(), 
    panel.grid.minor=theme_blank(), 
    panel.border=theme_blank(), 
    axis.line=theme_segment() 
) + 
    geom_vline(data=test.cdf, aes(xintercept=value.mean, colour=cond), 
    linetype='dashed', size=1, show_guide = TRUE) 

enter image description here

문제를 재현 : ggplot2이 버전에서는 show_guide를 사용하여 전설의 선두로부터 조정할 수 있습니다. 기본값은 show_guide = FALSE입니다. 이전 버전에서는 legend = FALSEgeom_vline에 추가하여 범례를 생략 할 수 있습니다. legend = FALSE을 추가하면 여전히 현재 버전에서 작동 여전히 작동하지만 경고를 던졌습니다 :

Warning message: 
In get(x, envir = this, inherits = inh)(this, ...) : 
    "legend" argument in geom_XXX and stat_XXX is deprecated. Use show_guide = TRUE or show_guide = FALSE for display or suppress the guide display. 

내가 ggplot2 업그레이드를 추천 할 것입니다.

+0

Thanks @Paul ... ggplot을 업그레이드하면 원하는대로하는'show_guide' 플래그가 활성화되었습니다. 건배. – Hassantm

+1

ggplot 2.0.0부터 :'show_guide'는 더 이상 사용되지 않습니다. 대신'show.legend'를 사용하십시오. –

관련 문제