R levelplot:根据一个变量的颜色green-white-red(白色为0),但显示另一个变量的值
R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
标题差不多self-descriptive。我想用格子做一个 heatmap-like 图,也显示数据值,比如 here
然而,在我的例子中,我想根据一个变量 (fold.change) 为绘图着色,但显示另一个变量 (p.value) 的值。
颜色范围最好是 green-white-red,白色为 0(绿色为负 fold.change 值,红色为正值)。
我的最后一个问题是如何更改标题和坐标轴文本的文字大小、删除坐标轴标题以及将x 轴文本旋转45 度;我在文档中找不到此信息。谢谢!
到目前为止,这是我的 MWE:
library(lattice)
library(latticeExtra)
library(RColorBrewer)
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, 1),
fold.change = runif(24, -2, 6))
myPanel <- function(x, y, z, ...) {
panel.levelplot(x,y,z,...)
panel.text(x, y, round(z,1))
}
cols <- rev(colorRampPalette(brewer.pal(6, "RdYlGn"))(20))
png(filename = "test.png", height = 1000, width = 600)
print(
levelplot(fold.change ~ comparison*compound,
pv.df,
panel = myPanel,
col.regions = cols,
colorkey = list(col = cols,
at = do.breaks(range(pv.df$fold.change), 20)),
scales = list(x = list(rot = 90)),
main = "Total FAME abundance - TREATMENT",
type = "g")
)
dev.off()
产生这个情节的是:
谢谢!
你的问题有几个部分。让我们一一解决:
1: 更改标签。这可以通过改变 panel.text()
:
的第三个参数来完成
myPanel <- function(x, y, z, ...) {
panel.levelplot(x, y, z, ...)
panel.text(x, y, round(pv.df$p.value, 2))
}
2:更改色标,白色位于 0。计算色标的每一段应该有多长,然后分别定义每一段:
color.ramp.length <- 20
negative.length <- round(abs(range(pv.df$fold.change)[1]) /
diff(range(pv.df$fold.change)) *
color.ramp.length)
positive.length <- color.ramp.length - negative.length
cols <- c(colorRampPalette(c("seagreen", "white"))(negative.length),
colorRampPalette(c("white", "firebrick"))(positive.length))
(注意:您可以使用 here 中的其他颜色选项。我只是觉得与 "red" / "green" 相关的颜色很伤眼。)
3:修改坐标轴标题/标签。在levelplot()
.
中指定相关参数
levelplot(fold.change ~ comparison*compound,
pv.df,
panel = myPanel,
col.regions = cols,
colorkey = list(col = cols,
at = do.breaks(range(pv.df$fold.change),
color.ramp.length)),
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 = "Total FAME abundance - TREATMENT",
cex = 1.5)) # change font size for plot title
标题差不多self-descriptive。我想用格子做一个 heatmap-like 图,也显示数据值,比如 here
然而,在我的例子中,我想根据一个变量 (fold.change) 为绘图着色,但显示另一个变量 (p.value) 的值。
颜色范围最好是 green-white-red,白色为 0(绿色为负 fold.change 值,红色为正值)。
我的最后一个问题是如何更改标题和坐标轴文本的文字大小、删除坐标轴标题以及将x 轴文本旋转45 度;我在文档中找不到此信息。谢谢!
到目前为止,这是我的 MWE:
library(lattice)
library(latticeExtra)
library(RColorBrewer)
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, 1),
fold.change = runif(24, -2, 6))
myPanel <- function(x, y, z, ...) {
panel.levelplot(x,y,z,...)
panel.text(x, y, round(z,1))
}
cols <- rev(colorRampPalette(brewer.pal(6, "RdYlGn"))(20))
png(filename = "test.png", height = 1000, width = 600)
print(
levelplot(fold.change ~ comparison*compound,
pv.df,
panel = myPanel,
col.regions = cols,
colorkey = list(col = cols,
at = do.breaks(range(pv.df$fold.change), 20)),
scales = list(x = list(rot = 90)),
main = "Total FAME abundance - TREATMENT",
type = "g")
)
dev.off()
产生这个情节的是:
谢谢!
你的问题有几个部分。让我们一一解决:
1: 更改标签。这可以通过改变 panel.text()
:
myPanel <- function(x, y, z, ...) {
panel.levelplot(x, y, z, ...)
panel.text(x, y, round(pv.df$p.value, 2))
}
2:更改色标,白色位于 0。计算色标的每一段应该有多长,然后分别定义每一段:
color.ramp.length <- 20
negative.length <- round(abs(range(pv.df$fold.change)[1]) /
diff(range(pv.df$fold.change)) *
color.ramp.length)
positive.length <- color.ramp.length - negative.length
cols <- c(colorRampPalette(c("seagreen", "white"))(negative.length),
colorRampPalette(c("white", "firebrick"))(positive.length))
(注意:您可以使用 here 中的其他颜色选项。我只是觉得与 "red" / "green" 相关的颜色很伤眼。)
3:修改坐标轴标题/标签。在levelplot()
.
levelplot(fold.change ~ comparison*compound,
pv.df,
panel = myPanel,
col.regions = cols,
colorkey = list(col = cols,
at = do.breaks(range(pv.df$fold.change),
color.ramp.length)),
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 = "Total FAME abundance - TREATMENT",
cex = 1.5)) # change font size for plot title