[英]Setting up custom color scale in R
我正在嘗試在R中設置自定義比例。我的數據范圍從-5.4到+3.6,我想將數據居中於0(白色)。 我希望對數據進行縮放,使得我在0以上和以下具有相同數量的漸變(我現在正在拍攝7個)。 我遇到的問題是我無法正確縮放,我不確定我的問題在哪里。
我的代碼(源數據在底部的Pastebin鏈接中):
png('127-2_4_compare_other.png',width = 1200, height = 800, units = "px")
colfunc <- colorRampPalette(c("blue", "white", "red"))
f <- function(m) t(m)[,nrow(m):1]
colorBarz=matrix(seq(-5.5,4,len=15),nrow=1)
colorBarx=1
source("127-2_4.CompareMatrix.txt")
colorBary=seq(-5.4,3.6,len=15)
cus_breaks=c(-5.400, -4.725, -4.050, -3.375, -2.700, -2.025, -1.350, -0.675, 0.45, 0.90, 1.35, 1.80, 2.25, 2.70, 3.15, 3.60)
layout(matrix(c(1,2), 1, 2, byrow = TRUE), widths=c(9,1))
image(f(Compare2and4),axes=FALSE,ylab="Amino acids",xlab="Position",main="Sample 2 vs. 4",col=colfunc(15),breaks=cus_breaks)
axis(1, seq(from = 0, to = 1, by = 0.03703), labels=c(1:11,1:17))
axis(2, seq(from = 0, to = 1, by = 0.0526),labels=rev(c("A","R","N","D","C","E","Q","G","H","I","L","K","M","F","P","S","T","W","Y","V")),las=2)
image(colorBarx,colorBary,colorBarz,col=colfunc(15),axes=FALSE,xlab="",ylab="log(Sample4 / Sample2)",breaks=cus_breaks)
axis(2,las=2)
dev.off()
我正在尋找0到3.6之間的7個均勻分割的箱子和0到-5.4以下的7個均勻分開的箱子,我想要在白色箱子的中間點擊0。 此外,如果任何人都可以查看熱圖代碼本身,以確保沒有明顯的錯誤,我會非常感激。 源數據的Pastebin
您提出的色標的一個問題是,刻度上每個點之間的感知距離不相等。 與負端相比,相同的顏色距離是指正端的更大范圍的數據值。 我建議不要重新發明輪子,並利用ggplot
的強大功能。
默認情況下,發散色標將為白色,中間值為0。
例如:
Compare2and4_t <- t(Compare2and4)[,nrow(Compare2and4):1]
am_ac <- c("A","R","N","D","C","E","Q","G","H","I","L","K","M","F","P","S","T","W","Y","V")
pdat <- data.frame(x = 1:nrow(Compare2and4_t),
y = factor(rep(am_ac, each = nrow(Compare2and4_t)), levels = rev(am_ac)),
val = c(Compare2and4_t))
library(ggplot2)
library(scales)
ggplot(pdat, aes(x, y, fill = val)) +
geom_tile() +
scale_fill_gradient2(low = 'darkblue', high = 'darkred',
limits = c(-5.4, 3.6), oob = squish) +
coord_equal(expand = FALSE) +
scale_x_continuous(breaks = unique(pdat$x)) +
theme_classic() +
labs(x = 'Position', y = 'Amino acids')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.