How to plot a heat map by chromosome in R

岁酱吖の 提交于 2021-01-29 06:00:34

问题


I have a data table in the following format:

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))
)

And I am trying to plot a figure similar (but not identical) to this:

I have tried using ggplot and geom_rect(), but with no luck.

Any help will be greatly appreciated.


回答1:


You could try the plotly package, here's a start:

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)))



来源:https://stackoverflow.com/questions/55893099/how-to-plot-a-heat-map-by-chromosome-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!