Generating split-color rectangles from ggplot2 geom_raster()

后端 未结 1 2010
死守一世寂寞
死守一世寂寞 2021-01-06 09:54

I\'ve been trying to generate a 2D rectangular plots using ggplots/reshape2 with code like this:

library(reshape2)
library(ggplot2)
m <- matrix( c(\'SNV\'         


        
相关标签:
1条回答
  • 2021-01-06 10:26

    This is a little more manual than I'd like, but it generalizes to more than two values within a cell. This also uses data.table, just because it makes the transformation of values in "X/Y/Z" format within a melted data frame blissfully easy:

    library(data.table)
    dt <- data.table(melt(m))
    dt <- dt[, strsplit(as.character(value), "/"), by=list(Var1, Var2)]  # this expands "X/Y/Z" into three rows
    dt[, shift:=(1:(.N))/.N - 1/(2 * .N) - 1/2, by=list(Var1, Var2)]
    dt[, height:=1/.N, by=list(Var1, Var2)]
    ggplot(dt, aes(Var1,y=Var2 + shift, fill=V1, height=height)) + 
      geom_tile(color="yellow", size=1) + xlab('Patient') + ylab('Gene')
    

    enter image description here

    Note I made your data set a little more interesting by adding one box with three values (and also that this is with geom_tile, not raster, which hopefully isn't a deal breaker).

    m <- structure(c("SNV", "SNV", NA, NA, "INDEL/POS/NEG", "SNV", "INDEL", 
                "SNV", "SNV/INDEL"), .Dim = c(3L, 3L))
    
    0 讨论(0)
提交回复
热议问题