1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
| setwd("E://Datasetlocalize/People/HSY/CRC/20210729_CRC_Glycosylation_project/Glycosylation/Volcano/") data=read.table(file="GlyBvsAvolcano.txt",header=T,sep="\t") library(ggpubr) pdf(file = "GlyBAvolcano.pdf",width = 20,height = 15) ggscatter(data, x = "fold.change", y ="t.test.p.value", ylab ="-log10(Pvalue)", xlab = "log2(FoldChange)", size=3, color = "Difference", label = "ProteinName",font.label = c(24,"bold","navy"), repel = TRUE, palette = c("#FC4E07","#00AFBB","#999999"), label.select = c("LAMP1","ITB3","CD36","KNG1","ITA6","CD34","APMAP","A2MG","ICAM1","CD68") + xlim(-11.5,11.5)+ geom_hline(yintercept=-log10(0.05),linetype=4)+ geom_vline(xintercept=c(-1,1),linetype=4)+ theme(legend.title = element_blank())+ font("xlab",size = 30)+font("ylab",size = 30)+ font("subtitle",size=30)+font("caption",size = 20)+ font("xy.text",size = 18)+ font("legend.text",size = 20) dev.off()
```
#### 2.2 heatmap 内容比较多,后续会增加注释 ```R setwd("F:/Taogroup/ZH/20201205_pca_exosome_auto_12_PEAKS_10/1213third/") library(pheatmap) data=read.table(file="proZ.txt",header=T,sep="\t") rownames(data)=data[,9] pheatmap(data[,1:8],show_rownames =T,cluster_cols = F,fontsize_row=7)
bk <- c(seq(-2,-0.1,by=0.1),seq(0,2,by=0.1))
pheatmap(data[,1:12],show_rownames =F,cluster_cols = F,cluster_rows=F,fontsize_row=3,colorRampPalette(colors = c("blue","white","red"))(100))
pheatmap(data[,2:3],cluster_cols = FALSE,show_rownames = FALSE,border_color=NA, fontsize_row=6,main = "ProteinDiff",fontsize=10,color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/2),colorRampPalette(colors = c("white","red"))(length(bk)/2)), legend_breaks=seq(0,10,0.5),breaks=bk, clustering_method="complete", treeheight_row=50 )
setwd("F://Taogroup/People/HSY/MM/Gly/MMGlyPeaks/MMGlyPeaks530_PEAKS_14/3group/") library(pheatmap) data=read.table(file="3groupforheatmap.txt",header=T,sep="\t") rownames(data)=data[,13] pheatmap(data[,1:12],show_rownames =F,cluster_cols = F,cluster_rows = F,fontsize_row=7) pheatmap(data[,1:12],show_rownames =F,cluster_cols = F,fontsize_row=7, clustering_distance_rows = "euclidean", clustering_method = "complete", cutree_rows = 5)
pheatmap(data[,1:12], treeheight_col = 0, treeheight_row = 20, border_color = "grey", cellwidth = 10, cellheight = 0.1, fontsize = 4, show_rownames = FALSE, cluster_col = FALSE, height = 1, clustering_distance_rows = "euclidean", clustering_method = "complete", cutree_rows = 5)
scale_rows <- function (x) { m = apply(x, 1, mean, na.rm = T) s = apply(x, 1, sd, na.rm = T) return((x - m)/s) } mat = switch('row', none = data[,2:10], row = scale_rows(data[,2:10]), column = t(scale_rows(t(data[2:10]))))
d = dist(mat, method = 'euclidean') tree = hclust(d, method = 'complete')
v = cutree(tree, 5)[tree$order] gaps = which((v[-1] - v[-length(v)]) != 0)
gene.cluster <- as.data.frame(v) gene.cluster$gene <- strsplit(rownames(gene.cluster),'[.]')[,1] write.table(gene.cluster, "gene.cluster.txt", row.names = FALSE, quote = FALSE, sep = "\t")
setwd("F:/Taogroup/People/HSY/CRC/20210519/Full/result/") library(pheatmap) data=read.table(file="3groupforheatmap.txt",header=T,sep="\t") rownames(data)=data[,11] annotation_row = data.frame( Cluster = factor(rep(c("Cluster1", "Cluster2", "Cluster3","Cluster4","Cluster5"), c(429, 353, 113,593,645))) )
rownames(annotation_row) = rownames(data) pheatmap(data[,1:9],show_rownames =F,cluster_cols = F,cluster_rows=F,fontsize_row=7,annotation_row=annotation_row)
library(ComplexHeatmap) library(circlize) data=data.matrix(data) color = colorRamp2(seq(0, 100,length=2), c("gray95","firebrick3")) labels_row=c("GYPA","CDH5","SELP") ht = Heatmap(data,name = "Positive beads(%)",col = color,rect_gp = gpar(col = "white", lwd = 2), cluster_columns = FALSE, cluster_rows = FALSE, width = ncol(data)*unit(5, "mm"), height = nrow(data)*unit(6, "mm"), show_row_names = F, show_column_names = F, heatmap_legend_param = list(legend_direction="horizontal", legend_width=unit(5,"cm"),title_position="lefttop") ) draw(ht,heatmap_legend_side="bottom")
setwd("E://Datasetlocalize/People/HSY/CRC/20210729_CRC_Glycosylation_project/Glycosylation/") library(pheatmap) data=read.table(file="tangjihuaheatmap.txt",header=T,sep="\t") data=data.matrix(data) pheatmap(data[,1:9],show_rownames =F,fontsize_row=3,cluster_col=F,cluster_rows = F, cellwidth = 24, colorRampPalette(colors = c("forestgreen","white","red"))(100), gaps_row = c(242,458,504,666,690,695,918))
)
library(grid) library(gtable)
heatmap_motor <- function (matrix, border_color, cellwidth, cellheight, tree_col, tree_row, treeheight_col, treeheight_row, filename, width, height, breaks, color, legend, annotation_row, annotation_col, annotation_colors, annotation_legend, annotation_names_row, annotation_names_col, main, fontsize, fontsize_row, fontsize_col, hjust_col, vjust_col, angle_col, fmat, fontsize_number, number_color, gaps_col, gaps_row, labels_row, labels_col, ...) { lo = pheatmap:::lo(coln = labels_col, rown = labels_row, nrow = nrow(matrix), ncol = ncol(matrix), cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, angle_col = angle_col, gaps_row = gaps_row, gaps_col = gaps_col, ...) res = lo$gt mindim = lo$mindim if (!is.na(filename)) { if (is.na(height)) { height = convertHeight(gtable_height(res), "inches", valueOnly = T) } if (is.na(width)) { width = convertWidth(gtable_width(res), "inches", valueOnly = T) } r = regexpr("\\.[a-zA-Z]*$", filename) if (r == -1) stop("Improper filename") ending = substr(filename, r + 1, r + attr(r, "match.length")) f = switch(ending, pdf = function(x, ...) pdf(x, ...), png = function(x, ...) png(x, units = "in", res = 300, ...), jpeg = function(x, ...) jpeg(x, units = "in", res = 300, ...), jpg = function(x, ...) jpeg(x, units = "in", res = 300, ...), tiff = function(x, ...) tiff(x, units = "in", res = 300, compression = "lzw", ...), bmp = function(x, ...) bmp(x, units = "in", res = 300, ...), stop("File type should be: pdf, png, bmp, jpg, tiff")) f(filename, height = height, width = width) gt = heatmap_motor(matrix, cellwidth = cellwidth, cellheight = cellheight, border_color = border_color, tree_col = tree_col, tree_row = tree_row, treeheight_col = treeheight_col, treeheight_row = treeheight_row, breaks = breaks, color = color, legend = legend, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, filename = NA, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, labels_row = labels_row, labels_col = labels_col, gaps_col = gaps_col, gaps_row = gaps_row, ...) grid.draw(gt) dev.off() return(gt) } if (mindim < 3) border_color = NA if (!is.na(main)) { elem = pheatmap:::draw_main(main, fontsize = 1.3 * fontsize, ...) res = gtable_add_grob(res, elem, t = 1, l = 3, name = "main", clip = "off") } if (!pheatmap:::is.na2(tree_col) & treeheight_col != 0) { elem = pheatmap:::draw_dendrogram(tree_col, gaps_col, horizontal = T) res = gtable_add_grob(res, elem, t = 2, l = 3, name = "col_tree") } if (!pheatmap:::is.na2(tree_row) & treeheight_row != 0) { elem = pheatmap:::draw_dendrogram(tree_row, gaps_row, horizontal = F) res = gtable_add_grob(res, elem, t = 4, l = 1, name = "row_tree") } elem = pheatmap:::draw_matrix(matrix, border_color, gaps_row, gaps_col, fmat, fontsize_number, number_color) res = gtable_add_grob(res, elem, t = 4, l = 3, clip = "off", name = "matrix") if (length(labels_col) != 0) { pars = list(labels_col, gaps = gaps_col, fontsize = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, ...) elem = do.call(pheatmap:::draw_colnames, pars) res = gtable_add_grob(res, elem, t = 5, l = 3, clip = "off", name = "col_names") } if (length(labels_row) != 0) { pars = list(labels_row, gaps = gaps_row, fontsize = fontsize_row, ...) elem = do.call(pheatmap:::draw_rownames, pars) res = gtable_add_grob(res, elem, t = 4, l = 3, clip = "off", name = "row_names") } if (!pheatmap:::is.na2(annotation_col)) { converted_annotation = convert_annotations(annotation_col, annotation_colors) elem = pheatmap:::draw_annotations(converted_annotation, border_color, gaps_col, fontsize, horizontal = T) res = gtable_add_grob(res, elem, t = 3, l = 3, clip = "off", name = "col_annotation") if (annotation_names_col) { elem = pheatmap:::draw_annotation_names(annotation_col, fontsize, horizontal = T) res = gtable_add_grob(res, elem, t = 3, l = 4, clip = "off", name = "col_annotation_names") } } if (!pheatmap:::is.na2(annotation_row)) { converted_annotation = convert_annotations(annotation_row, annotation_colors) elem = pheatmap:::draw_annotations(converted_annotation, border_color, gaps_row, fontsize, horizontal = F) res = gtable_add_grob(res, elem, t = 4, l = 2, clip = "off", name = "row_annotation") if (annotation_names_row) { elem = pheatmap:::draw_annotation_names(annotation_row, fontsize, horizontal = F, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col) res = gtable_add_grob(res, elem, t = 5, l = 2, clip = "off", name = "row_annotation_names") } } annotation = c(annotation_col[length(annotation_col):1], annotation_row[length(annotation_row):1]) annotation = annotation[unlist(lapply(annotation, function(x) !pheatmap:::is.na2(x)))] if (length(annotation) > 0 & annotation_legend) { elem = pheatmap:::draw_annotation_legend(annotation, annotation_colors, border_color, fontsize = fontsize, ...) t = ifelse(is.null(labels_row), 4, 3) res = gtable_add_grob(res, elem, t = t, l = 6, b = 5, clip = "off", name = "annotation_legend") } if (!pheatmap:::is.na2(legend)) { elem = pheatmap:::draw_legend(color, breaks, legend, fontsize = fontsize, ...) t = ifelse(is.null(labels_row), 4, 3) res = gtable_add_grob(res, elem, t = t, l = 5, b = 5, clip = "off", name = "legend") } return(res) }
lo <- function (rown, coln, nrow, ncol, cellheight = NA, cellwidth = NA, treeheight_col, treeheight_row, legend, annotation_row, annotation_col, annotation_colors, annotation_legend, annotation_names_row, annotation_names_col, main, fontsize, fontsize_row, fontsize_col, angle_col, gaps_row, gaps_col, ...) { if (!is.null(coln[1]) | (!pheatmap:::is.na2(annotation_row) & annotation_names_row)) { if (!is.null(coln[1])) { t = coln } else { t = "" } tw = strwidth(t, units = "in", cex = fontsize_col/fontsize) if (annotation_names_row) { t = c(t, colnames(annotation_row)) tw = c(tw, strwidth(colnames(annotation_row), units = "in")) } longest_coln = which.max(tw) gp = list(fontsize = ifelse(longest_coln <= length(coln), fontsize_col, fontsize), ...) coln_height = unit(1, "grobheight", textGrob(t[longest_coln], rot = angle_col, gp = do.call(gpar, gp))) + unit(10, "bigpts") } else { coln_height = unit(5, "bigpts") } if (!is.null(rown[1])) { t = rown tw = strwidth(t, units = "in", cex = fontsize_row/fontsize) if (annotation_names_col) { t = c(t, colnames(annotation_col)) tw = c(tw, strwidth(colnames(annotation_col), units = "in")) } longest_rown = which.max(tw) gp = list(fontsize = ifelse(longest_rown <= length(rown), fontsize_row, fontsize), ...) rown_width = unit(1, "grobwidth", textGrob(t[longest_rown], rot = 0, gp = do.call(gpar, gp))) + unit(10, "bigpts") } else { rown_width = unit(5, "bigpts") } gp = list(fontsize = fontsize, ...) if (!pheatmap:::is.na2(legend)) { longest_break = which.max(nchar(names(legend))) longest_break = unit(1.1, "grobwidth", textGrob(as.character(names(legend))[longest_break], gp = do.call(gpar, gp))) title_length = unit(1.1, "grobwidth", textGrob("Scale", gp = gpar(fontface = "bold", ...))) legend_width = unit(12, "bigpts") + longest_break * 1.2 legend_width = max(title_length, legend_width) } else { legend_width = unit(0, "bigpts") } if (is.na(main)) { main_height = unit(0, "npc") } else { main_height = unit(1.5, "grobheight", textGrob(main, gp = gpar(fontsize = 1.3 * fontsize, ...))) } textheight = unit(fontsize, "bigpts") if (!pheatmap:::is.na2(annotation_col)) { annot_col_height = ncol(annotation_col) * (textheight + unit(2, "bigpts")) + unit(2, "bigpts") t = c(as.vector(as.matrix(annotation_col)), colnames(annotation_col)) annot_col_legend_width = unit(1.2, "grobwidth", textGrob(t[which.max(nchar(t))], gp = gpar(...))) + unit(12, "bigpts") if (!annotation_legend) { annot_col_legend_width = unit(0, "npc") } } else { annot_col_height = unit(0, "bigpts") annot_col_legend_width = unit(0, "bigpts") } if (!pheatmap:::is.na2(annotation_row)) { annot_row_width = ncol(annotation_row) * (textheight + unit(2, "bigpts")) + unit(2, "bigpts") t = c(as.vector(as.matrix(annotation_row)), colnames(annotation_row)) annot_row_legend_width = unit(1.2, "grobwidth", textGrob(t[which.max(nchar(t))], gp = gpar(...))) + unit(12, "bigpts") if (!annotation_legend) { annot_row_legend_width = unit(0, "npc") } } else { annot_row_width = unit(0, "bigpts") annot_row_legend_width = unit(0, "bigpts") } annot_legend_width = max(annot_row_legend_width, annot_col_legend_width) treeheight_col = unit(treeheight_col, "bigpts") + unit(5, "bigpts") treeheight_row = unit(treeheight_row, "bigpts") + unit(5, "bigpts") if (is.na(cellwidth)) { mat_width = unit(1, "npc") - rown_width - legend_width - treeheight_row - annot_row_width - annot_legend_width } else { mat_width = unit(cellwidth * ncol, "bigpts") + length(gaps_col) * unit(4, "bigpts") } if (is.na(cellheight)) { mat_height = unit(1, "npc") - main_height - coln_height - treeheight_col - annot_col_height } else { mat_height = unit(cellheight * nrow, "bigpts") + length(gaps_row) * unit(4, "bigpts") } gt = gtable(widths = unit.c(treeheight_row, rown_width, mat_width, treeheight_row, legend_width, annot_legend_width), heights = unit.c(main_height, treeheight_col, annot_col_height, mat_height, coln_height), vp = viewport(gp = do.call(gpar, gp))) cw = convertWidth(mat_width - (length(gaps_col) * unit(4, "bigpts")), "bigpts", valueOnly = T)/ncol ch = convertHeight(mat_height - (length(gaps_row) * unit(4, "bigpts")), "bigpts", valueOnly = T)/nrow mindim = min(cw, ch) res = list(gt = gt, mindim = mindim) return(res) }
draw_rownames <- function (rown, gaps, ...) { coord = pheatmap:::find_coordinates(length(rown), gaps) y = unit(1, "npc") - (coord$coord - 0.5 * coord$size) res = textGrob(rown, x = unit(-3, "bigpts"), y = y, vjust = 0.5, hjust = 1, gp = gpar(...)) return(res) }
assignInNamespace(x="draw_rownames", value=draw_rownames, ns="pheatmap") assignInNamespace(x="lo", value=lo, ns="pheatmap") assignInNamespace(x="heatmap_motor", value=heatmap_motor, ns="pheatmap")
library(ComplexHeatmap) library(circlize) NDMM = read.table(file = "NDMM.txt",header = T,sep = "\t",row.names = 1) RRMM = read.table(file = "RRMM.txt",header = T,sep = "\t",row.names = 1)
pdf("positive beads.pdf") NDMM = data.matrix(NDMM) RRMM = data.matrix(RRMM) color = colorRamp2(seq(0, 100,length=2), c("gray95","maroon")) labels_row=c("BCMA","CD47","CD147","CD98","CD38") labels_col=c("Isotype","Control","NDMM1","NDMM2","NDMM3","NDMM4","Isotype","Control","RRMM1","RRMM2","RRMM3") ht1=Heatmap(NDMM,name = "Positive beads(%)",col = color,rect_gp = gpar(col = "white", lwd = 2), cluster_columns = FALSE, cluster_rows = FALSE, width = ncol(data)*unit(6, "mm"), height = nrow(data)*unit(6, "mm"), show_row_names = T, show_column_names = T,row_names_side = "left",column_names_side = "top",column_names_rot = 45, heatmap_legend_param = list(legend_direction="horizontal", legend_width=unit(5,"cm"),title_position="lefttop") ) ht2=Heatmap(RRMM,name = "Positive beads(%)",col = color,rect_gp = gpar(col = "white", lwd = 2), cluster_columns = FALSE, cluster_rows = FALSE, width = ncol(data)*unit(5, "mm"), height = nrow(data)*unit(6, "mm"), show_row_names = T, show_column_names = T,row_names_side = "left",column_names_side = "top",column_names_rot = 45, heatmap_legend_param = list(legend_direction="horizontal", legend_width=unit(5,"cm"),title_position="lefttop") ) ht_list = ht1 + ht2 draw(ht_list,heatmap_legend_side="bottom",ht_gap =unit(3,"mm")) dev.off()
setwd("F:/Taogroup/People/SJ/20220301PRM/heatmap/") library(pheatmap) data=read.table(file="salivaheatmap1.txt",header=T,sep="\t",row.names = 1) annotation_col = read.table(file = "annotation.txt",header=T,sep="\t") rownames(annotation_col)=colnames(data)
pdf(file = "salivaheatmap.pdf",width = 10,height = 15) pheatmap(data,show_rownames =F,cluster_cols = F,cluster_rows=F, fontsize_row=7,show_colnames = F, annotation_col = annotation_col, cellwidth = 10,cellheight = 20, colorRampPalette(colors = c("#7AC5CD","white","#EE6A50"))(50), gaps_col = c(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32)) dev.off()
library(pheatmap) exp = read.table("exp_top30.original.txt",header=T,row.names=1,sep="t") result <- pheatmap(data,cluster_cols = F,clustering_distance_rows = "euclidean",clustering_method = "average") col_oder=result$order
row_oder=result$tree_row$order
cn_new <- colnames(data)[col_oder]
rn_new <- rownames(data)[row_oder]
new_exp <- matrix(rep(0,ncol(data)*nrow(data)),nrow = nrow(data),ncol = ncol(data))
out <- matrix(rep(0,ncol(data)*nrow(data)),nrow = nrow(data),ncol = ncol(data))
for (i in 1:ncol(data)){ new_exp[,i]=data[,i][row_oder] }
rownames(new_exp)=rn_new
colnames(new_exp)=cn_new
write.table(new_exp,"hp_exp.txt",sep="\t")
|