0

我有以下问题,我未能重命名层堆栈。

这是我的代码示例。

###
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"

如图所示:

输出 但我需要没有“X”,即:

[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)

欢迎所有建议。

问候!

4

1 回答 1

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)
          })
于 2015-10-15T11:36:53.830 回答