2012-03-28 6 views
4

教育的な例として、海洋生物学コースのための簡単な系統樹を作りたいと思う。 Iは、樹状図(クラスター分析)を取得し、第一の切断点としてドメインを使用したい種のリストから簡単な系統樹樹(樹木)を作る

Group <- c("Benthos","Benthos","Benthos","Benthos","Benthos","Benthos","Zooplankton","Zooplankton","Zooplankton","Zooplankton", 
"Zooplankton","Zooplankton","Fish","Fish","Fish","Fish","Fish","Fish","Phytoplankton","Phytoplankton","Phytoplankton","Phytoplankton") 
Domain <- rep("Eukaryota", length(Group)) 
Kingdom <- c(rep("Animalia", 18), rep("Chromalveolata", 4)) 
Phylum <- c("Annelida","Annelida","Arthropoda","Arthropoda","Porifera","Sipunculida","Arthropoda","Arthropoda","Arthropoda", 
"Arthropoda","Echinoidermata","Chorfata","Chordata","Chordata","Chordata","Chordata","Chordata","Chordata","Heterokontophyta", 
"Heterokontophyta","Heterokontophyta","Dinoflagellata") 
Class <- c("Polychaeta","Polychaeta","Malacostraca","Malacostraca","Demospongiae","NA","Malacostraca","Malacostraca", 
"Malacostraca","Maxillopoda","Ophiuroidea","Actinopterygii","Chondrichthyes","Chondrichthyes","Chondrichthyes","Actinopterygii", 
"Actinopterygii","Actinopterygii","Bacillariophyceae","Bacillariophyceae","Prymnesiophyceae","NA") 
Order <- c("NA","NA","Amphipoda","Cumacea","NA","NA","Amphipoda","Decapoda","Euphausiacea","Calanioda","NA","Gadiformes", 
"NA","NA","NA","NA","Gadiformes","Gadiformes","NA","NA","NA","NA")      
Species <- c("Nephtys sp.","Nereis sp.","Gammarus sp.","Diastylis sp.","Axinella sp.","Ph. Sipunculida","Themisto abyssorum","Decapod larvae (Zoea)", 
"Thysanoessa sp.","Centropages typicus","Ophiuroidea larvae","Gadus morhua eggs/larvae","Etmopterus spinax","Amblyraja radiata", 
"Chimaera monstrosa","Clupea harengus","Melanogrammus aeglefinus","Gadus morhua","Thalassiosira sp.","Cylindrotheca closterium", 
"Phaeocystis pouchetii","Ph. Dinoflagellata") 
dat <- data.frame(Group, Domain, Kingdom, Phylum, Class, Order, Species) 
dat 

、第としてKindom、第として門などがありません:私は、分類学上のランクの種のリストを持っています値は無視する必要があります(カットポイントなし、代わりに直線)。グループは、ラベルの色分けカテゴリとして使用する必要があります。

このデータフレームから距離行列を作成する方法は少し不明です。 Rのための系統樹のパッケージがたくさんあります、彼らはnewickのデータ/ DNA /その他の高度な情報を望んでいるようです。したがって、これで助けていただければ幸いです。

+0

)=、それが何かを見つけるために一週間かかりR.と作業の流れを示しています。 – Joel

+0

'r-sig-phylo @ r-project.org'でこの質問に役立つかもしれません... –

+0

うーん... plot.hclust()は素敵なプロットを生成します。確かに、このデータセットをhclustオブジェクトに変換する方法が必要ですか? ade4パッケージのplot.phylog(http://pbil.univ-lyon1.fr/ade4/ade4-html/plot.phylog.html)はさらに良いものになりますが、おそらく不可能なこのデータフレームをphylogオブジェクトに変換しています(http: /pbil.univ-lyon1.fr/ade4/ade4-html/phylog.html)? – Mikko

答えて

3

私自身の質問に答えるのは少し難解ですが、簡単な解決策が見つかりました。たぶん誰かを助けるかもしれない。

library(ape) 
taxa <- as.phylo(~Kingdom/Phylum/Class/Order/Species, data = dat) 

col.grp <- merge(data.frame(Species = taxa$tip.label), dat[c("Species", "Group")], by = "Species", sort = F) 

cols <- ifelse(col.grp$Group == "Benthos", "burlywood4", ifelse(col.grp$Group == "Zooplankton", "blueviolet", ifelse(col.grp$Group == "Fish", "dodgerblue", ifelse(col.grp$Group == "Phytoplankton", "darkolivegreen2", "")))) 

plot(taxa, type = "cladogram", tip.col = cols) 

すべての列は要素でなければならないことに注意してください。コード自体は行だけのカップルですが、質問が複雑に聞こえるとスマートな人々の注意を必要とするので、これは投票アップ

enter image description here

+0

しかし、私はどのように線の色を与えるのだろうか。エッジ名は、ラベル名に基づいて色を制御するのが難しいように割り当てられているようです – Mikko

3

あなたが手 でツリーを描画したい場合(これはおそらくそれを行うための最善の方法ではありません)、 は次のように開始することができます(それは完全な答えではありません。 色が欠けている、 とエッジが長すぎます)。 これは、データがすでにソートされていることを前提としています。

# Data: remove Group 
dat <- data.frame(Domain, Kingdom, Phylum, Class, Order, Species) 

# Start a new plot 
par(mar=c(0,0,0,0)) 
plot(NA, xlim=c(0,ncol(dat)+1), ylim=c(0,nrow(dat)+1), 
    type="n", axes=FALSE, xlab="", ylab="", main="") 

# Compute the position of each node and find all the edges to draw 
positions <- NULL 
links <- NULL 
for(k in 1:ncol(dat)) { 
    y <- tapply(1:nrow(dat), dat[,k], mean) 
    y <- y[ names(y) != "NA" ] 
    positions <- rbind(positions, data.frame(
    name = names(y), 
    x = k, 
    y = y 
)) 
} 
links <- apply(dat, 1, function(u) { 
    u <- u[ !is.na(u) & u != "NA" ] 
    cbind(u[-length(u)],u[-1]) 
}) 
links <- do.call(rbind, links) 
rownames(links) <- NULL 
links <- unique(links[ order(links[,1], links[,2]), ]) 

# Draw the edges 
for(i in 1:nrow(links)) { 
    from <- positions[links[i,1],] 
    to <- positions[links[i,2],] 
    lines(c(from$x, from$x, to$x), c(from$y, to$y, to$y)) 
} 

# Add the text 
text(positions$x, positions$y, label=positions$name) 
+0

コードをありがとう!しかし、私はこの解決策に完全に満足していません。たぶん、これを行う簡単な方法があります。 – Mikko

関連する問題