programing tip

미학과 geom_text를 사용할 때 범례에서 'a'제거

itbloger 2020. 8. 10. 07:48
반응형

미학과 geom_text를 사용할 때 범례에서 'a'제거


이 코드로 생성 된 범례에서 문자 'a'를 제거하려면 어떻게해야합니까? 를 제거하면 geom_text'a'문자가 범례에 표시되지 않습니다. geom_text그래도 유지하고 싶습니다 .

ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape = Species, colour = Species)) + 
   geom_point() + 
   geom_text(aes(label = Species))

설정 show.legend = FALSE에서 geom_text:

ggplot(data = iris,
       aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species, label = Species)) + 
    geom_point() +
    geom_text(show.legend = FALSE)

인수의 show_guide이름이 show.legendin으로 변경 되었습니다 ggplot2 2.0.0( 릴리스 뉴스 참조 ).


사전 ggplot2 2.0.0:

show_guide = FALSE같은 ...

ggplot( data=iris, aes(x=Sepal.Length, y=Sepal.Width , colour = Species , shape = Species, label = Species ) , size=20 ) + 
geom_point()+
geom_text( show_guide  = F )

여기에 이미지 설명 입력


비슷한 문제 가있었습니다 . Simon의 솔루션은 나를 위해 일했지만 약간의 비틀기가 필요했습니다. 나는 geom_text의 인수 "show_guide = F" 추가 해야한다는 것을 깨닫지 못했습니다 . 기존 인수로 대체하는 것이 아니라 Simon의 솔루션이 보여주는 것입니다. 나 같은 ggplot2 멍청한 사람에게는 이것은 그렇게 분명하지 않았습니다. 적절한 예는 OP의 코드를 사용하고 다음과 같이 누락 된 인수를 추가했을 것입니다.

..
geom_text(aes(label=Species), show_guide = F) +
..

Nick이 말한 것처럼

다음 코드는 여전히 오류를 생성합니다.

geom_text(aes(x=1,y=2,label="",show_guide=F))

여기에 이미지 설명 입력

이므로:

geom_text(aes(x=1,y=2,label=""),show_guide=F)

aes 인수 외부에서는 범례에 대한 a를 제거합니다.

여기에 이미지 설명 입력


guide_legend(override.aes = aes(...))범례에서 'a'를 숨기는 데 사용할 수 있습니다 .

아래는 guide_legend () 사용 방법에 대한 간단한 예입니다.

library(ggrepel)
#> Loading required package: ggplot2

d <- mtcars[c(1:8),]

p <- ggplot(d, aes(wt, mpg)) +
  geom_point() +
  theme_classic(base_size = 18) +
  geom_label_repel(
    aes(label = rownames(d), fill = factor(cyl)),
    size = 5, color = "white"
  )

# Let's see what the default legend looks like.
p

# Now let's override some of the aesthetics:
p + guides(
  fill = guide_legend(
    title = "Legend Title",
    override.aes = aes(label = "")
  )
)

2019-04-29에 reprex 패키지 (v0.2.1)에 의해 생성됨

참고 URL : https://stackoverflow.com/questions/18337653/remove-a-from-legend-when-using-aesthetics-and-geom-text

반응형