2013-07-23 50 views
0

是否可以使用兩個條件繪製數據幀?如何結合兩個條件來做一個情節?

我有這樣的數據幀:

tdat=structure(list(X = structure(c(1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L), class = "factor", .Label = c("AS", 
"Dup", "MCH")), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("bot", 
"top", "all"), class = "factor"), value = c(1.009936818, 1.414634463, 
0.778023226, 1.046037598, 2.370167409, 0.714638976, 0.241778577, 
0.684398769, 0.181664019, 0.44099306, 1.212003504, 0.237309508, 
1.257632594, 2.329136359, 1.037219886, 1.495702786, 2.990687546, 
1.069762508)), .Names = c("X", "variable", "value"), row.names = c(NA, 
-18L), class = "data.frame") 

> tdat 
    X variable  value 
1 AS  bot 1.0099368 
2 MCH  bot 1.4146345 
3 Dup  bot 0.7780232 
4 AS  bot 1.0460376 
5 MCH  bot 2.3701674 
6 Dup  bot 0.7146390 
7 AS  top 0.2417786 
8 MCH  top 0.6843988 
9 Dup  top 0.1816640 
10 AS  top 0.4409931 
11 MCH  top 1.2120035 
12 Dup  top 0.2373095 
13 AS  all 1.2576326 
14 MCH  all 2.3291364 
15 Dup  all 1.0372199 
16 AS  all 1.4957028 
17 MCH  all 2.9906875 
18 Dup  all 1.0697625 

我可以用

qplot(x=variable, y=value,data=tdat). 

但是我需要同時使用 「X」 和 「變量」 子組。所以我需要9組:AS-bot,MCH-bot,Dup-bot,AS-top等等。那麼有沒有辦法告訴qplot使用y作爲y = value + X?

+2

這很難說,正是你想要暗算的..也許這會指出正確的方向什麼??不知道雖然:'ggplot(tdat,aes(x = interaction(X,variable),y = value))+ geom_line()'? – Arun

+0

@阿倫明亮!也許你可以添加'sep =' - ''並將其作爲答案! – agstudy

+0

@agstudy,是的,我會的。但也許在OP解釋這是否是他正在尋找的.. :)我不想寫一個答案,並通過編輯..有一張紙,明天讀:P .. – Arun

回答

2

正如評論中已經提到的那樣,您可以使用interaction。在這裏,我用了兩次2 aes。爲了獲得更好的傳奇小標題,我使用了scale_color_discrete

ggplot(tdat, aes(x=interaction(X,variable,drop=TRUE,sep='-'), y=value, 
       color=X)) + 
       geom_point() + 
       scale_color_discrete(name='interaction levels') 

enter image description here

+0

好的,謝謝你的回答,但爲了着色,我希望所有的AS具有相同的顏色,Dup也是MCH。由於我們使用的是交互()函數,我不確定這是可能的。 – Wicelo

+0

@Wicelo我編輯我的答案。 – agstudy