R pheatmap determine column order - r

Hi Stack Overflow community,
I am creating heatmap using pheatmap() and have difficulty ordering columns in a sorted order. My data 'matrix' is already sorted in the desired order for display on the plot.
I tried save my desired order (ascending order) into 'col_order' and use 'column_order = col_order' as an option in pheatmap(). However, it doesn't work. I also tried set 'cluster_cols = F' but R returns error message 'formal argument "cluster_cols" matched by multiple actual arguments'
I appreciate any comment!
Here is my code:
pheatmap(matrix,
annotation_row=groupmatrix,
cluster_cols = T,
color = colorRampPalette(c("blue", "white", "red"))(50),
show_colnames = T,
scale="row",
border_color ="NA",
fontsize =8,
fontsize_row=6,
fontsize_col=6,
treeheight_row = 0,
treeheight_col = 0, # remove dendrogram
cluster_rows = F,
cluster_cols = F)
Here is what my plot looks like right now, the x-axis order is all mess up and I want it to go from 1 to 48 in order.

Try this:
Remove the cluster_cols = TRUE in the third row:
Here is an example:
library(pheatmap)
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 = "")
pheatmap(test,
color = colorRampPalette(c("blue", "white", "red"))(50),
show_colnames = T,
scale="row",
border_color ="NA",
fontsize =8,
fontsize_row=6,
fontsize_col=6,
treeheight_row = 0,
treeheight_col = 0, # remove dendrogram
cluster_rows = F,
cluster_cols = F)

Related

Error in printing values within complex heatmap

I am trying to use the ComplexHeatmap R package to generate heatmaps and also print the values.
NES_final is a list of list containing expt at first level and scenario at second level. Within each expt I need to create a combined heatmap of all scenario.
The heatmap is created as expected, but while printing only the last value of scenario in each expt gets printed for all heatmaps within that expt.
Test data:
NES_final = list("expt1" = list("scenario1" = matrix(rnorm(36), nrow=3, ncol=3),
"scenario2" = matrix(rnorm(36), nrow=3, ncol=3),
"scenario3" = matrix(rnorm(36), nrow=3, ncol=3)),
"expt2" = list("scenario1" = matrix(rnorm(36), nrow=3, ncol=3),
"scenario2" = matrix(rnorm(36), nrow=3, ncol=3),
"scenario3" = matrix(rnorm(36), nrow=3, ncol=3)),
"expt3" = list("scenario1" = matrix(rnorm(36), nrow=3, ncol=3),
"scenario2" = matrix(rnorm(36), nrow=3, ncol=3),
"scenario3" = matrix(rnorm(36), nrow=3, ncol=3)))
Code:
for(expt in names(NES_final)){
NES <- NES_final[[expt]]
heatMap <- NULL
for(scenario in names(NES)){
heatMap <- heatMap + Heatmap(NES[[scenario]],
cluster_rows = FALSE,
cluster_columns = FALSE,
name = scenario,
column_title = scenario,
show_heatmap_legend = TRUE,
cell_fun = function(j, i, x, y, width, height, fill){
grid.text(scenario, x, y, gp = gpar(fontsize = 2))
}
)
}
svglite(paste0("/Analysis_1/Heatmap_", expt, ".svg"), width = length(NES) * 25, height = 6)
draw(heatMap, column_title = expt)
dev.off()
}
I was able to find a solution using the lapply function instead of the for loop for scenario.
heatMap <- lapply(names(NES), function(scenario){
Heatmap(NES[[scenario]],
cluster_rows = TRUE,
cluster_columns = FALSE,
name = scenario,
column_title = scenario,
show_heatmap_legend = FALSE,
cell_fun = function(j, i, x, y, width, height, fill){
grid.text(scenario, x, y, gp = gpar(fontsize = 5))
} ) }) }
heatMap <- Reduce("+", heatMap)
draw(heatMap, column_title = expt, padding = unit(c(0.5, 0.5, 0.5, 15), "cm"))

Fill cells with value = 0 with Red color

I have a table:
and i need to fill cells with value = 0 with red color.
I realize i need data_color but how to set palette with 2 colors? red if value==0 , white if value<>0 ?
I've found an example:
data_color(
columns = colnames(updates.computers)[2:ncol(updates.computers)],
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material", direction = -1
) %>% as.character(),
domain = NULL
),
alpha = 0.8)
But it's not what I need.
I just need cells with 0 to be Red, and all the rest to stay White.
Have a look at the scales::col_bin function.
By cutting the data into two bins (0 and from 1 to infinity) you can specify the colors for each bin.
df <- data.frame(A2020 = sample(0:10, 12, replace = TRUE),
B2020 = sample(0:10, 12, replace = TRUE),
C2020 = sample(0:10, 12, replace = TRUE),
D2020 = sample(0:10, 12, replace = TRUE),
E2020 = sample(0:10, 12, replace = TRUE),
F2020 = sample(0:10, 12, replace = TRUE),
G2020 = sample(0:10, 12, replace = TRUE),
H2020 = sample(0:10, 12, replace = TRUE),
I2020 = sample(0:10, 12, replace = TRUE),
J2020 = sample(0:10, 12, replace = TRUE))
gt(df) %>%
data_color(
columns = everything(),
colors = scales::col_bin(
bins = c(0, 1, Inf),
palette = c("red", "white"),
)
)
Thanks, it works. The code:
data_color(
columns = colnames(updates.computers)[2:ncol(updates.computers)],
colors = scales::col_bin(
bins = c(0, 1, Inf),
palette = c("red", "darkseagreen1"),
),
alpha = 0.8
)
The result:
result

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á

Pheatmap annotation colors edit size

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)

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