I have mtx which has a range of values from 0 to 1 across 23 clusters which was generated using CellChat and I am trying to get the specific colors used in the output plots generated to use for other ggplots (by looking through CellChat's source code). I was able to get a matrix mtx with the values used from 0 to 1 for the ComplexHeatmap, and the color palette vector mypal. I would like to use these colors to plot other forms of this data, so I am wondering how I can get the exact hex codes that are used in the heatmap below for each cluster in mtx.
For example usage, how could I get these and use them in the ggplot below. Cluster 3 sender points would be dark green, but cluster 3 receivers are very light green (almost white). I am imagining an output where mtx values are replaced with hex codes. Thanks!
Data to reproduce at the end of the post. Thank you!
> mtx
clust_0 clust_1 clust_10 clust_11 clust_12 clust_13 clust_14 clust_15 clust_16 clust_17 clust_18 clust_19
Sender 0.3259701 0.2512304 0.2284519 0.3928275 0.4212690 0.1673189 0.4791000 0.7585386 0.4107654 0.2954138 0.03466265 0.4660074
Receiver 0.4317020 0.3413282 0.5559802 0.6140530 0.5890955 0.2941485 0.5154768 0.6074988 0.5415722 1.0000000 0.05423820 0.8127188
clust_2 clust_20 clust_21 clust_22 clust_3 clust_4 clust_5 clust_6 clust_7 clust_8 clust_9
Sender 0.4279163 0.4530084 0.9663671 0.5268797 1.0000000 0.2415731 0.2161009 0.1340106 0.1853619 0.8772612 0.2442095
Receiver 0.9196526 0.5413774 0.7327871 0.2009977 0.1535685 0.3520444 0.1545579 0.1640971 0.2488437 0.5501753 0.2748191
library(ComplexHeatmap)
Heatmap(mtx,
col = mypal)
library(tidyverse)
ggplot(data = df) +
geom_point(aes(x = cluster,
y = dat,
shape = sr))
mtx <- structure(c(0.325970079655735, 0.431702048898435, 0.251230373147469,
0.341328240221655, 0.228451906028636, 0.555980211092138, 0.392827544185538,
0.614052984578806, 0.421269002468793, 0.589095489397628, 0.167318851079854,
0.294148473400288, 0.479099971285772, 0.515476772660859, 0.758538606541378,
0.607498767394721, 0.410765389817039, 0.541572165527801, 0.295413848264102,
1, 0.0346626472640317, 0.0542381951611456, 0.46600740104779,
0.81271883627567, 0.427916324581243, 0.919652561477921, 0.453008397720977,
0.541377433274018, 0.966367059529975, 0.732787053769931, 0.526879715034283,
0.200997694838518, 1, 0.15356849682794, 0.241573106453975, 0.352044385460005,
0.216100882019013, 0.154557887600858, 0.134010605978614, 0.164097130100271,
0.185361902238227, 0.248843717726162, 0.877261240877266, 0.550175346935046,
0.24420949265889, 0.274819081034169), dim = c(2L, 23L), dimnames = list(
c("Sender", "Receiver"), c("clust_0", "clust_1", "clust_10",
"clust_11", "clust_12", "clust_13", "clust_14", "clust_15",
"clust_16", "clust_17", "clust_18", "clust_19", "clust_2",
"clust_20", "clust_21", "clust_22", "clust_3", "clust_4",
"clust_5", "clust_6", "clust_7", "clust_8", "clust_9")))
mypal <- c("#F7FCFD", "#F5FBFC", "#F4FAFC", "#F2FAFC", "#F1F9FB", "#EFF9FB",
"#EEF8FB", "#ECF8FA", "#EBF7FA", "#E9F6FA", "#E8F6F9", "#E7F5F9",
"#E5F5F9", "#E3F4F8", "#E1F3F6", "#DFF3F4", "#DDF2F3", "#DBF1F1",
"#D9F0F0", "#D7F0EE", "#D5EFED", "#D3EEEB", "#D1EEEA", "#CFEDE8",
"#CDECE7", "#CAEBE5", "#C6E9E3", "#C2E8E0", "#BEE6DE", "#BAE5DC",
"#B6E3D9", "#B2E1D7", "#AEE0D5", "#AADED2", "#A5DDD0", "#A1DBCD",
"#9DD9CB", "#99D8C9", "#95D6C6", "#91D4C3", "#8DD2C0", "#89D1BD",
"#84CFBA", "#80CDB7", "#7CCBB4", "#78CAB1", "#74C8AE", "#70C6AB",
"#6CC4A8", "#68C2A5", "#64C1A2", "#61BF9E", "#5EBD9A", "#5BBC96",
"#58BA93", "#55B98F", "#52B78B", "#4FB588", "#4CB484", "#49B280",
"#46B17C", "#43AF79", "#40AD75", "#3EAA71", "#3BA76D", "#39A569",
"#36A265", "#349F61", "#329C5D", "#2F9959", "#2D9755", "#2A9451",
"#28914D", "#268E49", "#238B45", "#208943", "#1E8641", "#1B843F",
"#18813D", "#157F3B", "#127D39", "#0F7A37", "#0D7835", "#0A7533",
"#077331", "#04702F", "#016E2D", "#006B2B", "#00682A", "#006528",
"#006127", "#005E25", "#005B24", "#005723", "#005421", "#005120",
"#004D1F", "#004A1D", "#00471C", "#00441B")
set.seed(123)
df <- data.frame(cluster = rep(c("clust_0", "clust_1", "clust_10",
"clust_11", "clust_12", "clust_13", "clust_14", "clust_15",
"clust_16", "clust_17", "clust_18", "clust_19", "clust_2",
"clust_20", "clust_21", "clust_22", "clust_3", "clust_4",
"clust_5", "clust_6", "clust_7", "clust_8", "clust_9"), 4),
dat = rnorm(92),
sr = sample(rep(c("Sender", "Receiver"), 46)))
ComplexHeatmap::Heatmap pulls 3 levels c(0, 0.5, 1) and assigns three colors from your platte c("#F7FCFDFF", "#6DC5A9FF", "#00441BFF") to them and interpolates the inbetween.
to get to this color mapping, you can do
library(ComplexHeatmap)
hm <- Heatmap(mtx, col = mypal)
color_fun <- circlize::colorRamp2(breaks = hm@matrix_color_mapping@levels, colors = hm@matrix_color_mapping@colors)
# I used `t()` here to bring your matrix into a longer shape but you can ommit it
mtx_hex <- t(apply(mtx, 2, function(x) color_fun(x))) |> data.frame()
giving
Sender Receiver
clust_0 #A0D8C6FF #82CDB4FF
clust_1 #B4E1D2FF #9BD7C3FF
clust_10 #BAE3D6FF #61B698FF
clust_11 #8DD1BAFF #55A686FF
clust_12 #85CEB6FF #5AAC8DFF
clust_13 #CBEAE0FF #A8DCCBFF
clust_14 #73C7ACFF #6AC1A4FF
clust_15 #37805CFF #56A788FF
clust_16 #88CFB8FF #64B99CFF
clust_17 #A8DCCBFF #00441BFF
clust_18 #EEF8F7FF #E9F6F4FF
clust_19 #77C9AFFF #2C724CFF
clust_2 #83CDB5FF #155730FF
clust_20 #7BCAB1FF #64BA9CFF
clust_21 #084C24FF #3D8663FF
clust_22 #67BEA1FF #C2E6DBFF
clust_3 #00441BFF #CEEBE3FF
clust_4 #B7E2D4FF #98D6C1FF
clust_5 #BEE5D8FF #CEEBE2FF
clust_6 #D3EEE6FF #CBEAE1FF
clust_7 #C6E8DDFF #B5E1D2FF
clust_8 #1E623BFF #62B799FF
clust_9 #B6E1D3FF #AEDECEFF
you can verify that the colors are correct, by plotting them with ggplot2
library(ggplot2)
mtx_hex$cluster <- rownames(mtx_hex)
mtx_hex <- mtx_hex |>
tidyr::pivot_longer(!cluster, names_to = "type", values_to = "color")
# reorder factor
mtx_hex$cluster <- factor(mtx_hex$cluster, levels =c("clust_21", "clust_8", "clust_15", "clust_3", "clust_2", "clust_19", "clust_17", "clust_12", "clust_11", "clust_14", "clust_20", "clust_16", "clust_10", "clust_0", "clust_22", "clust_4", "clust_1", "clust_9", "clust_13", "clust_7","clust_5","clust_6","clust_18"))
ggplot(data = mtx_hex) +
geom_point(aes(x = cluster,
y = forcats::fct_rev(type),
color = color), size = 20) +
scale_color_identity()