Pheatmap library(pheatmap) takes annotation_colors to add the header ID colors on the top of each heatmap column.
I want to control and edit the size of the column header (boxes red and blue).
Below is what I have done so far.
library(pheatmap)
set.seed(123)
df<-data.frame( matrix(sample(30), ncol = 5))
colnames(df)<-LETTERS[1:5]
subj<-c("P1", "P2","P3", "T1", "T2","T3")
rownames(df)<-subj
aka2 = data.frame(ID = factor(rep(c("Pat","Trea"), each=3)))
rownames(aka2)<-subj
aka3 = list(ID = c(Pat = "red", Trea="blue"))
pheatmap(t(scale(df)),
annotation_col = aka2,
annotation_colors = aka3[1],
annotation_legend = FALSE,
gaps_col = 3,
show_colnames = T, show_rownames = T, cluster_rows = F,
cluster_cols = F, legend = TRUE,
clustering_distance_rows = "euclidean", border_color = FALSE)
Related
I want to make a legend for the color annotation blocks at right-side of the complexheatmap graph
library(ComplexHeatmap)
x11()
#list = c("R","H","K","D",'E','S','T','N','Q','C','G','P','A','V','I','L','M','F','Y','W','O','U','B','Z','X','J')
A = rep("group1", 26)
A[4:5] = "group2"
A[6:9] = "group3"
A[10:12] = "group4"
A[13:20] = "group5"
A[21:26] = "group6"
rnames <- c("+","+","+", "-", "-","δ","δ","δ","δ", "*","*","*","h","h","h","h","h","h","h","h","u","u","u","u","u","u")
ha = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:7)), annotation_legend_param = list(foo = list(labels = c("+ve", "-ve", "polar", "special","hydrophobic","unique"))))
names(rnames) = unique(A)
ht = Heatmap(data2,
name = "Occurance",
cluster_rows = FALSE,
cluster_columns = FALSE,
column_title = "Amino Acid Occurance in Overlapped Region in LCD in Known RBP genes",
row_title = "AA",
column_names_gp = gpar(fontsize = 10),
row_title_gp = gpar(fontsize = 20),
column_names_rot = 90,
row_split = A,
right_annotation = ha,
row_names_side = "left",
row_names_gp = gpar(fontsize = 10),
heatmap_width = unit(12, "cm"),
heatmap_height = unit(12, "cm")
)
ht
and the result of graph is shown at below
As you can see there are 6 different color annotation block shown at right side, I want to make the legend for these 6 block named in order as "+ve", "-ve", "polar", "special","hydrophobic" and "unique". However, the above codes only result in an error of 'Error: Amount of legend params is larger than the number of simple annotations.'
Can anyone tell me and teach me how to do it?
I've built a heatmap but I can't seem to set up groups next to the rows and columns. Please I want something like this heatmap.
#Design of the data as an example
Data <- data.frame(replicate(6,sample(0:1,6,rep=TRUE)))
row.names(Data) <- c('Gene1','Gene2','Gene3','Gene4','Gene5','Gene6')
colnames(Data) <- c("Strain1", "Strain2", "Strain3", "Strain4", "Strain5", "Strain6")
pheatmap(Data, color=colorRampPalette(c("#FFE4B5","#708090"))(2),
legend_breaks = c(1, 0),
legend_labels = c("Presence", "Absence"),
border_color = "black", display_numbers = FALSE,
number_color = "black",
fontsize_number = 8)
#Now I want to create Groups (Row) ##To designate that they are
part of the same antibiotic resistance family##
#Groupe1 "Gene3 and Gene6" Color1
#Groupe2 "Gene1 and Gene5" Color2
#Groupe3 "Gene2 and Gene4" Color3
#How can I do this?
#Same question if I want to create Groupes for column ##To
designate that they are part of the same bacterial genus#
#Band1 "Strain1" and "Strain3" Color4
#Band2 "Strain4" and "Strain5" Color5
#Band3 "Strain2" and "Strain6" Color6
You need to create little data frames for your annotations and pass them to annotation_row and annotation_col. The actual colours are passed as a named list of two named vectors to annotation_colors:
row_df <- data.frame(Group = paste("Groupe", rep(1:3, each = 2)),
row.names = paste0("Gene", 1:6))
col_df <- data.frame(Genus = c("GenusA", "GenusC", "GenusA",
"GenusB", "GenusB", "GenusC"),
row.names = paste0("Strain", 1:6))
pheatmap(Data,
color = colorRampPalette(c("#FFE4B5","#708090"))(2),
legend_breaks = c(1, 0),
legend_labels = c("Presence", "Absence"),
border_color = "black", display_numbers = FALSE,
number_color = "black",
annotation_row = row_df,
annotation_col = col_df,
fontsize_number = 8,
annotation_colors = list(Group = c("Groupe 1" = "red3", "Groupe 2" = "blue4",
"Groupe 3" = "green4"),
Genus = c(GenusA = "gold", GenusB = "purple",
GenusC = "cyan")))
I have the following data frame:
library(pheatmap)
library(RColorBrewer)
dat <- structure(c(36.28516213, -9.184523335, 5.716034994, 32.82180488,
-18.5467127, 22.79930301, -24.79075287, -49.23768715), .Dim = c(2L,
4L), .Dimnames = list(c("motif_a", "motif_b"), c("cow", "dog",
"snake", "cat")))
dat
#> cow dog snake cat
#> motif_a 36.285162 5.716035 -18.54671 -24.79075
#> motif_b -9.184523 32.821805 22.79930 -49.23769
And with the following heatmap code:
p <- pheatmap(
dat,
color = colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(1000),
clustering_method = "ward.D2",
border_color = "grey60",
clustering_distance_columns = "correlation",
clustering_distance_rows = "correlation",
fontsize_row = 6,
cluster_rows = TRUE,
cluster_cols = TRUE,
scale = "row"
)
I can get this plot.
As noted in the plot how can I get corresponding value for
every point in the heatmap to the right-hand color scale.
For example, cow motif_b has value -9.184523, but the right-hand
color scale gives a value between 0 to -0.5. How can I get these values?
The final result is a data frame with the values transformed.
How can I do that?
You asked pheatmap to plot the Pearson correlation, which is between -1 and 1, when you specified clustering_distance_xxx = "correlation". Run ?pheatmap for more details.
If you remove those parts from the code, then you'll get what you expected
library(pheatmap)
library(RColorBrewer)
dat <- structure(c(36.28516213, -9.184523335, 5.716034994, 32.82180488,
-18.5467127, 22.79930301, -24.79075287, -49.23768715), .Dim = c(2L,
4L), .Dimnames = list(c("motif_a", "motif_b"), c("cow", "dog",
"snake", "cat")))
myCol <- colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(8)
p <- pheatmap(
dat,
color = myCol,
clustering_method = "ward.D2",
border_color = "grey60",
clustering_distance_columns = "correlation",
clustering_distance_rows = "correlation",
fontsize_row = 6,
cluster_rows = TRUE,
cluster_cols = TRUE,
scale = "row"
)
p1 <- pheatmap(
dat,
color = myCol,
clustering_method = "ward.D2",
border_color = "grey60"
)
Created on 2018-03-27 by the reprex package (v0.2.0).
is there any way to specify color key for d3heatmap in R? following is the code with which I am able to make the heatmap but could not show a color key. Kindly provide some answers.
library(d3heatmap)
d3heatmap(data, Rowv = TRUE, Colv = TRUE,
distfun = dist, hclustfun = hclust, dendrogram = c("both", "row",
"column", "none"), reorderfun = function(d, w) reorder(d, w), k_row=400, symm = FALSE, scale = "row", na.rm = TRUE,labRow = rownames(data), labCol = colnames(data), digits = 3L,theme = NULL, color = scales::col_quantile("Blues", NULL, 5),
width = 960, height = 500, xaxis_height = 80, yaxis_width = 120,
xaxis_font_size = "12pt", yaxis_font_size = "12pt", brush_color ="#0000FF",
show_grid = TRUE, anim_duration = 0)
I am creating a heatmap using the following code:
pheatmap(tissuedata3,
color = colorRampPalette(rev(c("#D73027", "#FC8D59", "#FEE090", "#FFFFBF", "#E0F3F8", "#91BFDB", "#4575B4")))(100),
cellwidth = 20, cellheight = 2.8,
border=TRUE,
treeheight_row=0,
treeheight_column=0,
kmeans_k = NA,
show_rownames = T, show_colnames = T,
fontsize=2,
scale="none",
clustering_method = "complete",
cluster_rows = FALSE, cluster_cols = TRUE,
clustering_distance_rows = "euclidean",
clustering_distance_cols = "euclidean",
legend=TRUE,
)
However, the resulting heatmap does not contain any borders. Does anyone know how to add borders to the cells and the heatmap as a whole?
I think the correct parameter is border_color whose default is "grey60", so I suspected (incorrectly as it turned out) you were getting partial name match from your argument "border" whose value becomes colors()[1] which is [1] "white".
Try:
library(pheatmap)
pheatmap(tissuedata3,
color = colorRampPalette(rev(c("#D73027", "#FC8D59", "#FEE090", "#FFFFBF", "#E0F3F8", "#91BFDB", "#4575B4")))(100),
cellwidth = 20, cellheight = 3, # changed to 3
border_color="blue",
treeheight_row=0,
treeheight_column=0,
kmeans_k = NA,
show_rownames = T, show_colnames = T,
fontsize=2,
scale="none",
clustering_method = "complete",
cluster_rows = FALSE, cluster_cols = TRUE,
clustering_distance_rows = "euclidean",
clustering_distance_cols = "euclidean",
legend=TRUE
)