【问题标题】:rename layers stacked in histogram (rasterVis)重命名直方图中堆叠的图层 (rasterVis)
【发布时间】:2015-10-15 09:06:42
【问题描述】:

我遇到以下问题,重命名层堆栈失败。

这是我的代码示例。

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

欢迎所有建议。

您好!

【问题讨论】:

    标签: r plot histogram rastervis


    【解决方案1】:

    names.attr 参数尚未在 histogram 函数中实现。相反,您必须使用原始lattice::histogram 函数提供的strip 参数:

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

    【讨论】:

    • 已解决。非常感谢@oscar-perpiñán 博士。
    猜你喜欢
    • 2016-12-19
    • 2014-03-12
    • 2015-11-28
    • 2021-11-24
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多