2017-11-29 16 views
-1

이 플롯에 범례를 추가하려면 어떻게해야합니까? 다음과 같이ggplot의 분산 밀도 플롯에 범례를 추가하려면 어떻게합니까

scatter plot showing density

내 코드는 다음과 같습니다

d1 = densCols(data4$x, data4$y, 
       colramp = colorRampPalette(c("navy blue","yellow","firebrick","firebrick"), 
       space = "Lab")) 

ggplot(data4) + 
    geom_point(aes(x=x, y= y, col = d1), size = 0.1) + 
    scale_color_identity() + 
    theme_bw() + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
    labs(x="xaxis", y="yaxis", title="xyz cells") + 
    theme(plot.title = element_text(hjust = 0.5)) 

theme(legend.position=c(1,1),legend.justification=c(1,1)) 

내가 이것과 언급 한 여러 가지 다른 일을 시도했지만 작동하지 않는 이유를 알고하지 않습니다.

미리 도움을 주셔서 감사합니다.

scale_color_identity (가이드 = "범례")를 추가 한 후 Ok. 내 그래프는 다음과 같습니다.

! scale_color_identity(guide = "legend")

+0

을 포맷팅이 작동하면 [{}] 버튼을 대신 사용하십시오. – awiebe

+0

예를 들어 주시면 이해가 가지 않습니다. – Goku

답변

0

다른 저울과 달리 의 기본값 인 guide"none"입니다. 이는 범례 없음을 의미합니다. 따라서 guide = "legend"을 수동으로 설정해야합니다.

ggplot(data4) + 
    geom_point(aes(x=x, y= y, col = d1), size = 0.1) + 
    scale_color_identity(guide = "legend") + 
... 

문서의 예제

은 이것을 잘 보여

http://ggplot2.tidyverse.org/reference/scale_identity.html이 (그건 그렇고, 당신이 당신의 예를 더 간단하고 재현 할 수 있도록 노력하는 것이 좋습니다 How to make a great R reproducible example? aka MCVE (Minimal, Complete, and Verifiable Example) 참조하십시오.) 어떻게하지 코드의 그

+0

내 전설이 이상하게 보입니다. 색상 그라디언트와 빈 수는 예상대로 없습니다. 코멘트 섹션에 플롯을 어떻게 추가합니까? – Goku