code

범례 gg 그림 2.2 제거

starcafe 2023. 6. 27. 22:26
반응형

범례 gg 그림 2.2 제거

한 레이어의 범례(평활함)를 유지하고 다른 레이어의 범례(점)를 제거하려고 합니다.로 전설들을 차단하려고 노력했습니다.guides(colour = FALSE)그리고.geom_point(aes(color = vs), show.legend = FALSE).

편집: 이 질문과 답변이 인기가 있기 때문에 재현 가능한 예는 다음과 같습니다.

library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs)) +
geom_point(aes(shape = factor(cyl))) +
geom_line(aes(linetype = factor(gear))) +
geom_smooth(aes(fill = factor(gear), color = gear)) + 
theme_bw() 

enter image description here

여기서 bp는 gg 플롯입니다.

특정 미관(채움)에 대한 범례 제거:

bp + guides(fill="none")

척도를 지정할 때도 수행할 수 있습니다.

bp + scale_fill_discrete(guide="none")

모든 범례가 제거됩니다.

bp + theme(legend.position="none")

이에 대한 다른 해결책이 있을 수 있습니다.
코드:

geom_point(aes(..., show.legend = FALSE))

다음을 지정할 수 있습니다.show.legend에 있는 매개 변수aes호출:

geom_point(aes(...), show.legend = FALSE)

그러면 해당하는 범례가 사라져야 합니다.

질문과 사용자 3490026의 답변이 검색어 1위를 차지함에 따라 OP의 질문을 명시적으로 해결하는 솔루션과 함께 지금까지 제안된 내용을 재현 가능한 사례와 간단한 예시로 작성했습니다.

그 중 하나가ggplot2수행하며 혼동될 수 있는 것은 특정 범례가 동일한 변수와 연결될 때 자동으로 혼합된다는 것입니다.예를 들어.factor(gear)두 번 표시, 한 번 표시linetype그리고 한 번은fill결합된 범례를 생성합니다.대조적으로,gear와 동일하게 처리되지 않으므로 고유한 범례 항목이 있습니다.factor(gear)지금까지 제공된 솔루션은 일반적으로 잘 작동합니다.그러나 때때로 가이드를 무시해야 할 수도 있습니다.맨 아래에 있는 제 마지막 예를 보십시오.

# reproducible example:
library(ggplot2)
p <- ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs)) +
geom_point(aes(shape = factor(cyl))) +
geom_line(aes(linetype = factor(gear))) +
geom_smooth(aes(fill = factor(gear), color = gear)) + 
theme_bw() 

enter image description here

모든 범례 제거: @user3490026

p + theme(legend.position = "none")

모든 범례 제거: @duhaime

p + guides(fill = FALSE, color = FALSE, linetype = FALSE, shape = FALSE)

전설 끄기: @Tjebo

ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs), show.legend = FALSE) +
geom_point(aes(shape = factor(cyl)), show.legend = FALSE) +
geom_line(aes(linetype = factor(gear)), show.legend = FALSE) +
geom_smooth(aes(fill = factor(gear), color = gear), show.legend = FALSE) + 
theme_bw() 

선종류가 표시되도록 채우기 제거

p + guides(fill = FALSE)

scale_fill_ 함수를 통해 위와 동일:

p + scale_fill_discrete(guide = FALSE)

그리고 이제 OP의 요청에 대한 한 가지 가능한 대답이 있습니다.

"한 레이어(점)의 범례를 유지하고 다른 레이어(점)의 범례를 제거하는 것"

일부는 애드혹 포스트혹을 해제합니다.

p + guides(fill = guide_legend(override.aes = list(color = NA)), 
           color = FALSE, 
           shape = FALSE)  

enter image description here

차트가 둘 다 사용하는 경우fill그리고.color다음을 사용하여 범례를 제거할 수 있습니다.

+ guides(fill=FALSE, color=FALSE)

언급URL : https://stackoverflow.com/questions/35618260/remove-legend-ggplot-2-2

반응형