如何在 R 中按染色体绘制热图

How to plot a heat map by chromosome in R

我有一个数据table,格式如下:

set.seed(1)

dt <- data.table(
chromosome = c(rep(1, 100), 
     rep(2, 100), 
     rep(3, 80)),
mb_from = c(seq(1, 1000, by=10),
      seq(1, 1000, by=10),
      seq(1, 800, by=10)),
mb_to = c(seq(10, 1000, by=10),
    seq(10, 1000, by=10),
    seq(10, 800, by=10)),
score = c(sample(1:10, 100, replace = T),
       sample(1:10, 100, replace = T),
       sample(1:10, 80, replace = T))
)

我正在尝试绘制一个与此相似(但不相同)的图形:

我尝试过使用 ggplot 和 geom_rect(),但没有成功。

任何帮助将不胜感激。

您可以尝试 plotly 包,这是一个开始:

library(dplyr)
library(plotly)
library(RColorBrewer)

dat <- apply(dt,
      1,
      function(x) data.table(chromosome = x["chromosome"], mb = x["mb_from"]:x["mb_to"], score = x["score"])
) %>%
  rbindlist()

plot_ly(dat, x = ~chromosome, y = ~mb, z = ~score, type = "heatmap",
        colors = "RdYlBu", reversescale = T) %>%
  layout(yaxis = list(range = c(1000, 0)))