ComplexHeatmap, cannot create horizontal legend - r

Hi I'm using the ComplexHeatmap package and followed their vignette however for some reason I cannot seem to force the legend to become horizontal. So for example here is an example,
set.seed(123)
library(ComplexHeatmap)
mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)
ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))),
col = list(type1 = c("a" = "red", "b" = "blue")),
annotation_legend_param = list(type1 = list(
title_gp = gpar(fontsize = 16),
legend_direction = "horizontal", labels_gp = gpar(fontsize = 8)))
)
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column)
draw(ht1, heatmap_legend_side = "right")
so despite add in legend_direction = "horizontal" I still keep getting this here,

If you need to plot the heatmap legend horizontally and at the bottom of the heatmap, you can use this solution:
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column,
heatmap_legend_param = list(
legend_direction = "horizontal",
legend_width = unit(5, "cm")
)
)
draw(ht1, heatmap_legend_side = "bottom")
Otherwise, if you need to draw your (discrete) annotation legend horizontally and at the top of the heatmap, you can use nrow=1 in annotation_legend_param:
ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))),
col = list(type1 = c("a" = "red", "b" = "blue")),
annotation_legend_param = list(
type1 = list(
title_gp = gpar(fontsize = 16),
labels_gp = gpar(fontsize = 8),
nrow=1)))
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column)
draw(ht1, annotation_legend_side = "top")

Related

How to make a legend based on the color annotation blocks?

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?

superscript in row names ComplexHeatmap R

I generated a heatmap graphic with the following code:
A = c(rep(100,22),rep(0,15),rep(10,12), rep(50,14))
B = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14))
C = c(rep(0,22),rep(8,15),rep(13,6),rep(75,6), rep(82,14))
D = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14))
E = c(rep(100,22),rep(0,15),rep(10,12), rep(50,14))
F = c(rep(0,22),rep(8,15),rep(13,6),rep(75,6), rep(82,14))
G = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14))
H = c(rep(80,22),rep(72,15),rep(5,12), rep(2,14))
df= data.frame(A,B,C,D,E,F,G, H)
correct_columnnames <- c("34T","56G", "45T","78T", "12F","25G","90F","78T")
colnames(df) <- correct_columnnames
split = c(rep(1,22),rep(2,15),rep(3,12), rep(4,14))
row_labels = c(rep("A",22),rep("B",15),rep("C",12), rep("D",14))
rowlabels= data.frame(row_labels)
row_labels = rowAnnotation(df = rowlabels,
col = list(row_labels = c("A"="#e48f1c","B"="#570c32","C"="#e5c616","C"="#d33b44", "D" = "#38ECF9")),
annotation_legend_param = list(direction = "horizontal",
nrow = 1,
title = "Groups",
title_position = "topcenter",
legend_gp = gpar(fontsize = 14),
labels_gp = gpar(fontsize = 14)),
gp = gpar(col = "grey") )
h1 = Heatmap(df,
clustering_method_columns = "average",
rect_gp = gpar(col = "grey", lwd = 1),
right_annotation = row_labels,
cluster_rows = FALSE,
heatmap_legend_param = list(legend_direction = "horizontal",
title = "M porcentage",
legend_width = unit(11, "cm"),
title_position = "topcenter",
at = c(0,10,20,30,40,50,60,70,80,90,100),
labels = c(0,10,20,30,40,50,60,70,80,90,100),
fontsize = 16),
width = ncol(filtered_data)*unit(5, "mm"),
height = nrow(filtered_data)*unit(5, "mm"),
column_split = 7,
column_title = NULL,
row_split = split,
row_title = NULL)
png("test.png",width=20,height=20, res = 300, "in")
draw(h1, heatmap_legend_side = "bottom", annotation_legend_side = "bottom" )
dev.off()
The script gives me a figure like this:
My question is:
Is it possible to write some letters in superscript in ComplexHeatmap in the row names and change row_labels to "row labels"?
so the graphic should be like this:

How to show annotation in ComplexHeatmap in R

I have 31 samples,and get 31 type of samples, such "a","b","c","d",but when i draw heatmap,it say the subscript out.I have check the columns of the matrix is 31 ,which is the same as the number of types.
ha1 = HeatmapAnnotation( bar = c("b","d","d","b","b","c","b","a","b"
,"b","a","d","b","b","c","c","a","b"
,"d","c","c","d","b","b","b","d","a"
,"c","c","b","c"),
col = list(bar = c("a" = "red", "b" = "green", "c" = "blue","d"="black"))
)
ha = rowAnnotation(foo = anno_mark(at = seq(0,7000,1000), labels = seq(0,7000,1000)))
hist_major <- Heatmap(major,
column_title = "Statistics for a list of SNP.Frequency.major",
column_title_gp = gpar(fontsize = 15, fontface = "bold"),
column_names_gp = gpar(fontsize = 10),
row_title = "Genome Postion(bp)",
row_title_gp = gpar(fontsize = 10),
name = "Variant.Frequency",
cluster_rows = FALSE,
column_order=order(as.numeric(gsub("SAMN041259", "", colnames(major)))),
show_column_dend = FALSE,
right_annotation = ha,
bottom_annotation = ha1,
show_row_names = FALSE,
use_raster= TRUE,
raster_resize_mat = max,
col = cols,
border_gp = gpar(col = "black", lty = 2)
)
This is a simple example that works with your code. Maybe it can help you.
library(ComplexHeatmap)
# Data matrix
major <- matrix(rnorm(3100),ncol=31)
colnames(major) <- paste0("Sample",1:31)
# Your code
ha1 = HeatmapAnnotation( bar = c("b","d","d","b","b","c","b","a","b"
,"b","a","d","b","b","c","c","a","b"
,"d","c","c","d","b","b","b","d","a"
,"c","c","b","c"),
col = list(bar = c("a"="red", "b"="green", "c"="blue", "d"="black"))
)
ticks <- seq(0,100,10)
ticks[1] <- 1
ha = rowAnnotation(foo = anno_mark(at = ticks, labels = ticks))
cols <- heat.colors(20)
hist_major <- Heatmap(major,
column_title = "Statistics for a list of SNP.Frequency.major",
column_title_gp = gpar(fontsize = 15, fontface = "bold"),
column_names_gp = gpar(fontsize = 10),
row_title = "Genome Postion(bp)",
row_title_gp = gpar(fontsize = 10),
name = "Variant\nFrequency",
cluster_rows = FALSE,
column_order=order(as.numeric(gsub("SAMN041259", "", colnames(major)))),
show_column_dend = FALSE,
right_annotation = ha,
bottom_annotation = ha1,
show_row_names = FALSE,
use_raster= TRUE,
raster_resize_mat = max,
col = cols,
border_gp = gpar(col = "black", lty = 2)
)
print(hist_major)

How can I highlight specific genes in Bioconductor Enhancedvolcano?

I like the package EnhancedVolcano. My data is RNAseq and I analyse it with DESeq2. I want to plot the results as a volcanoplot where I highlight a list of genes of my choice picked_genes. I have succeded in changing pointSize and I am using SelectLab to highlight but when I want to give the chosen genes another color I get stuck. I have added a logical vector to my results file specifying which genes to highlight. I have tried
col = ifelse...
It doesn't work, all dots are grey.
EnhancedVolcano(res_complete,
lab = res_complete$gene_name,
x = "log2FoldChange",
y = "pvalue",
pCutoff = 10e-3,
FCcutoff = 1,
xlim = c(-10, 10),
ylim = c(0, -log10(10e-12)),
col = (ifelse(res_complete$picked_genes == T, "forestgreen", "grey60")),
pointSize = (ifelse(res_complete$picked_genes == T, 5, 0.5)),
labSize = 2.5,
selectLab = picked_genes,
shape = 16,
shade = res_complete$picked_genes == T,
shadeFill = "forestgreen",
shadeSize = 5,
shadeLabel = res_complete$picked_genes,
boxedLabels = TRUE,
title = "DESeq2 results",
subtitle = "Differential expression HC vs RA",
caption = "FC cutoff, 1; p-value cutoff, 10e-3",
legendPosition = "right",
legendLabSize = 14,
colAlpha = 0.9,
drawConnectors = TRUE,
hline = c(10e-8),
widthConnectors = 0.2)
I have also tried:
colCustom =ifelse...
But I get an error message...
Error: Aesthetics must be either length 1 or the same as the data (58735): colour
EnhancedVolcano(res_complete,
lab = res_complete$gene_name,
x = "log2FoldChange",
y = "pvalue",
pCutoff = 10e-3,
FCcutoff = 1,
xlim = c(-10, 10),
ylim = c(0, -log10(10e-12)),
colCustom = (ifelse(res_complete$picked_genes == T, "forestgreen", "grey60")),
pointSize = (ifelse(res_complete$picked_genes == T, 5, 0.5)),
labSize = 2.5,
selectLab = picked_genes,
shape = 16,
shade = res_complete$picked_genes == T,
shadeFill = "forestgreen",
shadeSize = 5,
shadeLabel = res_complete$picked_genes,
boxedLabels = TRUE,
title = "DESeq2 results",
subtitle = "Differential expression HC vs RA",
caption = "FC cutoff, 1; p-value cutoff, 10e-3",
legendPosition = "right",
legendLabSize = 14,
colAlpha = 0.9,
drawConnectors = TRUE,
hline = c(10e-8),
widthConnectors = 0.2)
Can someone come up with a solution to this problem?
I found it, finally I understood it. colCustom needs a pair for each point, a color and a name. I created the matrix keyvals
keyvals <- ifelse(
res_complet$picked_genes < T, 'grey60',
'forestgreen')
names(keyvals)[keyvals == 'forestgreen'] <- 'picked'
names(keyvals)[keyvals == 'grey60'] <- 'rest'
`
Than I used it to replace the col=
`
EnhancedVolcano(res_complete,
lab = res_complete$gene_name,
x = "log2FoldChange",
y = "pvalue",
pCutoff = 10e-3,
FCcutoff = 1,
xlim = c(-10, 10),
ylim = c(0, -log10(10e-12)),
pointSize = (ifelse(res_complete$picked_genes == T, 5, 0.5)),
labSize = 2.5,
shape = c(19, 19, 19, 19),
selectLab = picked_genes,
boxedLabels = TRUE,
title = "DESeq2 results",
subtitle = "Differential expression HC vs RA",
caption = "FC cutoff, 1; p-value cutoff, 10e-3",
legendPosition = "right",
legendLabSize = 14,
colCustom = keyvals,
colAlpha = 0.9,
drawConnectors = TRUE,
hline = c(10e-8),
widthConnectors = 0.2)
`
In order to get all points visible I sorted my results dataframe after the logical column res_complete$picked_genes and made the volcano again. Voilá

Align text when using tableGrob or grid.table in R

When creating a table using tableGrob or grid.table.
Is there way to align the text inside the table? First column to the left, and the other columns to the right? Rather than the default "center".
Thank you!
something like this: where I want column "a" alligned to the left.
a <- c("one","two","thirty five")
b <- c(1, 2, 3)
c <- c(4, 5, 6)
data <- data.frame(a,b,c)
windows()
grid.table(
data,
gpar.coretext=gpar(fontsize = 12),
gpar.coltext = gpar(fontsize = 12),
gpar.rowtext = gpar(fontsize = 12),
gpar.corefill =
gpar(fill = "green", alpha = 0.5, col = NA),
h.even.alpha = 0.5,
equal.width = FALSE,
show.rownames = FALSE,
show.vlines = TRUE,
padding.h = unit(15, "mm"),
padding.v = unit(8, "mm")
)
With gridExtra v>=2.0.0, the parameters are now controlled via nested lists (themes),
library(gridExtra)
library(grid)
n=5
d <- data.frame(x=rnorm(n),y=rnorm(n),z=sample(letters[1:2],n,replace=T))
m <- format(d, digits = 1, scientific=F,big.mark = ",")
mytheme <- ttheme_default(core = list(fg_params = list(hjust=0, x=0.1,
fontsize=8)),
colhead = list(fg_params = list(fontsize=9,
fontface="bold"))
)
g1 <- tableGrob(m, theme = mytheme, rows=NULL)
grid.newpage()
grid.draw(g1)
Is this what you are looking for? There is a core.just parameter of the format() call.
require("gridExtra")
n=5
df<- data.frame(x=rnorm(n),y=rnorm(n),z=sample(letters[1:2],n,replace=T))
g1<-tableGrob(
format(df, digits = 1,
scientific=F,big.mark = ","),
core.just="left",
#core.just="right",
#col.just="right",
gpar.coretext=gpar(fontsize=8),
gpar.coltext=gpar(fontsize=9, fontface='bold'),
show.rownames = F,
h.even.alpha = 0,
gpar.rowtext = gpar(col="black", cex=0.7,
equal.width = TRUE,
show.vlines = TRUE,
show.hlines = TRUE,
separator="grey")
)
grid.draw(g1)
To set a "transparent" background, use the ttheme_minimal with hjust to set text alignment.
theme_1 <- ttheme_minimal(core = list(fg_params = list(hjust = 0,
x = 0.1,
fontsize = 9)),
colhead = list(fg_params = list(fontsize = 12,
fontface = "bold")))
You can then apply the theme to the tableGrob like this:
gridExtra::tableGrob(df_tbl, theme = theme_1, rows=NULL)

Resources