如何在R中使用自定义文本创建交互式热图图? [英] How to create an interactive heatmaply plot with custom text in R?

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

问题描述

我有一个数据集,在其中绘制了一个热图以比较7个组.我还在数据2列中包含了一些信息,希望将这些信息作为悬停文本添加到交互式热图中.

I have a dataset where I am plotting a heatmap to compare 7 groups. I also have in the data 2 columns with information that I want to include as hover text in an interactive heat map.

我的数据是我要比较的7列组,以及要添加到绘图中的2列悬停文本信息.这些行是对数p值,我希望比较它们在组之间的重要性.

My data is 7 columns of groups I want to compare, and 2 columns of hover text information I want to add to my plot. The rows are log p-values that I am looking to compare the significance of between the groups.

当前,我正在尝试使用 heatmaply 进行绘制,但是我在设置自定义文本时遇到了麻烦-有一种方法可以将数据列设置为 custom_text heatmaply()?我找不到任何专门执行此操作的示例.

Currently I am trying to use heatmaply to plot this but I'm having trouble setting the custom text - is there a way to set columns of data into the custom_text of heatmaply()? I can't find any examples that do this specifically.

输入示例数据:

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), Gene_Overlap = c(37L, 
14L, 14L, 13L, 22L), Mean_GB_Score = c(0.798, 0.788, 0.81, 0.879, 
0.805)), row.names = c("Cardiac Hypertrophy", 
"Cellular Effects of Adrenaline", "Metastasis Signaling", 
"Hormone Signaling", "Estrogen Receptor Signaling"
), class = "data.frame")

我要使用的代码:

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

heatmaply(groups, custom_hovertext = df[[Gene_Overlap]], scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(
  low = "pink", 
  high = "red"))

推荐答案

custom_hovertext 参数的描述中,您可以看到它应该是与输入尺寸相同的矩阵,即矩阵5行7列.

In the description for custom_hovertext parameter you can read that it should be a matrix of the same dimensions as the input, i.e. a matrix with 5 rows and 7 columns.

所以首先我们需要构造这样的矩阵:

So first we would need to construct such matrix:

library(dplyr)

labels <- 
  df %>% 
  mutate(label = paste(
    "Gene Overlap:", Gene_Overlap,
    "\nMean_GB_Score:", Mean_GB_Score
  )) %>% 
  # this writes contens of the new `label` column
  # in place of all the Group columns
  transmute(across(Group1:Group7, ~label)) %>% 
  as.matrix()

然后我们可以在 heatmaply

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

这篇关于如何在R中使用自定义文本创建交互式热图图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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