本文是要把昨天畫的聚類熱圖給“掰彎”丑勤,
gene <- read.csv(file = "clipboard",header = T,sep = "\t",check.names = F)? #讀入數(shù)據(jù)
row.names(gene) <- letters? ? #添加上基因名,如果直接有基因名俺榆,則省去這一步
gg <- hclust(dist(gene))? ?#行聚類
zz <- hclust(dist(t(gene)))? #列聚類
gene <- gene[,zz$order]? ?#聚類后排序
gene <- gene[gg$order,]
gene <- cbind(name = row.names(gene),gene)??
data <- melt(gene,id.vars = "name")
data$num <- rep(c(1:26),6)
data$x <- rep(c(1:6),each = 26)
tex <- data.frame(x = c(13:18),y = rep(0,6),variable = colnames(gene)[2:7])? ?#建立一個數(shù)據(jù)框,用于給環(huán)形熱圖添加標(biāo)簽
ggplot(data)+
? geom_tree(data = gg,continuous = c("size"),size = 1)+? ? #生成聚類樹
? geom_tile(data = data,aes(x = x+12,y = num,alpha = 0.7,fill = value),color = "gray")+? #在熱圖的左邊空出12列,方便畫出空心的圓
? scale_fill_gradient2(low = "green",high = "darkred",mid = "white",midpoint = 6,name = "FPKM",
? ? ? ? ? ? ? ? ? ? ? guide = guide_colourbar(barheight = unit(10,"cm"),title.theme = element_text(size = rel(15))))+
? scale_y_discrete(position = c("right"),limits = factor(c(1:28)),label = c(gene$name," "," "))+? ?# 在熱圖的底部空出兩行(26+2),設(shè)置環(huán)形的開口
? scale_x_discrete(label = NULL)+
? scale_size_continuous(range = c(4,9),guide = NULL)+
? scale_alpha(guide = NULL)+
? labs(x = NULL,y = NULL)+
? coord_polar(theta = "y",start = 0)+? ? #環(huán)形熱圖的本質(zhì)是采用極坐標(biāo)系
? geom_text(data = tex,aes(x,y,label = variable))+
? theme(panel.background = element_blank(),
? ? ? ? axis.line = element_blank(),
? ? ? ? axis.title.y = element_blank(),
? ? ? ? axis.title.x = element_text(size = rel(2),hjust = 0.5),
? ? ? ? axis.text.x = element_text(size = rel(2),hjust = 0.5),
? ? ? ? axis.text.y = element_text(hjust = 0.5,size = rel(2)),
? ? ? ? axis.ticks = element_blank())