GithubHelp home page GithubHelp logo

Comments (2)

dcarbajo avatar dcarbajo commented on June 23, 2024

I posted a full MWE in here, pasted below as well:

Check the following MWE with the iris data.

1- Create my data matrix (small subset of iris for just setosa and virginica Species), and a meta information data frame with just sample ID and Species (my grouping variable):

data(iris)
my_setosa=subset(iris, Species=="setosa")
my_virginica=subset(iris, Species=="virginica")
set.seed(123)
rows_used1 <- sort(sample(1:nrow(my_setosa), 5, replace = F))
rows_used2 <- sort(sample(1:nrow(my_virginica), 5, replace = F))
my_iris=rbind(my_setosa[rows_used1,], my_virginica[rows_used2,])
#
heat_mat=t(as.matrix(my_iris[,-ncol(my_iris)]))
meta_df=data.frame(ID=paste0("id",rownames(my_iris)), Species=my_iris[,ncol(my_iris)])
meta_df$Species=droplevels(meta_df$Species)
colnames(heat_mat)=meta_df$ID

These look like this:

> heat_mat
             id3 id14 id15 id31 id42 id114 id125 id137 id143 id150
Sepal.Length 4.7  4.3  5.8  4.8  4.5   5.7   6.7   6.3   5.8   5.9
Sepal.Width  3.2  3.0  4.0  3.1  2.3   2.5   3.3   3.4   2.7   3.0
Petal.Length 1.3  1.1  1.2  1.6  1.3   5.0   5.7   5.6   5.1   5.1
Petal.Width  0.2  0.1  0.2  0.2  0.3   2.0   2.1   2.4   1.9   1.8
> meta_df
      ID   Species
1    id3    setosa
2   id14    setosa
3   id15    setosa
4   id31    setosa
5   id42    setosa
6  id114 virginica
7  id125 virginica
8  id137 virginica
9  id143 virginica
10 id150 virginica

2- Define heatmap and Species grouping colors, column annotation (Species groups), column order and column split (gap between Species groups):

palette=grDevices::colorRampPalette(c("green","black","red"))(11)
col_vec=c("red","blue")
col_vec=stats::setNames(col_vec, levels(meta_df$Species))
column_ha <- ComplexHeatmap::HeatmapAnnotation(
               Species = meta_df$Species,
               col = list(Species = col_vec),
               show_legend = TRUE)
column_order=meta_df[order(meta_df$Species, decreasing=T),]$ID
column_split <- meta_df$Species

Note here (and this is the problem), that I want the virginica group on the left, and the setosa group on the right, so my column order is the following:

> column_order
 [1] "id114" "id125" "id137" "id143" "id150" "id3"   "id14"  "id15"  "id31"
[10] "id42"

3- Make the heatmap without column_split:

complex_heat <- ComplexHeatmap::Heatmap(heat_mat, cluster_rows = FALSE, cluster_columns = FALSE,
                                        row_dend_width = grid::unit(2, "inch"),
                                        rect_gp = grid::gpar(col = "white", lwd = 2),
                                        col = palette,
                                        top_annotation = column_ha,
                                        column_order = column_order,
                                        #column_split = column_split,
                                        column_gap = grid::unit(0.1, "inch"),
                                        border = TRUE)
grDevices::png(filename="heatmap.png", height=400, width=600)
ComplexHeatmap::draw(complex_heat)
grDevices::dev.off()

This is all good, the heatmap produced below has my sample IDs ordered correctly with virginica on the left (in blue as specified by col_vector), and setosa on the right (in red as specified by col_vector):

heatmap

4- Make the heatmap with column_split; if I just uncomment the column_split line that specifies to split the column by the Species variable, I woould expect the exact same heatmap, with just a gap between the two Species groups. However, the column_order is ignored, and setosa samples appear on the left...

complex_heat <- ComplexHeatmap::Heatmap(heat_mat, cluster_rows = FALSE, cluster_columns = FALSE,
                                        row_dend_width = grid::unit(2, "inch"),
                                        rect_gp = grid::gpar(col = "white", lwd = 2),
                                        col = palette,
                                        top_annotation = column_ha,
                                        column_order = column_order,
                                        column_split = column_split,
                                        column_gap = grid::unit(0.1, "inch"),
                                        border = TRUE)

heatmap

from complexheatmap.

dcarbajo avatar dcarbajo commented on June 23, 2024

Found the answer, column_split should be a factor with the same order as meta_df$Species, but the levels specified in the order we want them plotted...

The solution is to define column_split like this:

column_split=factor(as.character(meta_df$Species), levels=c('virginica','setosa'))

from complexheatmap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.