2017-03-31 2 views
1

My scatterplot 저는 R이 최고는 아니지만이 플롯에 색상을 도입하려고합니다.색상 값 Y 값에 따른 산란도

SecondPlot <- ggplot(sur11, aes(x=standec, y=compnoanti)) + 
geom_point(col ="black", size = 0.5) + 
geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5) 
SecondPlot 
SecondPlot + geom_abline(intercept = 50, slope = 0, size = 0.2) 

어떻게 수 I :

1) 색상 75 점 빨간색을 넘어 모든 Y 값?

2) 기존의 R 브루어 스케일 (예 : "블루스")을 사용하여 값에 따라 Y 값의 색상을 지정하는 스케일 된 색상을 도입 하시겠습니까?

나는 1부터 10까지의 각 y 값을 할당하고 인수 size = factor (Z)를 사용했지만이 방법은 효과가 없었습니다.

감사합니다.

답변

1

이 시도 :

1) 색상 레드 75 점을 넘어 모든 Y 값

SecondPlot <- ggplot(sur11, aes(x=standec, y=compnoanti)) + 
    geom_point(col = ifelse(sur11$compnoanti > 75, "red", "black"), size = 0.5) + 
    geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5) 
SecondPlot 

2) 조정 색상을 소개 나는 색상으로, 기존 R의 맥주 규모, 예를 들어, "블루스"를 사용하도록 Y 값은 값에 따라 다릅니 까?

#Interpolate Brewer palette 
library(RColorBrewer) 
colourCount = length(unique(sur11$compnoanti)) 
getPalette = colorRampPalette(brewer.pal(9, "Blues")) 


SecondPlot <- ggplot(mtcars, aes(x=standec, y=compnoanti)) + 
    geom_point(aes(col = compnoanti), size = 0.5) + 
    geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5) + 
    scale_colour_gradientn(colours=getPalette(colourCount)) 
SecondPlot 

재현 가능한 예제를 제공하지 않아 일반 데이터 세트를 사용하여 테스트했습니다. 이것은 일반적인 버전입니다 :

# Color points above a certain value red 
SecondPlot <- ggplot(mtcars, aes(x=mpg, y=disp)) + 
    geom_point(col = ifelse(mtcars$disp > 120, "red", "black"), size = 0.5) + 
    geom_text(label=rownames(mtcars), hjust = 0, nudge_x = 0.3, size = 2.5) 
SecondPlot 


#Interpolate Brewer palette 
library(RColorBrewer) 
colourCount = length(unique(mtcars$disp)) 
getPalette = colorRampPalette(brewer.pal(9, "Blues")) 


SecondPlot <- ggplot(mtcars, aes(x=mpg, y=disp)) + 
    geom_point(aes(col = disp), size = 0.5) + 
    geom_text(label=rownames(mtcars), hjust = 0, nudge_x = 0.3, size = 2.5) + 
    scale_colour_gradientn(colours=getPalette(colourCount)) 
SecondPlot 
+0

대단히 감사합니다. 한 가지 질문 - 레이블뿐만 아니라 점의 색을 원하면 어떻게 할 수 있습니까? 감사합니다 @HAVB –

+0

@HenryCann은'gees_text' 라인에'aes (color = whateverVariable)'를 추가합니다. 예를 보려면 여기를 확인하십시오. http://docs.ggplot2.org/current/geom_text.html – HAVB

+0

감사합니다. 레이블에 색상을 추가 할 수 있었지만 스케일이 아닌 색상을 추가 할 수 있었습니까? geom_text에 scale_colour_gradient 인수를 인수로 추가 할 수 있습니까? 건배 @HAVB –

관련 문제