本文是要把昨天画的聚类热图给“掰弯”,
gene <- read.csv(file = "clipboard",header = T,sep = "\t",check.names = F) #读入数据
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]) #建立一个数据框,用于给环形热图添加标签
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),设置环形的开口
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)+ #环形热图的本质是采用极坐标系
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())