2014-06-11 67 views
1

我有这样的数据框(如下图所示),我很感兴趣,有色细胞着色细胞Data.Frame [R

sample<- data.frame(c("k","k","k","k"), c("s","t","s","s"), c("t","n","t","t"), 
        c("c","c","t","c"), c("n","c","c","c")) 
rownames(sample)<- c("g1", "g2", "g3", "g4") 
colnames(sample)<- c(10, 13, 20, 21, 25)  

密谋AA表像图我已经发现了一些以前在这里回答问题(如:Conditional coloring of cells in table)并尝试了那里给出的建议。下面是我跑的代码示例:

#run ggplot2 suggestion by Drew Steen 
sample$gene<- row.names(sample) 
dfm<- melt(sample, id.vars="gene") 
p <- ggplot(dfm, aes(x=variable, y=gene, label=value, fill=as.factor(value))) + 
    geom_text(colour="black") + 
    geom_tile(alpha=0.5) 
p  

但是,这不完全是我正在寻找的配色方案。我需要下面描述的情节遵循另一个数据框提供的准则:

data<- data.frame(c("K", "s", "t", "c", "c"), c(10, 13, 20, 21, 25)) 
colnames(data)<- c("type", "position")  

因此,例如,在sample$"13",我需要所有的“S”,以显示为一种颜色,以及所有其他值是不是“ s“显示为不同的颜色。根据data提供的指导方针,我需要在sample的所有列上完成此操作。

回答

1

如何在dfm中添加新列以指示特定变量/值组合是否在数据中?

dfm$ismatch<-ifelse(
    with(dfm,interaction(variable, value)) %in% 
     with(data, interaction(position,type)), 
    "match","nomatch") 

然后我们就可以颜色基于该值

ggplot(dfm, aes(x=variable, y=gene, label=value, fill=ismatch)) + 
    geom_text(colour="black") + 
    geom_tile(alpha=0.5) 

这给了

resulting plot

+0

非常感谢,这正是我要找的... –

+0

道歉,在分析数据之后,似乎k10即使是匹配也会给出“不匹配”......有没有办法解决这个错误? –

+0

无视以前的评论,数据中的“K”是大写字母 –