我正在尝试在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 of the source data
答案 0 :(得分:1)
您提出的色标的一个问题是标尺上每个点之间的感知距离不相等。与负端相比,相同的颜色距离是指正端的更大范围的数据值。我建议不要重新发明轮子,并利用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')