在晶格图中使用添加/代替背景颜色的图案 [英] Using patterns in addition/instead of background colors in lattice plots
问题描述
我正在使用R晶格封装中的水平图.我得到的图看起来如下图所示.
I am using level plots from the R lattice package. My resulting plots look like the one shown below.
我现在的问题是我需要生成黑白版本进行打印.
My problem now is that I need to generate a black and white version for printing.
是否有一种方法可以将颜色更改为灰度并为矩形提供背景图案,以便一次将红色与蓝色区分开?例如,会想到点或斜线.
Is there a way to change the colors to grayscale and give the rectangles a background pattern so the the red once are distinguishable from the blue ones? For example, dots or diagonal dashes come to mind.
谢谢!
推荐答案
我找到了一种方法来手动绘制到levelplot面板中,并在所有值大于0.5的单元格上绘制对角填充图案
I found a way to manually draw into the levelplot panel and to draw a diagonal fill pattern over all cells with values greater than 0.5
但是,我无法在色键图例中绘制相同的图案.在阅读了几个小时的论坛并尝试了解晶格源代码后,我一无所知.也许其他人可以解决这个问题.这是我得到的:
However, I couldn't manage to draw the same pattern in the color key legend. After hours of reading forums and trying to understand the lattice source code, I couldn't get a clue. Maybe someone else could fix that. Here is what I got:
library(lattice)
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(8, "RdBu"))
data <- Harman23.cor$cov
fx <- fy <- c()
for (r in seq(nrow(data)))
for (c in seq(ncol(data)))
{
if (data[r, c] > 0.5)
{
fx <- c(fx, r);
fy <- c(fy, c);
}
}
diag_pattern <- function(...)
{
panel.levelplot(...)
for (i in seq(length(fx)))
{
panel.linejoin(x = c(fx[i],fx[i]+.5), y= c(fy[i]+.5,fy[i]), col="black")
panel.linejoin(x = c(fx[i]-.5,fx[i]+.5), y= c(fy[i]+.5,fy[i]-.5), col="black")
panel.linejoin(x = c(fx[i]-.5,fx[i]), y= c(fy[i],fy[i]-.5), col="black")
}
}
p <- levelplot(data, scales=list(x=list(rot=45)),
xlab="", ylab="", col.regions=cols, panel=diag_pattern)
print(p)
这篇关于在晶格图中使用添加/代替背景颜色的图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!