I use R and "circlize" package. I'm trying to create a circular heatmap and I want to sort them alphabetically within the groups. However, I couldn't find how to do it. How can I achieve this?
To produce this graph the code is below:
set.seed(123)
mat1 = rbind(cbind(matrix(rnorm(50*5, mean = 1), nr = 50),
matrix(rnorm(50*5, mean = -1), nr = 50)),
cbind(matrix(rnorm(50*5, mean = -1), nr = 50),
matrix(rnorm(50*5, mean = 1), nr = 50))
)
rownames(mat1) = paste0("R", 1:100)
colnames(mat1) = paste0("C", 1:10)
mat1 = mat1[sample(100, 100), ] # randomly permute rows
split = sample(letters[1:5], 100, replace = TRUE)
split = factor(split, levels = letters[1:5])
library(circlize) # >= 0.4.10
col_fun1 = colorRamp2(c(-2, 0, 2), c("blue", "white", "red"))
circos.heatmap(mat1, split = split, col = col_fun1, rownames.side = "outside",
rownames.col = 1:nrow(mat1) %% 10 + 1,
rownames.cex = runif(nrow(mat1), min = 0.3, max = 2),
rownames.font = 1:nrow(mat1) %% 4 + 1)
circos.clear()
See Figure1, I will have some groups and I want to sort them alphabetically. What I mean is:
For example, for group A, I want to sort it alphabetically. I should only consider group A as sorting the group. I will apply that to all of the groups. How do I do it?
You can sort the labels in your dataset and use cluster = FALSE
in circos.heatmap()
.
Here is the example code that you can modify according to your dataset.
library(circlize)
library(stringr)
set.seed(123)
mat1 = rbind(cbind(matrix(rnorm(50*5, mean=1), nr=50),
matrix(rnorm(50*5, mean= -1), nr=50)),
cbind(matrix(rnorm(50*5, mean= -1), nr=50),
matrix(rnorm(50*5, mean=1), nr=50)))
rownames(mat1) = paste0("R",1:100)
colnames(mat1) = paste0("C", 1:10)
mat1 = mat1[sample(100, 100), ] # randomly permute rows
mat1 = mat1[str_sort(rownames(mat1), numeric = TRUE),] #sorting the labels for heatmap
split = sample(letters[1:5], 100, replace = TRUE)
split = factor(split, levels = letters[1:5])
#the circular heatmap
circos.clear()
col_fun1 = colorRamp2(c(-2, 0, 2), c("blue", "white", "red"))
circos.heatmap(mat1, split = split, col = col_fun1,
rownames.side = "outside", cluster=FALSE,
rownames.col = 1:nrow(mat1) %% 10 + 1,
rownames.cex = runif(nrow(mat1), min = 0.3, max = 2),
rownames.font = 1:nrow(mat1) %% 4 + 1)