簡體   English   中英

R:水平圖中將red_to_blue中央調色板設置為0

[英]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))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM