如何在R中的热图的每一列添加自定义文本? [英] How to add custom text per column of a heatmap in R?

查看:66
本文介绍了如何在R中的热图的每一列添加自定义文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,在其中绘制了一个热图以比较7个组.我还每组有2列描述该组的数据.我正在尝试创建一个交互式图表,以按其信息列显示每个组的信息.

I have a dataset where I am plotting a heatmap to compare 7 groups. I also have per group 2 columns of data that describe the group. I am trying to create an interactive plot that shows each group's information per its information columns.

这里是一个数据示例,其中7个组每个都有2列相应的信息:

Here is an example of the data where 7 groups each have 2 columns of corresponding information:

df <- structure(list(Group1 = c(9.420318259, 5.801092847, 4.890727291, 
4.589825753, 4.836092781), Group2 = c(14.57805564, 8.798453748, 
7.982599836, 7.951599435, 10.81418654), Group3 = c(14.49131554, 
7.975284646, 8.258878348, 7.922657108, 13.3205827), Group4 = c(11.44447147, 
6.208332721, 6.529806574, 4.882623805, 10.69676399), Group5 = c(22.86835197, 
10.94297858, 7.197041788, 9.237584441, 12.70083108), Group6 = c(10.62687539, 
6.458410247, 7.461916094, 6.308454021, 12.39464562), Group7 = c(11.09404106, 
6.420303272, 6.821000583, 5.0727153, 11.13903127), Group1_Genes = c(46L, 
17L, 23L, 16L, 27L), Group1_Score = c(0.719, 0.757, 0.71, 0.807, 
0.761), Group2_Genes = c(58L, 22L, 30L, 22L, 40L), Group2_Score = c(0.754, 
0.766, 0.741, 0.807, 0.774), Group3_Genes = c(37L, 14L, 14L, 
13L, 22L), Group3_Score = c(0.798, 0.788, 0.81, 0.879, 0.805), 
    Group4_Genes = c(55L, 20L, 29L, 21L, 42L), Group4_Score = c(0.774, 
    0.768, 0.741, 0.822, 0.781), Group5_Genes = c(71L, 24L, 37L, 
    23L, 53L), Group5_Score = c(0.766, 0.767, 0.765, 0.811, 0.771
    ), Group6_Genes = c(69L, 24L, 37L, 23L, 53L), Group6_Score = c(0.772, 
    0.767, 0.765, 0.811, 0.771), Group7_Genes = c(58L, 21L, 33L, 
    22L, 48L), Group7_Score = c(0.79, 0.78, 0.774, 0.817, 0.78
    )), row.names = c("Cardiac Hypertrophy", 
"Cellular Effects of Adrenaline", "Metastasis Signaling", 
"Hormone Signaling", "Estrogen Receptor Signaling"
), class = "data.frame")

#One row of this data looks like:
Pathway  Group1  Group2  Group3  Group4  Group5  Group6  Group7  Group1_Score  Group1_Genes  Group2_Score  Group2_Genes ...
Cardiac  0.7      0.8      0.5    0.7      0.3    0.6     0.6        0.6           34           0.4     65

我正在尝试在热图中绘制第1-7组(也为列1:7),然后通过修改其他问题的答案来使用其余列的悬停文本(

I am trying to plot groups1-7 (also columns 1:7) in a heatmap then have hover text using the rest columns by modifying an answer in another question (How to create an interactive heatmaply plot with custom text in R?) in a heatmap with:

groups <- as.matrix(df[,1:7]) 

labels1 <- 
  df  %>% 
  mutate(label1 = paste(
    "Gene Overlap:", Group1_Genes,
    "\nMean_GB_Score:", Group1_Score
  )) %>% 

  transmute(across(Group1, ~label1)) %>% 
  as.matrix()

labels2 <- 
  df  %>% 
  mutate(label2 = paste(
    "Gene Overlap:", Group2_Genes,
    "\nMean_GB_Score:", Group2_Score
  )) %>% 

  transmute(across(Group2, ~label2)) %>% 
  as.matrix()


#I repeat making 7 labels objects to then cbind:

labels = cbind(labels1, labels2, labels3, labels4, labels5, labels6, labels7)

heatmaply(groups, custom_hovertext = labels,
          file = "heatmaply_plot.html",
          scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(
  low = "pink", 
  high = "red"))

但是尝试这样做会产生错误:

However trying this produces an error:

Error in custom_hovertext[rowInd, colInd, drop = FALSE] : 
  subscript out of bounds

我是否可以在 heatmaply()中创建 custom_text ,该方法指定在热图的每一列上提供的悬停文本信息,而不是在以下位置提供全局信息:每个热图正方形?

Is there a way for me to create custom_text in heatmaply() that specifies hover text information given per each column of the heatmap, as opposed to global information given to each heatmap square?

推荐答案

labels_df <- 
  df %>% 
  select(ends_with("Score"), ends_with("Genes")) %>% 
  rownames_to_column() %>% 
  pivot_longer(-rowname) %>% 
  separate(name, c("Group", "var")) %>% 
  pivot_wider(c(rowname, Group), names_from = var, values_from = value) %>% 
  mutate(label = paste(
    "Gene Overlap:", Genes,
    "\nMean_GB_Score:", Score
  )) %>% 
  pivot_wider(rowname, names_from = Group, values_from = label)

您可以通过在任何地方断开链并运行代码来检查每一步会发生什么.但基本上,我们只是进行一些转置,以使数据具有更有用的整洁格式,从而无需计算7个相似的表达式即可计算标签.然后,我们转置为 heatmaply 所需的格式.

You can check out what happens at each step by breaking the chain at any place and running the code. But basically we are just making some transposes to have the data in a more usable tidy format such that to calculate label we don't need to type in 7 similar expressions. And then we transpose back to the format needed for heatmaply.

重要的是,在所有这些转置之后,这些行恰好与开始时的顺序相同.这很酷,但是最好检查一下.

Important thing to mention here is that after all these transposes the rows happen to be in the same order as they were at the beginning. This is cool, but it's better to check such things.

矩阵形式的标签:

labels_mat <- 
  labels_df %>% 
  select(Group1:Group7) %>% 
  as.matrix()

最后:

heatmaply(
  groups,
  custom_hovertext = labels_mat,
  scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(low = "pink", high = "red")
)

这篇关于如何在R中的热图的每一列添加自定义文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆