热图中每个变量有多个填充图例

时间:2018-09-12 11:30:34

标签: r ggplot2 legend heatmap

我有一个输入文件file1.txt

V1          V2      Score
rs4939134   SIFT    1
rs4939134   Polyphen2   0
rs4939134   MutationAssessor    -1.75
rs151252290 SIFT    0.101
rs151252290 Polyphen2   0.128
rs151252290 MutationAssessor    1.735
rs12364724  SIFT    0
rs12364724  Polyphen2   0.926
rs12364724  MutationAssessor    1.75
rs34448143  SIFT    0.005
rs34448143  Polyphen2   0.194
rs34448143  MutationAssessor    0.205
rs115694714 SIFT    0.007
rs115694714 Polyphen2   1
rs115694714 MutationAssessor    0.895

这是我的R代码,用于将此表绘制为热图:

library(ggplot2)

mydata <- read.table("file7.txt", header = FALSE, sep = "\t")
names(mydata) <- c("V1", "V2", "Score") 

ggplot(data = mydata, aes(x = V1, y = V2, fill = Score)) + 
  geom_tile() + 
  geom_text(aes(V1, V2, label = Score), color = "black", size = 3) + 
  scale_fill_continuous(type = "viridis", limits = c(-5.76, 5.37)) + 
  labs(x = "pic1", y = "") + 
  theme_bw()
  theme(panel.border = element_rect(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text = element_text(size = 4))

这就是我得到的情节:

enter image description here

我需要的是每一行(V2中的每种类型),我需要放置一个图例来表示,因此最后将有3个图例,每个图例都代表(一个用于SIFT,第二个用于Polyphen,第三个用于MutationAssessor ),但可以指定其他范围。

例如:SIFT从(0,1) 和(0,1)中的Polyphen 和(-6,6)的MutationAssessor

我尝试了先前提出的问题中的另一件事,但是对我没有任何帮助。

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您可以遍历三个给定的变量,并为每个变量绘制不同的图。最后,您必须将它们结合起来。

创建具有所需限制的数据集:

myLimits <- list(
    list("SIFT", 0, 1),
    list("Polyphen2", 0, 1),
    list("MutationAssessor", -6, 6)
)

一次仅绘制一个变量的热图的功能:

plotHeat <- function(type, MIN, MAX) {
    library(ggplot2)
    p <- ggplot(subset(mydata, V2 == type), 
                aes(V1, V2, fill = Score, label = Score)) + 
        geom_tile() + 
        geom_text(color = "black", size = 3) + 
        scale_fill_continuous(type = "viridis", limits = c(MIN, MAX)) + 
        labs(x    = "SNP", 
             y    = NULL,
             fill = type) + 
        theme_bw()
    # Output x-axis only for the last plot
    if (type != myLimits[[length(myLimits)]][[1]]) {
        p <- p + theme(axis.text.x = element_blank(),
                       axis.title.x = element_blank(),
                       axis.line.x = element_blank(),
                       axis.ticks.x = element_blank())
    }
    return(p)
} 

使用egg软件包绘制和合并图:

res <- lapply(myLimits, function(x) {plotHeat(x[[1]], x[[2]], x[[3]])})
egg::ggarrange(plots = res)

enter image description here

答案 1 :(得分:2)

这可能与this有关。

xs <- split(mydata, f = mydata$V2)

p1 <- ggplot(data = xs$MutationAssessor, aes(x = V1, y = 0, fill = Score)) + 
  geom_tile() + 
  geom_text(aes(label = Score), color = "black", size = 3) + 
  scale_fill_continuous(type = "viridis", limits = c(-5.76, 5.37)) + 
  labs(x = "pic1", y = "") + 
  facet_grid(V2 ~ .) + 
  theme_bw() + 
  theme(panel.border = element_rect(colour = "black"), 
        panel.grid.major = element_blank(),   
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text = element_text(size = 4))

p2 <- p1 %+% xs$Polyphen2
p3 <- p1 %+% xs$SIFT

library(gridExtra)
grid.arrange(p1, p2, p3)

结果是:

enter image description here

编辑:

如果您希望facets具有不同的范围,但希望值具有可比性(例如,在所有图中,5左右的值应为黄色),有可能的解决方法

首先离散化您的fill变量

mydata$colour <- cut(mydata$Score, 
                     quantile(mydata$Score, c(0, 0.25, 0.5, 0.75, 1)), 
                     include.lowest = T)

然后创建图:

xs <- split(mydata, f = mydata$V2)

p1 <- ggplot(data = xs$MutationAssessor, aes(x = V1, y = 0, fill = colour)) + 
  geom_tile() + 
  geom_text(aes(label = Score), color = "black", size = 3) + 
  labs(x = "pic1", y = "") + 
  facet_grid(V2 ~ .) + 
  theme_bw() + 
  theme(panel.border = element_rect(colour = "black"), 
        panel.grid.major = element_blank(),   
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text = element_text(size = 4))

p2 <- p1 %+% xs$Polyphen2
p3 <- p1 %+% xs$SIFT

最后更改调色板:

mypalette <- c("#FFFFCC", "#A1DAB4", "#41B6C4", "#2C7FB8", "#253494")
names(mypalette) <- levels(mydata$colour)

p1 <- p1 + scale_fill_manual(values = mypalette[levels(xs$MutationAssessor$colour)]) 
p2 <- p2 + scale_fill_manual(values = mypalette[levels(xs$Polyphen2$colour)]) 
p3 <- p3 + scale_fill_manual(values = mypalette[levels(xs$SIFT$colour)]) 

结果是:

grid.arrange(p1, p2, p3)

enter image description here