2016-04-13 11 views
0

こんにちは、現在私、私は特定のカットオフggplots2:別のベクトルに基づいて点の色を変更します。

library(ggplot2) 
library(ggrepel) 

x = c(0.8846, 1.1554, 0.9317, 0.9703, 0.9053, 0.9454, 1.0146, 0.9012, 
     0.9055, 1.3307) 
y = c(0.9828, 1.0329, 0.131, 1.3794, 0.9273, 0.3605, 1.0259, 0.9542, 
     0.9717, 0.9357) 
z= c("a", "b", "c", "d", "e", "f", 
    "g", "h", "i", "j") 


df = data.frame(x = x, y = y, z = z) 
z <- as.vector(df$z) 
ggplot(data = df, aes(x = x, y = y)) + theme_bw() + 


    geom_text_repel(data = subset(df, y > 1.3), aes(label = z), 
        box.padding = unit(0.45, "lines")) + 

    geom_point(aes(colour = cut(y, c(-Inf,0.2,0.5,Inf))), size = 3) + 
    # set color scales 
    scale_color_manual(
    name = "p-values", 
    values = c("(-Inf,0.2]" = "blue", 
       "(0.2,0.5]" = "red", 
       "(0.5, Inf]" = "grey")) 

に基づくポイントの色を変更することができプロットがグラフは次のようになり、 enter image description here

は、しかし、私はベースの色を変えることができる方法はあり別のベクトルのセットに置き換えます。例えば はX2で唯一のポイントは赤であり、X3は青とグレーの残りの部分になるように、私は、

x2<-c("a","b") 
x3<-c("f") 

が、私はそれを設定することができていると言いますか?前もって感謝します!

+0

、 "blue"、 "gray"))))+ scale_color_identity()+ geom_point(size = 3) '? – lukeA

答えて

1

あなたは正しいアイデアがあります。条件を指定する余分な列を作成するだけです。 %のX3で `ggplot(データ=のDF、AES(X = xで、yは、yは=、色= ifelse(Z%%でX2、 "赤"、ifelse(Z%のような

df$condition <- "others" 
df[df$z %in% c("a","b"), "condition"] <- "x2" 
df[df$z %in% c("f"), "condition"] <- "x3" 

ggplot(data = df, aes(x = x, y = y, colour= condition)) + theme_bw() + 
    geom_point(size = 3) + 
    # set color scales 
    scale_color_manual(
    name = "p-values", 
    values = c("x2" = "red", 
       "x3" = "blue", 
       "others" = "grey")) 

enter image description here

関連する問題