从ggplot2生成分割颜色的矩形geom_raster() [英] Generating split-color rectangles from ggplot2 geom_raster()

查看:2081
本文介绍了从ggplot2生成分割颜色的矩形geom_raster()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(reshape2)使用ggplots / reshape2生成2D矩形图)
library(ggplot2)
m < - 矩阵(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')

请注意,如果具有SNV / INDEL的图块将其蓝色作为单独的类别进行着色, 。我只是想知道是否有办法让它实际上有一块分色瓷砖,这样瓷砖的颜色就变成了栗色/绿色(如一半的瓷砖是褐红色,另一半是绿色的)?



感谢,

解决方案

'喜欢,但它推广到一个单元格内的两个以上的值。这也使用 data.table ,仅仅是因为它使熔化数据框中的X / Y / Z格式的值变得非常容易:

$ (数据表)
dt < - data.table(melt(m))
dt < - dt [,strsplit(as.character(value),/),by = list(Var1,Var2)]#将X / Y / Z扩展为三行
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')


注意我让你的数据集更有趣一点添加一个包含三个值的盒子(还有 geom_tile ,而不是光栅,希望这不是一个交易断路器)。

  m < - 结构(c(SNV,S NV,NA,NA,INDEL / POS / NEG,SNV,INDEL,
SNV,SNV / INDEL),.Dim = c(3L,3L))


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

Notice how for the tile that has a SNV/INDEL it colors it blue as a separate category as it should. I was just wondering if there was a way to make it actually have a split-color tile such that the tile color is maroon/green (like half of the tile is maroon and the other half is green)?

Thanks,

解决方案

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

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

这篇关于从ggplot2生成分割颜色的矩形geom_raster()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆