R语言 如何制作基因本体术语的热图

d4so4syb  于 2024-01-03  发布在  其他
关注(0)|答案(1)|浏览(113)

我尝试用两种基因型(“rsh 3”和“iron”)差异表达基因的基因本体的-log(FDR)来做热图。在这两列中,“rsh 3”和“iron”写的是GO术语的-log(FDR)。
我不明白是我写错了代码还是我在框架中错误地显示了我的数据。
以防我没说清楚,这是我的框架的样子:
x1c 0d1x的数据
我试了这个代码:

library(ggplot2)

heat <- read_excel("directory/heat_final.xlsx")

heat_matrix <- as.matrix(heat)

ggplot(heat, aes(x = factor(c("rsh3", "iron")), y = GO_term)) + 
  geom_tile()

字符串
我得到这个错误:

> ggplot(heat, aes(x = factor(c("rsh3", "iron")), y = GO_term)) + 
+   geom_tile()
Error in `geom_tile()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (288)
✖ Fix the following mappings: `x`
Run `rlang::last_trace()` to see where the error occurred.


实际上,我正在尝试获得一个热图,看起来像本文的图3:


https://www.tandfonline.com/doi/full/10.1080/21580103.2020.1801524

gblwokeq

gblwokeq1#

您的主要问题是在ggplot(aes())中,x应该是列名称,其中列包含基因型,而不是包含列名称的因子。
一个附带说明是,ggplot()期望的数据是data.frame,而不是矩阵,所以你的转换充其量是无用的(将立即恢复),或者可能使事情变得更糟。
所以,你需要的是一个数据.frame,它有一个列GO_term,你可以Map到y,还有一个列genotype,你可以Map到x。当然,还有一个列,你可以Map到fill颜色,我们叫它log_FDR
这意味着您需要将对应于基因型的列合并为一列,以使数据为tidy,例如pivot_longer()
因此,使用虚拟数据来说明,这里是一个可重复的示例:

library(tidyverse)

# dummy data
heat <- tribble(~GO_term, ~rsh3, ~iron, ~mutant2,
                "GO1", 1.81, 1.86, 2.5,
                "GO2", 3.2, 0, 1)

heat |>
  select(GO_term, rsh3, iron) |>
  pivot_longer(-GO_term,
               names_to = "genotype",
               values_to = "log_FDR") |>
  ggplot(aes(x = genotype, y = GO_term, fill = log_FDR)) + 
  geom_tile()

字符串

相关问题