0

我有一个包含一些比较的数据框,该值表示对象之间的相似性。与一些随机的对象相比,我有一个真实的对象,这导致了非常小的相似性。此外,我比较了随机对象和随机对象,这导致更高的相似率。在这一点上,我想将所有内容放在一起并将其绘制为热图。问题是我想强调的非常小的相似性值与随机随机比较中不太小的相似性值具有相同的颜色。当然这是一个比例问题,但我不知道如何管理色阶。以下代码生成实际显示问题的热图。在这里,第一列是淡黄色的,这很好,但这与其他瓷砖的颜色相同,另一方面,其他瓷砖具有更高的、不可比较的值。

编码:

set.seed(131)

#number of comparisons in the original data: 1 value versus n=10
n <- 10
#generate real data (very small values)
fakeRealData <- runif(n, min=0.00000000000001, max=0.00000000000002)

#and create the data structure
realD <- cbind.data.frame(rowS=rep("fakeRealData", n), colS=paste("rnd", seq(1, n, by=1), sep=" "), Similarity=fakeRealData, stringsAsFactors=F)

#the same for random data, n=10 random comparisons make for a n by n matrix
rndN <- n*n
randomData <- data.frame(matrix(runif(rndN), nrow=n, ncol=n))

rowS <- vector()
#for each column of randomData
for (r in seq(1, n, by=1)) {
    #create a vector of the first rowname, then the second, the third, etc etc which is long as the number of columns
    rowS <- append(rowS, rep(paste("rnd", r, sep=" "), n))
}

#and create the random data structure
randomPVs <- cbind.data.frame(rowS=rowS, colS=rep(paste("rnd", seq(1, n, by=1), sep=" "), n), Similarity=unlist(randomData), stringsAsFactors=F)

#eventually put everything together
everything <- rbind.data.frame(randomPVs, realD)

#and finally plot the heatmap
heaT <- ggplot(everything, aes(rowS, colS, fill=Similarity)) + 
    geom_tile() +
    scale_fill_distiller(palette = "YlGn", direction=2) +
    theme_bw() + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1))+
    xlab("")+
    ylab("")

plot(heaT)
4

1 回答 1

1

以下是三种方法:

添加geom_text到您的绘图中以显示颜色差异较小时的值。

heaT <- ggplot(everything, aes(rowS, colS)) + 
  geom_tile(aes(fill=Similarity)) +
  scale_fill_distiller(palette = "YlGn", direction=2) +
  geom_text(aes(label = round(Similarity, 2))) +
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  xlab("") +
  ylab("")

带有文本标签的热图

使用values参数将非线性比例设置为scale_fill_distiller我在 0.01 处添加了一个额外的断点,以突出 0 和小的非零数字之间的差异。我让其余的比例线性。

heaT <- ggplot(everything, aes(rowS, colS)) + 
  geom_tile(aes(fill=Similarity)) +
  scale_fill_distiller(palette = "YlGn", direction=2, 
                       values = c(0, 0.01, seq(0.05, 1, 0.05))) +
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  xlab("") +
  ylab("")

在此处输入图像描述

正如理查德在评论中提到的那样改变你的规模。请注意,这会与图例中的值混淆,因此请重命名或隐藏它。

heaT <- ggplot(everything, aes(rowS, colS)) + 
  geom_tile(aes(fill=Similarity)) +
  scale_fill_distiller(palette = "YlGn", direction=2, trans = "log10", 
                       name = "log10(Similarity)") +

  theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  xlab("")+
  ylab("")

在此处输入图像描述

尝试这些方法的组合,看看你喜欢什么。

于 2020-08-06T11:56:00.987 回答