标题几乎是自我描述性的。我想用格子做一个类似热图的绘图,显示数据值,如here
但是,在我的情况下,我想根据一个变量(fold.change)为绘图着色,但是显示另一个变量的值(p.value)。
最佳颜色范围为绿 - 白 - 红,白色为0(负折叠。绿色为正值,红色为正值)。
我的最后一个问题是如何更改标题和轴文本的文本大小,删除轴标题,以及将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()
产生这个情节:
谢谢!
答案 0 :(得分:1)
您的问题有几个部分。让我们逐一解决它们:
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中的其他颜色选项。我只是找到与&#34;红色&#34; /&#34;绿色&#34;眼睛酸痛相关的颜色。)
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