[R-sig-Geo] Select and plot specific row in rasterstack

Oscar Perpiñán Lamigueiro oscar.perpinan at upm.es
Mon Nov 11 12:37:49 CET 2013


If you know the row and column numbers use 's[row, column]':

s[2, 3]

or if you want to know the cell number:

myCell <- cellFromRowCol(s, 2, 3)
s[myCell]

Oscar

Eddie Smith writes:

> Thanks a lot Oscar.
>
> I just realized that I had misunderstood how R listed the pixels.Row 1 is
> actually consist of pixel 1 to 5. Row 2, pixel 6 to 10 and so on.
>
> What I want to plot is actually a single pixel over time. For example, I
> want to plot pixel at row 2, column 3 for those 5 years.
>
> I know I can use plot(c(s[8])) but it's hard for me to know the pixel if
> for example I had much larger rasterstack and want to plot pixel at row 200
> and column 130.
>
>
>
>
>
>
>
>
>
>
> On Mon, Nov 11, 2013 at 10:13 AM, Oscar Perpi..n Lamigueiro <
> oscar.perpinan at upm.es> wrote:
>
>> Hello,
>>
>> You can extract a row from a RasterStack with s[i,]. Try this code:
>>
>> library(raster)
>> library(zoo)
>> library(lattice)
>>
>> r <- raster(nrow=20, ncol=5)
>> s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) )
>> ## assign a time index to the 'RasterStack'
>> s <- setZ(s, seq(Sys.Date()-4, by='day', length=5))
>>
>> ## extract a row (10) and convert the result (a 'matrix') into a 'zoo'
>> ## object using the time index of the 'RasterStack' object
>> zz <- zoo(t(s[10,]), order.by=getZ(s))
>>
>> xyplot(zz, superpose=TRUE)
>>
>>
>> Best,
>>
>> Oscar.
>> Eddie Smith writes:
>>
>> > Dear list,
>> >
>> > 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
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-sig-Geo mailing list
>> > R-sig-Geo at r-project.org
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>> --
>> Oscar Perpi..n Lamigueiro
>> Dpto. Ingenier.a El.ctrica (ETSIDI-UPM)
>> Grupo de Sistemas Fotovoltaicos (IES-UPM)
>> URL: http://http://oscarperpinan.github.io
>> Twitter: @oscarperpinan
>>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-- 
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://http://oscarperpinan.github.io
Twitter: @oscarperpinan



More information about the R-sig-Geo mailing list