Data.Frame R的着色单元格

时间:2014-06-11 22:00:11

标签: r graph plot ggplot2 dataframe

我有这个数据框(如下所示),我有兴趣绘制一个像彩色单元格的图表

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)    

我在这里找到了一些以前回答的问题(例如: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”的值显示为不同的颜色。我需要根据sample提供的指南在data的所有列上完成此操作。

1 个答案:

答案 0 :(得分: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