我使用R格子包中的等級圖。我的結果如下圖所示。在格子圖中使用圖案添加/替代背景顏色
我現在的問題是,我需要生成一個黑白版本的打印。
有沒有辦法將顏色更改爲灰度,並給矩形一個背景圖案,以便紅色與藍色區別一次?例如,想到點或斜線。
謝謝!
我使用R格子包中的等級圖。我的結果如下圖所示。在格子圖中使用圖案添加/替代背景顏色
我現在的問題是,我需要生成一個黑白版本的打印。
有沒有辦法將顏色更改爲灰度,並給矩形一個背景圖案,以便紅色與藍色區別一次?例如,想到點或斜線。
謝謝!
我找到了一種方法手動繪製成levelplot面板,並提請在全部小區的對角線填充圖案值大於0.5
然而更大的,我不能設法在顏色關鍵圖例中繪製相同的圖案。經過幾個小時的閱讀論壇,並試圖瞭解格子源代碼,我無法得知線索。也許別人可以解決這個問題。以下是我的了:
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)
使用兩個以上的模式(例如45°和135°定向的線具有不同密度的)將是混亂的,IMO。 (儘管事實上我不知道如何使用點陣來實現這一點)。通過使用灰度可以實現可讀模式,請參閱levelplot()
中的col.regions
參數。
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(8, "RdBu"))
p <- levelplot(Harman23.cor$cov, scales=list(x=list(rot=45)),
xlab="", ylab="", col.regions=cols)
# versus all greys
p <- levelplot(Harman23.cor$cov, scales=list(x=list(rot=45)),
xlab="", ylab="", col.regions=gray.colors)
p <- levelplot(Harman23.cor$cov, scales=list(x=list(rot=45)),
xlab="", ylab="", col.regions=gray.colors(6), cuts=6)
點會更容易增加,簡單地增加在上面panel.points
。給傳說添加點可能有點困難。以下函數在網格圖形中執行。
grid.colorbar(runif(10, -2, 5))
require(RColorBrewer)
require(scales)
diverging_palette <- function(d = NULL, centered = FALSE, midpoint = 0,
colors = RColorBrewer::brewer.pal(7,"PRGn")){
half <- length(colors)/2
if(!length(colors)%%2)
stop("requires odd number of colors")
if(!centered && !(midpoint <= max(d) && midpoint >= min(d)))
warning("Midpoint is outside the data range!")
values <- if(!centered) {
low <- seq(min(d), midpoint, length=half)
high <- seq(midpoint, max(d), length=half)
c(low[-length(low)], midpoint, high[-1])
} else {
mabs <- max(abs(d - midpoint))
seq(midpoint-mabs, midpoint + mabs, length=length(colors))
}
scales::gradient_n_pal(colors, values = values)
}
colorbarGrob <- function(d, x = unit(0.5, "npc"),
y = unit(0.1,"npc"),
height=unit(0.8,"npc"),
width=unit(0.5, "cm"), size=0.7,
margin=unit(1,"mm"), tick.length=0.2*width,
pretty.breaks = grid.pretty(range(d)),
digits = 2, show.extrema=TRUE,
palette = diverging_palette(d), n = 1e2,
point.negative=TRUE, gap =5,
interpolate=TRUE,
...){
## includes extreme limits of the data
legend.vals <- unique(round(sort(c(pretty.breaks, min(d), max(d))), digits))
legend.labs <- if(show.extrema)
legend.vals else unique(round(sort(pretty.breaks), digits))
## interpolate the colors
colors <- palette(seq(min(d), max(d), length=n))
## 1D strip of colors, from bottom <-> min(d) to top <-> max(d)
lg <- rasterGrob(rev(colors), # rasterGrob draws from top to bottom
y=y, interpolate=interpolate,
x=x, just=c("left", "bottom"),
width=width, height=height)
## box around color strip
bg <- rectGrob(x=x, y=y, just=c("left", "bottom"),
width=width, height=height, gp=gpar(fill="transparent"))
## positions of the tick marks
pos.y <- y + height * rescale(legend.vals)
if(!show.extrema) pos.y <- pos.y[-c(1, length(pos.y))]
## tick labels
ltg <- textGrob(legend.labs, x = x + width + margin, y=pos.y,
just=c("left", "center"))
## right tick marks
rticks <- segmentsGrob(y0=pos.y, y1=pos.y,
x0 = x + width,
x1 = x + width - tick.length,
gp=gpar())
## left tick marks
lticks <- segmentsGrob(y0=pos.y, y1=pos.y,
x0 = x ,
x1 = x + tick.length,
gp=gpar())
## position of the dots
if(any(d < 0)){
yneg <- diff(range(c(0, d[d<0])))/diff(range(d)) * height
clipvp <- viewport(clip=TRUE, x=x, y=y, width=width, height=yneg,
just=c("left", "bottom"))
h <- convertUnit(yneg, "mm", "y", valueOnly=TRUE)
pos <- seq(0, to=h, by=gap)
}
## coloured dots
cg <- if(!point.negative || !any(d < 0)) nullGrob() else
pointsGrob(x=unit(rep(0.5, length(pos)), "npc"), y = y + unit(pos, "mm") ,
pch=21, gp=gpar(col="white", fill="black"),size=unit(size*gap, "mm"), vp=clipvp)
## for more general pattern use the following
## gridExtra::patternGrob(x=unit(0.5, "npc"), y = unit(0.5, "npc") , height=unit(h,"mm"),
## pattern=1,granularity=unit(2,"mm"), gp=gpar(col="black"), vp=clipvp)
gTree(children=gList(lg, lticks, rticks, ltg, bg, cg),
width = width + margin + max(stringWidth(legend.vals)), ... , cl="colorbar")
}
grid.colorbar <- function(...){
g <- colorbarGrob(...)
grid.draw(g)
invisible(g)
}
widthDetails.colorbar <- function(x){
x$width
}
編輯:爲圖案填充,可以取代pointsGrob
與gridExtra::patternGrob
(你也可以爲基體的瓷磚做)。
(+1)尼斯的迴應。 – chl 2012-02-24 10:38:00
謝謝!但是,我希望中心是白色的。模式也只能用於一種顏色。 – Manuel 2012-02-23 16:19:35
@曼紐爾我不知道我們如何疊加虛線或虛線圖案。以灰色爲中心將很難:)也許使用ggplot你可以使用單元格的高度/寬度來玩,就像'ggfluctuation'中所做的一樣? – chl 2012-02-23 16:32:53