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

3

我有一个输入文件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), Polyphen 范围为 (0,1), MutationAssessor 范围为 (-6,6)。

我尝试了之前提出的问题的不同方法,但都没有成功。

感谢任何帮助。


我不知道在热力图中是否可以为超过2个变量创建图例。简单的解决方法就是创建三个不同的图表。 - pogibas
不,我需要将它们作为一个图来比较,这比将它们分成不同的图更好。@PoGibas - LamaMo
2个回答

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


哇,我喜欢它:))), 非常感谢3个图表,但它们看起来像一个,再次感谢。 - LamaMo
@SaraWasl 很高兴能帮忙 :-) - pogibas

2

This is maybe related to 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


但是,正如我在问题中提到的那样,我希望每种类型的图例值范围不同。SIFT 范围为 (0,1),Polyphen 范围为 (0,1),MutationAssessor 范围为 (-6,6)。 - LamaMo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接