2016-09-26 8 views
0

私のプロットに値> 5、値< -5とその値に基づいて異なる色(色範囲)を与えたいと思います。付属の画像と同様に、画像には、5/5未満の2色とその間の値があります。上下限値のプロットを散布する色範囲を与える方法

私のコードは次のとおりです。ここで

ggplot(data, aes(S1, S2, color=abs(S1-S2)>5)) +geom_point()+ smoother 

Scatter plot image

+0

'color = cut((S1-S2)、breaks = c(-Inf、-5,5 + Inf))' – JasonAizkalns

+0

jasonさん、ありがとうございます。 ** g> 5:80 ** – user3616494

答えて

0

ggplot前に色のグループに対応する新しい変数を作成するソリューションです:

set.seed(1) 
df = data.frame(x = rnorm(1000,1,10)) 
df$y = df$x + rnorm(1000,1,5) 

df$col = NA 
df$col[(df$x - df$y) > 5] = "g1" 
df$col[(df$x - df$y) < 5 & (df$x - df$y) > -5] = "g2" 
df$col[(df$x - df$y) < -5] = "g3" 

ggplot(df, aes(x, y, color = col)) + geom_point() 

enter image description here

EDIT
あなたは観測数で伝説にラベルを付けて、色を選択します:

library(plyr) 
df_labels = ddply(df, "col", summarise, n = n()) 

ggplot(df, aes(x, y, color = col)) + geom_point() + 
scale_color_manual(labels = df_labels$n, values = c("g1" = "red", "g2" = "blue", "g3" = "green")) 

enter image description here

+0

答えをいただきありがとうございます。凡例にカウント数を与えるにはどうすればよいですか?このように、例えば。 ** g1:87 **。 – user3616494

+0

私は自分の答えを編集してラベルを修正しました。 – bVa

0

あなたの色の計算は、3つの値の結果を取得していることを確認してください:

ggplot(data, aes(S1, S2, color=factor(ifelse(S1-S2>5,1,ifelse(S2-S1>5,2,4)) , labels = c("less","more","same")))) +geom_point() 

かプロットする前にスクリプトの別のステップでこれを行います。

関連する問題