Reputation: 803
How do I plot a specific row over time? For example I want to plot row 10(layer.1 to 5 is actually year 1 to year 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
Thanks in advance.
Upvotes: 1
Views: 1782
Reputation: 4511
If your RasterStack
contains spatiotemporal data you could try the
setZ
and getZ
functions from raster
in combination with the
zoo
package.
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
Let's define a time series with zoo
with the content of the cell 10
of s
(row 10 of s[]
):
z <- zoo(c(s[10]), getZ(s))
> z
2001 2002 2003 2004 2005
0.07586314 0.10235694 -1.28134027 -0.84954013 -1.11903690
There is a method to plot zoo
objects:
plot(z)
If you have NA
values there are several functions in zoo
to manage them (na.approx
, na.fill
, etc.):
z[4] <- NA
plot(na.approx(z))
Upvotes: 1
Reputation: 4928
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
Upvotes: 1