Comments (2)
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):
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)
from complexheatmap.
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)
- Question about setting color function
- Conditional coloring of heatmap
- Asterisks to indicate significant associations
- picking new set of random colors for annotations
- Anno_textbox not scaling when saving image
- ComplexHeatmap crashing R session HOT 1
- Change pch size and color in legend
- Not getting correct row order in the row split ComplexHeatmap R HOT 1
- Help with anno_boxplot
- Making a heatmap with categorical value such as CNV(AMP, DEL) status
- Cluster column slices but not columns within slices
- Feature request: integrate ` heatmap_legend_side` into ` heatmap_legend_param`?
- Oncoprint - Customize with continuous color scale
- Color Branches w/ K-Means
- Padding not working with heatmap list HOT 1
- ComplexHeatmap with specific rownames
- How to use layer_fun to add a box in the aim areas
- Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
- Issue with the anno_numeric function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from complexheatmap.