ggplot2画简单的heatmap
gg_heatmap
gg_heatmap
PeRl
ggplot2 heatmap
偶然的机会,发现ggplot2画的heatmap也挺好看的,除了不能画出聚类树来(手动滑稽)。 随意新建了两个矩阵,并且计算对应的相关系数。
x <- matrix(sample(1:1000, 100), ncol = 10)
y <- matrix(sample(1:1000, 100), ncol = 10)
cor_score <- cor(t(x), t(y))
plot_df <- data.frame(
x = rep(1:10, 10),
y =rep(1:10, rep(10,10)),
value = as.vector(cor_score)
)
利用geom_tile()
画矩形,就大致完成了heatmap图。
ggplot(data = plot_df, aes(x,y)) +
geom_tile(aes(fill = value)) +
scale_fill_continuous(low = "#00FF00", high = "#FF0000") +
scale_x_continuous(breaks = 1:10, labels = paste("sample", 1:10, sep = "_"))+
scale_y_continuous(breaks = 1:10, labels = paste("gene", 1:10, sep = "_"))+
theme_bw() +
theme(
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.title = element_blank(),
axis.text.x = element_text(angle = 90)
)
更进一步,发现可以利用极坐标画出圆形的heatmap,挺有意思的,主要还是计算文字旋转的角度。
text <- paste("gene", 1:10, sep = "_")
one_cir_ang = 360/nrow(x)
right_text_ang <- -(seq(one_cir_ang/2, (180 - one_cir_ang/2), by = one_cir_ang))
right_text_ang[which(right_text_ang < -90)] <- 180 + right_text_ang[which(right_text_ang < -90)]
left_text_ang <- right_text_ang
text_ang = c(right_text_ang, left_text_ang)
然后就利用scale_y_discrete
和coord_polar(theta = "x")
转换为极坐标
ggplot(data = plot_df, aes(x,y)) +
geom_tile(aes(fill = value)) +
geom_text(
aes(x = x, y = y,label = text, angle = text_ang),
data = data.frame(x = 1:10, y = 12, text, text_ang),
) +
scale_fill_continuous(low = "#00FF00", high = "#FF0000") +
scale_y_discrete(breaks = 1:10, labels = paste("gene", 1:10, sep = "_"))+
coord_polar(theta = "x") +
theme_bw() +
theme(
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()
)