【问题标题】:How to select and plot raster specific row in R?如何在 R 中选择和绘制特定于栅格的行?
【发布时间】:2013-11-08 22:30:37
【问题描述】:

如何随时间绘制特定行?例如我想绘制第 10 行(layer.1 到 5 实际上是第 1 年到第 5 年。

library(raster)  
r <- raster(nrow=5, ncol=5)  
s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) )  
s[]  

       layer.1    layer.2     layer.3    layer.4    layer.5  
 [1,]  6.7134890  6.9141251  4.38213123  4.8995302  2.3105321  
 [2,]  3.4323121  6.1074031 10.12872426  3.6728949  3.2252562  
 [3,]  4.4370107  3.1397068  5.47572912  1.9692684  4.0064603  
 [4,] -1.5588723  0.4075960 -0.73333754  6.3589944  5.0355051  
 [5,]  2.8095750  5.4264553  1.17820009  2.0665198  8.0491221  
 [6,]  4.3422219  2.1106691  1.08638206  5.0640175  6.8057674  
 [7,] -3.1072366 -1.1174633  6.28901706  5.0713964  1.8651354  
 [8,] -0.5628539  2.1868130  1.21288191  0.3114011  3.0452161  
 [9,]  0.1725606  3.4535112 -1.38043518  3.6439042  5.4005650  
[10,] -2.3376856  4.8803363 -0.05927408  7.9275016  4.7013126  
[11,]  2.3032655  2.4974161  4.63961513  1.4021305 10.2302589  
[12,]  0.4470648  1.1660421 -0.70127807  6.3293479  6.6178080  
[13,]  2.5835127 -0.8768809  2.87405383  6.1361518  3.4851934  
[14,] -3.2769134  2.1721391  2.17317611  1.4170633  0.6446692  
[15,]  1.0771079 -2.5369687  4.89710339  1.8667695  4.4847933  
[16,]  7.2532218  3.0210221  0.56993065  2.4564492  6.9473683  
[17,]  4.0682441 -0.8198112  4.85259334  7.3296033  8.9541915  
[18,]  5.3991328 -0.9818425  1.73782230  2.9220433  4.9865858  
[19,]  2.0556183 -0.7470914  5.44869675  1.6452235  4.5236089  
[20,] -0.6277883  6.7255821  5.12606765  5.5721351  4.7081256  
[21,]  9.0139352  3.1350767  6.59366754  2.0351358  5.1865195  
[22,]  7.0598020  0.2869291  7.14368927  9.7213576  0.4251934  
[23,]  1.6430309  6.3806803  5.95776881  7.5234383  4.8860264  
[24,]  1.9473764  1.5386180  3.89690297  2.5333431  7.7217174  
[25,]  0.7960661 -1.5137800  2.84861591 -5.9986647  2.9309536  

提前致谢。

【问题讨论】:

  • Opss....NA 值现在是个问题....有人吗?
  • 看看?na.omit
  • 不走运@eddi。 > plot(na.omit(c(s[125]))) plot.window(...) 中的错误:需要有限的“xlim”值此外:警告消息:1:在 min(x) 中:没有非min 缺少参数;返回 Inf 2:在 max(x) 中:max 没有非缺失参数;返回 -Inf 3: In min(x) : min 没有非缺失参数;返回 Inf 4:在 max(x) 中:max 没有非缺失参数;返回 -Inf
  • 好吧,它告诉你问题 - 你需要取出无限值

标签: r plot raster


【解决方案1】:
require(raster)  
r <- raster(nrow=5, ncol=5)  
set.seed(20) #exact reproducible example
s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) )  

s[is.na(s[])] <- 0 #NA values to be replaced (here it is being replaced with zero)

summary(s) # look for minimum and maximum pixel values

#some plot examples
plot(c(s[2]),ylim=c(-8,11)) #range -8 to 11 encompasses minimun and maximum s values

plot(c(s[25]),ylim=c(-8,11))

plot(c(s[600]),ylim=c(-8,11)) #creates an empty plot as there is no such row

plot(s) #plot raster s

【讨论】:

    【解决方案2】:

    如果您的 RasterStack 包含时空数据,您可以尝试 setZgetZ 函数来自 rasterzoo 包。

    library(raster)
    library(zoo)
    
    r <- raster(nrow=5, ncol=5)
    ## another way to create the RasterStack using init
    s <- stack(lapply(1:5, function(i)init(r, fun=rnorm, mean=i, sd=3)))
    ## Set the time indez of the RasterStack
    s <- setZ(s, 2001:2005)
    s
    
    > s
    class       : RasterStack 
    dimensions  : 5, 5, 25, 5  (nrow, ncol, ncell, nlayers)
    resolution  : 72, 36  (x, y)
    extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    coord. ref. : +proj=longlat +datum=WGS84 
    names       :    layer.1,    layer.2,    layer.3,    layer.4,    layer.5 
    min values  : -2.0324027, -2.7135752, -2.0341025, -0.8495401, -1.6712431 
    max values  :  1.3919942,  1.9809912,  2.8753805,  1.3746949,  0.6760691 
    time        : 2001, 2002, 2003, 2004, 2005
    

    让我们用zoo定义一个时间序列,单元格10的内容 s 的第 10 行(s[] 的第 10 行):

    z <- zoo(c(s[10]), getZ(s))
    > z
           2001        2002        2003        2004        2005 
     0.07586314  0.10235694 -1.28134027 -0.84954013 -1.11903690 
    

    有一种方法可以绘制zoo对象:

    plot(z)
    

    如果您有NA 值,zoo 中有几个函数可以管理它们(na.approxna.fill 等):

    z[4] <- NA
    
    plot(na.approx(z))
    

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 2021-09-17
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      相关资源
      最近更新 更多