###读取数据常见错误:
在读取数据过程中可能遇到以下问题,参照上一篇博客:
可能遇到报错:
1、Error in if (is.na(n) || n > 65536L) stop(“size cannot be NA nor exceed 65536”) :
missing value where TRUE/FALSE needed
没有处理数据转化距离。
2、Error in hclust(dist(test)) : NA/NaN/Inf in foreign function call (arg 11)
In addition: Warning message:
In dist(test) : NAs introduced by coercion
数据读入方式错误,详见各参数,与两种数据类型。
###Plot开始画图:
> test<-read.table("C:/Users/admin/Desktop/test.txt")
> hc = hclust(dist(test))
plot(hc,hang=1,cex=0.5,labels = NULL)
###另类聚类图
将hclude生成的对象转换为另类的聚类图
> hcd = as.dendrogram(hc)
> plot(hcd)
# tweeking some parameters
op=par(bg="#DDE3CA")
plot(hc,col="#487AA1",col.main="#45ADA8",col.lab="#7C8071",
col.axis="#F38630",lwd=3,lty=3,sub="",hang=-1,axes=FALSE)
# add axis
axis(side=2,at=seq(0,400,100),col="#F38630",labels=FALSE,
lwd=2)
# add text in margin
mtext(seq(0,400,100),side=2,at=seq(0,400,100),line=1,
col="#A38630",las=2)
plot(hcd, type = "triangle")
放大在树状图
另一个非常有用的功能是选择树的一部分。例如,如果我们要研究的树状图上的分区,我们可以把它在一个高度75
op = par(mfrow = c(2, 1))
> plot(cut(hcd, h = 75)$upper, main = "Upper tree of cut at h=75")
> plot(cut(hcd, h = 75)$lower[[2]], main = "Second branch of lower tree with cut at h=75")
为了获得更多的定制的图形,我们需要更多的代码。一个非常有用的功能dendrapply可以应用一个函数的一dendrgoram所有节点。如果我们要添加一些色彩的标签这是非常方便的。
labelColors = c("#CDB380", “#036564”, “#EB6841”, “#EDC951”)
clusMember = cutree(hc, 4)
colLab <- function(n) {
-
if (is.leaf(n)) {
-
a <- attributes(n)
-
labCol <- labelColors[clusMember[which(names(clusMember) == a$label)]]
-
attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
-
}
-
n
- }
clusDendro = dendrapply(hcd, colLab)
plot(clusDendro, main = “Cool Dendrogram”)
由R包ape提供更具吸引力的树非常好的工具,利用as.phylo功能将hclust objects转换成phylo对象
plot(as.phylo(hc), cex = 0.5, label.offset = 5)
###4种不同类型的聚类树形图
plot.phylo函数的4种不同类型的聚类树形图
plot(as.phylo(hc), type ="cladogram", cex = 0.9, label.offset = 1)
plot(as.phylo(hc), cex=1,type = "unrooted")
圆形树形图
# fan
plot(as.phylo(hc), cex=1,label.offset = 2,type = "fan")
plot(as.phylo(hc), type = "radial")
自定义的系统进化树
ape包对树的性状有着很多控制,能够定制他们以不同的方式。例如:
plot(as.phylo(hc), type = “fan”, tip.color = hsv(runif(15, 0.65, 0.95), 1, 1, 0.7), edge.color = hsv(runif(10, 0.65, 0.75), 1, 1, 0.7), edge.width = runif(20,0.5, 3), use.edge.length = TRUE, col = “gray80”)
修改一些参数
mypal=c("#556270", "#4ECDC4", "#1B676B", "#FF6B6B", "#C44D58")
clus5=cutree(hc, 5)
op=par(bg="#E8DDCB")
plot(as.phylo(hc), type="fan", tip.color=mypal[clus5], label.offset=1, cex=log(mtcars$mpg, 10), col="red")
###彩色叶子节点
The Rpackagesparclprovides theColorDendrogramfunction that allows to add some color. For example, we can add color to theleaves
R包还提供ColorDendrogram功能来让我们给聚类树点颜色看看。比如我们可以给叶子节点来点颜色
# install.packages('sparcl')
library(sparcl)
# colors the leaves of a dendrogram
y=cutree(hc, 3)
ColorDendrogram(hc, y=y, labels=names(y), main="My Simulated Data",
branchlength=80)
R包ggplot2没有功能绘制树状图的原因我不知道。然而,包ggdendro提供一个像样的解决方案。
library(ggdendro)
library(plyr)
# basic option
ggdendro(hc)
#another option
ggdendrogram(hc, rotate = TRUE, size = 4, theme_dendro = FALSE, color ="tomato")
# Triangular lines
ddata <- dendro_data(as.dendrogram(hc), type = "triangle")
ggplot(segment(ddata)) + geom_segment(aes(x = x, y = y, xend = xend,
yend = yend)) + ylim(-10, 150) + geom_text(data = label(ddata), aes(x = x,
y = y, label = label), angle = 90, lineheight = 0)
Colored dendrogram
Lastbut not least, there's one more resource available from Romain Francois'saddicted to Rgraph gallery which I find really interesting. The code inR for generating colored dendrograms, which you can download and modify ifwanted so, is availablehere
最后,你可以到罗曼弗朗索瓦的图形库里面进一步学习~~~
你甚至可以修改他的代码
地址是:
http://gallery.r-enthusiasts.com/RGraphGallery.PHP?graph=79(貌似要翻墙)
http://addictedtor.free.fr/packages/A2R/lastVersion/R/code.R
# load code of A2R function
source("http://addictedtor.free.fr/packages/A2R/lastVersion/R/code.R")
# colored dendrogram
op=par(bg="#EFEFEF")
A2Rplot(hc, k=3, boxes=FALSE, col.up="gray50", col.down=c("#FF6B6B",
"#4ECDC4", "#556270"))
par(op)
#another colored dendrogram
op = par(bg = "gray15")
cols = hsv(c(0.2, 0.57, 0.95), 1, 1, 0.8)
A2Rplot(hc, k = 3, boxes = FALSE, col.up = "gray50", col.down = cols)
二维码查看: