R基礎(chǔ)-05-熱圖
主要通過(guò)學(xué)習(xí)example侠坎,掌握學(xué)習(xí)R包的學(xué)習(xí)方法;用pheatmap畫(huà)圖;
rm(list = ls())
library(pheatmap)
a1=rnorm(100);
dim(a1)=c(5,20);
pheatmap(a1)# Draw heatmaps;
a2 = rnorm(100) + 2 #or a2=a1+2;# Create test matrix;
b=cbind(a1,a2)
dim(a2)=c(5,20)
pheatmap(a2)
b = cbind(a1,a2)
pheatmap(cbind(a1,a2),cluster_cols = F)
b = as.data.frame(b)
paste('a1',1:20)#鏈接
paste('a1',1:20, sep = '20')
c(paste('a1',1:20, sep = '20'),paste('a2',1:20, sep = '20'))#a1和a2鏈接
names(b)= c(paste('a1',1:20, sep = '20'),paste('a2',1:20, sep = '20'))
pheatmap(b,cluster_cols = F)
?pheatmap
#b=as.data.frame(b),
#names(b)=c(paste(a1,1:20,sep=''), paste(a2, 1:20, sep=''))
#pheatmap(b, cluster_rows=F, cluster_cols=F);
#as.data.frame(group=c(rep("a1",20),rep("a2",20)))
#rownames(b)<-colnames(a);
#scale(log2(expr+1))
# Show text within cells;
# Fix cell sizes and save to file with correct size;
# Generate annotations for rows and columns;# Display row and color annotations;
?pheatmap
找到Examples,運(yùn)行代碼葬毫,參考數(shù)據(jù)框的畫(huà)圖方式央勒,繪制數(shù)據(jù)框的熱圖
繪制數(shù)據(jù)框的熱圖
tmp =data.frame(group=c(rep('a1',20),rep('a2',20)))#構(gòu)建數(shù)據(jù)框
rownames(tmp)=colnames(b)#把列名賦給行名
pheatmap(b,annotation_col = tmp)
Examples
# Create test matrix
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
# Draw heatmaps
pheatmap(test)
pheatmap(test, kmeans_k = 2)
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
pheatmap(test, cluster_row = FALSE)
pheatmap(test, legend = FALSE)
# Show text within cells
pheatmap(test, display_numbers = TRUE)
pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1"))
# Fix cell sizes and save to file with correct size
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
# Generate annotations for rows and columns
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)),
Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
# Display row and color annotations
pheatmap(test, annotation_col = annotation_col)
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
# Change angle of text in the columns
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
pheatmap(test, annotation_col = annotation_col, angle_col = "0")
# Specify colors
ann_colors = list(
Time = c("white", "firebrick"),
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,
annotation_colors = ann_colors)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2])
# Gaps in heatmaps
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),
cutree_col = 2)
# Show custom strings as row/col names
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
# Specifying clustering from distance matrix
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
# Modify ordering of the clusters using clustering callback option
callback = function(hc, mat){
sv = svd(t(mat))$v[,1]
dend = reorder(as.dendrogram(hc), wts = sv)
as.hclust(dend)
}
pheatmap(test, clustering_callback = callback)
## Not run:
# Same using dendsort package
library(dendsort)
callback = function(hc, ...){dendsort(hc)}
pheatmap(test, clustering_callback = callback)
## End(Not run)