我遇到以下问题,我无法重命名图层堆栈。
这是我的代码示例。
###
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
oo<-stack(r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r)
names(oo) <- as.character(2000:2015)
names(oo)
histogram(oo,na.rm=T,col="blue",
panel = function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath = dnorm, col = "red",
args = list(mean=mean(x),sd=sd(x)),lwd=2.5)
})
###
如果只使用数字
names(oo) <- as.character(2000:2015)
结果: 名称(OO)
[1] "X2000" "X2001" "X2002" "X2003" "X2004" "X2005" "X2006" "X2007" "X2008"
[10] "X2009" "X2010" "X2011" "X2012" "X2013" "x2014" "X2015"
如图所示:
[1] "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008"
[10] "2009" "2010" "2011" "2012" "2013" "2014" "2015"
显然不适用于数字,因为如果我尝试使用:
names(oo) <- paste0("hola",1:16)
names(oo)
[1] "hola1" "hola2" "hola3" "hola4" "hola5" "hola6" "hola7" "hola8"
[9] "hola9" "hola10" "hola11" "hola12" "hola13" "hola14" "hola15" "hola16"
结果在情节中没问题。
PD:同时尝试使用参数:
names.attr=as.character(2000,2015)
欢迎所有建议。
问候!
答案 0 :(得分:1)
names.attr
函数尚未在histogram
函数中实现。相反,您必须使用原始strip
函数提供的lattice::histogram
参数:
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
oo <- stack(replicate(8, r))
nms <- as.character(2000:2015)
histogram(oo, na.rm = T, col = "blue",
strip = strip.custom(factor.levels = nms),
panel = function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath = dnorm, col = "red",
args = list(mean=mean(x),
sd=sd(x)),
lwd=2.5)
})