programing

ggplot2 scale_brewer의 역순

shortcode 2021. 1. 17. 11:16
반응형

ggplot2 scale_brewer의 역순


겉보기에는 매우 간단한 일이지만 답을 찾지 못한 채 30 분 이상 걸렸습니다.

색상 순서를 바꾸려면 어떻게합니까? 보고 scale_brewer에 대한 문서 , 내가 그것을 할 수있다 생각 formatter=인수 의심되는. 나는 통과 'rev'하고 rev있지만 효과 (오류 메시지, 단지 무시)가 없습니다.


brewer.pal직접 사용하여 색상을 선택 하고 다음을 사용 하고 싶을 것입니다 scale_colour_manual.

ggplot(mtcars,aes(x = mpg, y = disp)) + 
    geom_point(aes(colour = factor(cyl))) + 
    scale_colour_manual(values = rev(brewer.pal(3,"BuPu")))

그런 다음 rev거기에서 색상 순서를 지정할 수 있습니다 .

ggplot 2.0,0 버전부터는 더 직접적인 방법이 있습니다. 아래 @pbaylis의 답변을 참조하십시오.


ggplot2의 CRAN의 버전은 사용자가 지정할 수 있습니다 direction=-1scale_brewer 색상을 반전 할 수 있습니다. 다음은 허용 된 답변과 동일한 플롯을 생성합니다.

ggplot(mtcars,aes(x = mpg, y = disp)) + 
  geom_point(aes(colour = factor(cyl))) + 
  scale_colour_brewer(palette="BuPu", direction=-1)

이것은 OP의 문제에 도움이되지 않습니다-알아요. 와 같은 이산 스케일의 scale_..._brewer()경우하는 scale_..._manual(values = rev(colorsYouHad))것이 정답입니다.

그럼에도 불구하고 연속 척도의 경우 다음을 간단히 전달할 수 있습니다.

scale_..._...(..., trans = "reverse")

예를 들어, 다음의 연속 등가물에 대해 scale_..._brewer():

scale_..._distiller("My Scale", palette = "Spectral", trans = "reverse")


RColorBrewer(아름다운 패키지)를 직접 다루고 싶지 않다면 원래 data.frame에서 요소의 수준을 반전 한 다음 플로팅 할 수 있습니다.

dsamp <- diamonds[sample(nrow(diamonds), 1000), ] 

# Reverse the levels of the factor associated with color, here 'clarity'
# (Might be safer to assign this to a new column named, e.g., 'clarity2')
levels(dsamp$clarity) <- rev(levels(dsamp$clarity))

d <- qplot(carat, price, data = dsamp, colour = clarity)
d + scale_colour_brewer(breaks = levels(dsamp$clarity))

반전 전과 동일한 순서로 키를 인쇄하려면 다음과 같이하십시오.

d + scale_colour_brewer(breaks = rev(levels(dsamp$clarity)))

파티에 아주 늦었어요. 그러나 나는이 문제를 잠시 만났고 위의 해결책을 사용했습니다. 저는 현재 Hadley Wickham의 r4ds를 진행 중이며 엄청나게 쉬운 솔루션이 있으므로 게시 할 것이라고 생각했습니다. 이것을 변경하십시오 :

ggplot(mtcars,aes(x = mpg, y = disp)) + 
geom_point(aes(colour = factor(cyl)))

이에:

ggplot(mtcars,aes(x = mpg, y = disp)) + 
geom_point(aes(colour = factor(-cyl))) #note the minus symbol

참조 URL : https://stackoverflow.com/questions/8750871/ggplot2-reverse-order-of-scale-brewer

반응형