Create color rectangles from ggplot2 geom_raster ()

I am trying to create 2D rectangular graphs using ggplots / reshape2 with this code:

library(reshape2)
library(ggplot2)
m <- matrix( c('SNV', 'SNV', NA, NA, 'INDEL', 'SNV', 'INDEL', 'SNV', 'SNV/INDEL'), 3, 3 )
ggplot(melt(m), aes(Var1,Var2, fill=value)) + geom_raster() + xlab('Patient') + ylab('Gene')

Please note that for tiles with SNV / INDEL, it colors it blue as a separate category. I'm just wondering if there is a way to make it really a tile with a color color, so that the color of the tile is maroon / green (for example, half of the tile is maroon and the other half is green)?

Thanks,

+4
source share
1 answer

, , . data.table, , "X/Y/Z" :

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

. , ( , geom_tile, , , , ).

m <- structure(c("SNV", "SNV", NA, NA, "INDEL/POS/NEG", "SNV", "INDEL", 
            "SNV", "SNV/INDEL"), .Dim = c(3L, 3L))
+7

Source: https://habr.com/ru/post/1529497/


All Articles