[英]how to change geom_tile scale for very small values?
我有一個 dataframe 包含一些比較,該值表示對象之間的相似性。 我有一個真正的 object 與一些隨機的相比,這導致了非常小的相似性。 此外,我比較了隨機對象和隨機對象,這導致更高的相似率。 在這一點上,我想把所有的和 plot 放在一起作為熱圖。 問題是我想強調的非常小的相似性值與隨機隨機比較中不太小的相似性值具有相同的顏色。 當然這是一個比例問題,但我不知道如何管理色階。 以下代碼生成實際顯示問題的熱圖。 在這里,第一列是淡黃色的,這很好,但這與其他瓷磚的顏色相同,另一方面,它們具有更高的、不可比較的值。 如何根據實際比例為瓷磚着色?
編碼:
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)
以下是三種方法:
將geom_text
添加到 plot 以在色差很小時顯示值。
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 和小的非零數字之間的差異。 我讓刻度線的rest。
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("")
嘗試這些方法的組合,看看你喜歡什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.