2013-12-17 34 views
0

我對R是全新的。
我有預處理和組合的表達譜數據。看起來像這樣(「exp.txt」)R)創建雙標記的MDS圖

 STUDY_1_CANCER_1 STUDY_1_CON_1 STUDY_2_CANCER_1 STUDY_2_CANCER_2 
P53 1.111    1.22    1.3    1.4 
..... 

此外,我創建了表型數據。看起來精簡版這個(「pheno.txt」)

Sample    Disease  Study 
STUDY_1_CANCER_1  Cancer   GSE1 
STUDY_1_CON_1  Normal   GSE1 
STUDY_2_CANCER_1  Cancer   GSE2 
STUDY_2_CON_1  Normal   GSE2 

在這裏,我試圖用古典cmdscale命令這樣做MDS情節。

data=read.table("exp.txt", row.names=1, header=T) 
DATA=as.matrix(data) 
pc=cor(DATA, method="p") 
mds=cmdscale(as.dist(1-pc),2) 
plot(mds) 

enter image description here

我想創建這樣的數字與顏色的雙標(研究和疾病)的情節。我應該怎麼做?

回答

1

首先創建一個空圖,然後添加指定顏色/形狀的點。

下面是一個例子:

require(vegan) 
data(dune) 
data(dune.env) 

mds <- cmdscale(vegdist(dune, method='bray')) 


# set colors and shapes 
cols = c('red', 'blue', 'black', 'steelblue') 
shps = c(15, 16, 17) 
# empty plot 
plot(mds, type = 'n') 
# add points 
points(mds, col = cols[dune.env$Management], pch = shps[dune.env$Use]) 
# add legend 
legend('topright', col=cols, legend=levels(dune.env$Management), pch = 16, cex = 0.7) 
legend('bottomright', legend=levels(dune.env$Use), pch = shps, cex = 0.7) 

注意的因素是內部編碼爲整數,這有助於在這裏。

> levels(dune.env$Management) 
[1] "BF" "HF" "NM" "SF" 

所以

cols[dune.env$Management] 

將採取cols第一個因子水平的第一項。 Similariy爲不同的形狀。

最後添加圖例。當然這個情節仍然需要一些拋光,但多數民衆贊成在路上...

順便說一句:加文辛普森有一個不錯的blogpost關於定製協調情節。

enter image description here

1

其實,你可以在默認情況下,直接做到這一點plot命令,它可以採取pchcol參數作爲載體。用途:

with(data, plot(mds, col = as.numeric(Study), pch = as.numeric(Disease), asp = 1) 

必須使用asp = 1當你繪製cmdscale結果:兩個軸必須進行類似縮放。您還可以爲較好的軸標籤添加xlabylab參數。要添加圖例並選擇繪製字符和顏色,請參閱其他回覆。