2017-06-02 9 views
1

2つの列レベル(ここではtreatment,)の組み合わせに基づいてプロットする方法は?プロット:2つの列レベルの組み合わせに基づく色

set.seed(0) 
x <- rep(1:10, 4) 
y <- sample(c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm(20)/5)) 
treatment <- sample(gl(8, 5, 40, labels=letters[1:8])) 
replicate <- sample(gl(8, 5, 40)) 
d <- data.frame(x=x, y=y, treatment=treatment, replicate=replicate) 

プロット:2つの列レベルの組み合わせがa-1, a-2, a-3, ... h-6, h-7, h-8あろう単一の列レベル

ggplot(d, aes(x=x, y=y, colour=treatment)) + geom_point() 

output

ggplot(d, aes(x=x, y=y, colour=replicate)) + geom_point() 

output

に基づいて色。

+2

何について2つの変数を連結するだけですか? –

+2

W/8x8では、64通りの組み合わせが可能ですか?それはあなたが欲しいものですか? – gung

+0

@gungはい、正確には – Prradep

答えて

3

64色は解釈できません。どのように代わりのポイントについてラベル:

ggplot(d, aes(x=x, y=y, colour=treatment)) + 
    geom_text(aes(label=paste0(treatment, replicate)), size=3, show.legend=FALSE) + 
    theme_classic() 

enter image description here

それとも、あなたは多分ファセッティング、異なる治療のためのパターンの相違を発見しようとしている場合には役立つだろう:

ggplot(d, aes(x=x, y=y, colour=treatment)) + 
    geom_text(aes(label=paste0(treatment, replicate)), size=3, show.legend=FALSE) + 
    facet_wrap(~ treatment, ncol=4) + 
    scale_x_continuous(expand=c(0,0.7)) + 
    theme_bw() + theme(panel.grid=element_blank()) 

enter image description here

しかし、あなたが本当に色の束をしたい場合...

ggplot(d, aes(x=x, y=y, colour=interaction(treatment,replicate,sep="-",lex.order=TRUE))) + 
    geom_point() + 
    labs(colour="Treatment-Replicate") + 
    theme_classic() 

(あなたはすべての潜在的なtreatment-replicate組み合わせが凡例に表示されたい場合は、関係なく、彼らはデータ内に存在しているかどうかの、そしてプロットコードに+ scale_colour_discrete(drop=FALSE)を追加します。)

enter image description here

+0

あなたの教育的な例のおかげで@ eipi10。私は現在のシナリオで最後の例を探していて、他の2種類のプロットを使用するのは非常に面白いでしょう。 – Prradep

関連する問題