R:水平图中 0 处的中心 red_to_blue 调色板
R: center red_to_blue color palette at 0 in levelplot
我正在制作一个 levelplot
,其中我的数据框的一个变量用于为单元格 (fold.change) 着色,另一个 (map.signif) 写在顶部。在这种情况下,我为重要的单元格写 * 和 **。
这是我的 MWE:
set.seed(150)
pv.df <- data.frame(compound=rep(LETTERS[1:8], each=3), comparison=rep(c("a/b","b/c","a/c"), 8), p.value=runif(24, 0, 0.2), fold.change=runif(24, -0.3, 0.9))
pv.df$map.signif <- ifelse(pv.df$p.value > 0.05, "", ifelse(pv.df$p.value > 0.01,"*", "**"))
pv.df
myPanel <- function(x, y, z, ...) {
panel.levelplot(x, y, z, ...)
panel.text(x, y, pv.df$map.signif, cex=3)
}
#install.packages("latticeExtra")
library(latticeExtra)
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(11, "RdBu"))(11)
png(filename="test.png", height=800, width=400)
print(
levelplot(fold.change ~ comparison*compound, #p.value instead of p.adjust depending on map.signif
pv.df,
panel = myPanel,
col.regions = cols,
at = do.breaks(range(pv.df$fold.change), 11),
colorkey = list(col = cols,
at = do.breaks(range(pv.df$fold.change), 11)),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Test\nfold change color\n*pv<0.05\t**pv<0.01",
cex = 1.5))
)
dev.off()
产生:
我的问题是:由于 fold.change 包括负值和正值,我如何使 0 与我的调色板中的白色一致,使负值显示为红色,正值显示为蓝色?
为了获胜,是否可以在单元格背景清晰时将*写成黑色,在背景较暗时将*写成白色?非常感谢!
你的范围不是对称的。另一种选择是:
max_abs <- max(abs(pv.df$fold.change))
brk <- do.breaks(c(-max_abs, max_abs), 11)
levelplot(fold.change ~ comparison*compound, #p.value instead of p.adjust depending on map.signif
pv.df,
panel = myPanel,
col.regions = cols,
at = brk,
colorkey = list(col = cols,
at = brk),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Test\nfold change color\n*pv<0.05\t**pv<0.01",
cex = 1.5))
编辑
如果您不想要额外的休息时间:
max_abs <- max(abs(pv.df$fold.change))
brk <- do.breaks(c(-max_abs, max_abs), 11)
first_true <- which.max(brk > min(pv.df$fold.change))
brk <- brk[(first_true -1):length(brk)]
cols <- cols[(first_true -1):length(cols)]
levelplot(fold.change ~ comparison*compound, #p.value instead of p.adjust depending on map.signif
pv.df,
panel = myPanel,
col.regions = cols,
at = brk,
colorkey = list(col = cols,
at = brk),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Test\nfold change color\n*pv<0.05\t**pv<0.01",
cex = 1.5))
我正在制作一个 levelplot
,其中我的数据框的一个变量用于为单元格 (fold.change) 着色,另一个 (map.signif) 写在顶部。在这种情况下,我为重要的单元格写 * 和 **。
这是我的 MWE:
set.seed(150)
pv.df <- data.frame(compound=rep(LETTERS[1:8], each=3), comparison=rep(c("a/b","b/c","a/c"), 8), p.value=runif(24, 0, 0.2), fold.change=runif(24, -0.3, 0.9))
pv.df$map.signif <- ifelse(pv.df$p.value > 0.05, "", ifelse(pv.df$p.value > 0.01,"*", "**"))
pv.df
myPanel <- function(x, y, z, ...) {
panel.levelplot(x, y, z, ...)
panel.text(x, y, pv.df$map.signif, cex=3)
}
#install.packages("latticeExtra")
library(latticeExtra)
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(11, "RdBu"))(11)
png(filename="test.png", height=800, width=400)
print(
levelplot(fold.change ~ comparison*compound, #p.value instead of p.adjust depending on map.signif
pv.df,
panel = myPanel,
col.regions = cols,
at = do.breaks(range(pv.df$fold.change), 11),
colorkey = list(col = cols,
at = do.breaks(range(pv.df$fold.change), 11)),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Test\nfold change color\n*pv<0.05\t**pv<0.01",
cex = 1.5))
)
dev.off()
产生:
我的问题是:由于 fold.change 包括负值和正值,我如何使 0 与我的调色板中的白色一致,使负值显示为红色,正值显示为蓝色?
为了获胜,是否可以在单元格背景清晰时将*写成黑色,在背景较暗时将*写成白色?非常感谢!
你的范围不是对称的。另一种选择是:
max_abs <- max(abs(pv.df$fold.change))
brk <- do.breaks(c(-max_abs, max_abs), 11)
levelplot(fold.change ~ comparison*compound, #p.value instead of p.adjust depending on map.signif
pv.df,
panel = myPanel,
col.regions = cols,
at = brk,
colorkey = list(col = cols,
at = brk),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Test\nfold change color\n*pv<0.05\t**pv<0.01",
cex = 1.5))
编辑
如果您不想要额外的休息时间:
max_abs <- max(abs(pv.df$fold.change))
brk <- do.breaks(c(-max_abs, max_abs), 11)
first_true <- which.max(brk > min(pv.df$fold.change))
brk <- brk[(first_true -1):length(brk)]
cols <- cols[(first_true -1):length(cols)]
levelplot(fold.change ~ comparison*compound, #p.value instead of p.adjust depending on map.signif
pv.df,
panel = myPanel,
col.regions = cols,
at = brk,
colorkey = list(col = cols,
at = brk),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Test\nfold change color\n*pv<0.05\t**pv<0.01",
cex = 1.5))