[R-sig-Geo] Plot spatial time series
Thiago Veloso
thi_veloso at yahoo.com.br
Sat Jun 9 19:50:19 CEST 2012
Matthew,
Thank you very much for the suggestion. You assumed correctly: I wanted to plot an average of the whole raster.
I managed to plot the data (please see http://img137.imageshack.us/img137/601/screenshotan.png), but it's still precarious. Right now I am tying to improve the plot with ggplot2 package!
Cheers,
Thiago.
----- Original Message -----
From: Matt Landis <landis at isciences.com>
To: Thiago Veloso <thi_veloso at yahoo.com.br>
Cc: R-SIG list <r-sig-geo at r-project.org>
Sent: Friday, June 8, 2012 3:45 PM
Subject: Re: [R-sig-Geo] Plot spatial time series
Fixing typos:
tmp.df <- data.frame(date = rep(NA, length(files)), lai = NA)
for ( i in 1:length(files) ){
f <- files[i]
cat('Reading file: ', f, '\n')
tmp.df$date[i] <- regmatches(f, regexpr('[0-9]{7}', f))
tmp.df$lai[i] <- mean(getValues(r), na.rm = TRUE)
}
Hopefully that makes more sense...
2012/6/8 Matthew Landis <landis at isciences.com>:
> Thiago,
>
> It's not clear to me exactly what your plot would represent. Is it the
> average of the whole scene? One pixel?
>
> Assuming you're plotting 1 value per raster, I wouldn't try to stack all 506
> files. I would read one at a time, summarize the values and put them into a
> dataframe. Here's an idea of what I would try:
>
> temp.df <- data.frame(date = rep(NA, length(files)), lai = NA)
> for ( f in files ){
> cat('Reading file: ', f, '\n')
> tmp.df$date <- regmatches(f, regexpr('[0-9]{7}', f))
> tmp.df$lai <- mean(getValues(r), na.rm = TRUE)
> }
>
> You could convert date to a Date object, but I don't know off the top of my
> head how to deal with Julian days.
>
> More simply, you could say tmp.df$date <- as.numeric(tmp.df$date), and plot
> would treat it approximately correctly.
>
> Finish up with:
>
> with(tmp.df, plot(date, lai))
>
> Matt
>
>
> On 6/8/2012 2:10 PM, Thiago Veloso wrote:
>>
>> Dear all,
>>
>> I have 506 netcdf files representing a time series of leaf index area
>> estimated by MODIS, starting from 01jan/2001 and with increment of 8 days.
>> Dates are in the name of the file and are presented in julian format.
>> Together, files reach 35GB. Please see below how I load the files:
>>
>>> library (raster)
>>> files<- list.files(pattern='lai*')
>>> files
>>
>> [1] "lai2001001.nc" "lai2001009.nc" "lai2001017.nc" "lai2001025.nc"
>> [5] "lai2001033.nc" "lai2001041.nc" "lai2001049.nc" "lai2001057.nc"
>> [9] "lai2001065.nc" "lai2001073.nc" "lai2001081.nc" "lai2001089.nc"
>> [13] "lai2001097.nc" "lai2001105.nc" "lai2001113.nc" "lai2001121.nc"
>> [17] "lai2001129.nc" "lai2001137.nc" "lai2001145.nc" "lai2001153.nc"
>> [21] "lai2001161.nc" "lai2001169.nc" "lai2001177.nc" "lai2001185.nc"
>> [25] "lai2001193.nc" "lai2001201.nc" "lai2001209.nc" "lai2001217.nc"
>> [29] "lai2001225.nc" "lai2001233.nc" "lai2001241.nc" "lai2001249.nc"
>> [33] "lai2001257.nc" "lai2001265.nc" "lai2001273.nc" "lai2001281.nc"
>> [37] "lai2001289.nc" "lai2001297.nc" "lai2001305.nc" "lai2001313.nc"
>> [41] "lai2001321.nc" "lai2001329.nc" "lai2001337.nc" "lai2001345.nc"
>> [45] "lai2001353.nc" "lai2001361.nc" "lai2002001.nc" "lai2002009.nc"
>> [49] "lai2002017.nc" "lai2002025.nc" "lai2002033.nc" "lai2002041.nc"
>> [53] "lai2002049.nc" "lai2002057.nc" "lai2002065.nc" "lai2002073.nc"
>> [57] "lai2002081.nc" "lai2002089.nc" "lai2002097.nc" "lai2002105.nc"
>> [61] "lai2002113.nc" "lai2002121.nc" "lai2002129.nc" "lai2002137.nc"
>> [65] "lai2002145.nc" "lai2002153.nc" "lai2002161.nc" "lai2002169.nc"
>> [69] "lai2002177.nc" "lai2002185.nc" "lai2002193.nc" "lai2002201.nc"
>> [73] "lai2002209.nc" "lai2002217.nc" "lai2002225.nc" "lai2002233.nc"
>> [77] "lai2002241.nc" "lai2002249.nc" "lai2002257.nc" "lai2002265.nc"
>> [81] "lai2002273.nc" "lai2002281.nc" "lai2002289.nc" "lai2002297.nc"
>> [85] "lai2002305.nc" "lai2002313.nc" "lai2002321.nc" "lai2002329.nc"
>> [89] "lai2002337.nc" "lai2002345.nc" "lai2002353.nc" "lai2002361.nc"
>> [93] "lai2003001.nc" "lai2003009.nc" "lai2003017.nc" "lai2003025.nc"
>> [97] "lai2003033.nc" "lai2003041.nc" "lai2003049.nc" "lai2003057.nc"
>> [101] "lai2003065.nc" "lai2003073.nc" "lai2003081.nc" "lai2003089.nc"
>> [105] "lai2003097.nc" "lai2003105.nc" "lai2003113.nc" "lai2003121.nc"
>> [109] "lai2003129.nc" "lai2003137.nc" "lai2003145.nc" "lai2003153.nc"
>> [113] "lai2003161.nc" "lai2003169.nc" "lai2003177.nc" "lai2003185.nc"
>> [117] "lai2003193.nc" "lai2003201.nc" "lai2003209.nc" "lai2003217.nc"
>> [121] "lai2003225.nc" "lai2003233.nc" "lai2003241.nc" "lai2003249.nc"
>> [125] "lai2003257.nc" "lai2003265.nc" "lai2003273.nc" "lai2003281.nc"
>> [129] "lai2003289.nc" "lai2003297.nc" "lai2003305.nc" "lai2003313.nc"
>> [133] "lai2003321.nc" "lai2003329.nc" "lai2003337.nc" "lai2003345.nc"
>> [137] "lai2003353.nc" "lai2003361.nc" "lai2004001.nc" "lai2004009.nc"
>> [141] "lai2004017.nc" "lai2004025.nc" "lai2004033.nc" "lai2004041.nc"
>> [145] "lai2004049.nc" "lai2004057.nc" "lai2004065.nc" "lai2004073.nc"
>> [149] "lai2004081.nc" "lai2004089.nc" "lai2004097.nc" "lai2004105.nc"
>> [153] "lai2004113.nc" "lai2004121.nc" "lai2004129.nc" "lai2004137.nc"
>> [157] "lai2004145.nc" "lai2004153.nc" "lai2004161.nc" "lai2004169.nc"
>> [161] "lai2004177.nc" "lai2004185.nc" "lai2004193.nc" "lai2004201.nc"
>> [165] "lai2004209.nc" "lai2004217.nc" "lai2004225.nc" "lai2004233.nc"
>> [169] "lai2004241.nc" "lai2004249.nc" "lai2004257.nc" "lai2004265.nc"
>> [173] "lai2004273.nc" "lai2004281.nc" "lai2004289.nc" "lai2004297.nc"
>> [177] "lai2004305.nc" "lai2004313.nc" "lai2004321.nc" "lai2004329.nc"
>> [181] "lai2004337.nc" "lai2004345.nc" "lai2004353.nc" "lai2004361.nc"
>> [185] "lai2005001.nc" "lai2005009.nc" "lai2005017.nc" "lai2005025.nc"
>> [189] "lai2005033.nc" "lai2005041.nc" "lai2005049.nc" "lai2005057.nc"
>> [193] "lai2005065.nc" "lai2005073.nc" "lai2005081.nc" "lai2005089.nc"
>> [197] "lai2005097.nc" "lai2005105.nc" "lai2005113.nc" "lai2005121.nc"
>> [201] "lai2005129.nc" "lai2005137.nc" "lai2005145.nc" "lai2005153.nc"
>> [205] "lai2005161.nc" "lai2005169.nc" "lai2005177.nc" "lai2005185.nc"
>> [209] "lai2005193.nc" "lai2005201.nc" "lai2005209.nc" "lai2005217.nc"
>> [213] "lai2005225.nc" "lai2005233.nc" "lai2005241.nc" "lai2005249.nc"
>> [217] "lai2005257.nc" "lai2005265.nc" "lai2005273.nc" "lai2005281.nc"
>> [221] "lai2005289.nc" "lai2005297.nc" "lai2005305.nc" "lai2005313.nc"
>> [225] "lai2005321.nc" "lai2005329.nc" "lai2005337.nc" "lai2005345.nc"
>> [229] "lai2005353.nc" "lai2005361.nc" "lai2006001.nc" "lai2006009.nc"
>> [233] "lai2006017.nc" "lai2006025.nc" "lai2006033.nc" "lai2006041.nc"
>> [237] "lai2006049.nc" "lai2006057.nc" "lai2006065.nc" "lai2006073.nc"
>> [241] "lai2006081.nc" "lai2006089.nc" "lai2006097.nc" "lai2006105.nc"
>> [245] "lai2006113.nc" "lai2006121.nc" "lai2006129.nc" "lai2006137.nc"
>> [249] "lai2006145.nc" "lai2006153.nc" "lai2006161.nc" "lai2006169.nc"
>> [253] "lai2006177.nc" "lai2006185.nc" "lai2006193.nc" "lai2006201.nc"
>> [257] "lai2006209.nc" "lai2006217.nc" "lai2006225.nc" "lai2006233.nc"
>> [261] "lai2006241.nc" "lai2006249.nc" "lai2006257.nc" "lai2006265.nc"
>> [265] "lai2006273.nc" "lai2006281.nc" "lai2006289.nc" "lai2006297.nc"
>> [269] "lai2006305.nc" "lai2006313.nc" "lai2006321.nc" "lai2006329.nc"
>> [273] "lai2006337.nc" "lai2006345.nc" "lai2006353.nc" "lai2006361.nc"
>> [277] "lai2007001.nc" "lai2007009.nc" "lai2007017.nc" "lai2007025.nc"
>> [281] "lai2007033.nc" "lai2007041.nc" "lai2007049.nc" "lai2007057.nc"
>> [285] "lai2007065.nc" "lai2007073.nc" "lai2007081.nc" "lai2007089.nc"
>> [289] "lai2007097.nc" "lai2007105.nc" "lai2007113.nc" "lai2007121.nc"
>> [293] "lai2007129.nc" "lai2007137.nc" "lai2007145.nc" "lai2007153.nc"
>> [297] "lai2007161.nc" "lai2007169.nc" "lai2007177.nc" "lai2007185.nc"
>> [301] "lai2007193.nc" "lai2007201.nc" "lai2007209.nc" "lai2007217.nc"
>> [305] "lai2007225.nc" "lai2007233.nc" "lai2007241.nc" "lai2007249.nc"
>> [309] "lai2007257.nc" "lai2007265.nc" "lai2007273.nc" "lai2007281.nc"
>> [313] "lai2007289.nc" "lai2007297.nc" "lai2007305.nc" "lai2007313.nc"
>> [317] "lai2007321.nc" "lai2007329.nc" "lai2007337.nc" "lai2007345.nc"
>> [321] "lai2007353.nc" "lai2007361.nc" "lai2008001.nc" "lai2008009.nc"
>> [325] "lai2008017.nc" "lai2008025.nc" "lai2008033.nc" "lai2008041.nc"
>> [329] "lai2008049.nc" "lai2008057.nc" "lai2008065.nc" "lai2008073.nc"
>> [333] "lai2008081.nc" "lai2008089.nc" "lai2008097.nc" "lai2008105.nc"
>> [337] "lai2008113.nc" "lai2008121.nc" "lai2008129.nc" "lai2008137.nc"
>> [341] "lai2008145.nc" "lai2008153.nc" "lai2008161.nc" "lai2008169.nc"
>> [345] "lai2008177.nc" "lai2008185.nc" "lai2008193.nc" "lai2008201.nc"
>> [349] "lai2008209.nc" "lai2008217.nc" "lai2008225.nc" "lai2008233.nc"
>> [353] "lai2008241.nc" "lai2008249.nc" "lai2008257.nc" "lai2008265.nc"
>> [357] "lai2008273.nc" "lai2008281.nc" "lai2008289.nc" "lai2008297.nc"
>> [361] "lai2008305.nc" "lai2008313.nc" "lai2008321.nc" "lai2008329.nc"
>> [365] "lai2008337.nc" "lai2008345.nc" "lai2008353.nc" "lai2008361.nc"
>> [369] "lai2009001.nc" "lai2009009.nc" "lai2009017.nc" "lai2009025.nc"
>> [373] "lai2009033.nc" "lai2009041.nc" "lai2009049.nc" "lai2009057.nc"
>> [377] "lai2009065.nc" "lai2009073.nc" "lai2009081.nc" "lai2009089.nc"
>> [381] "lai2009097.nc" "lai2009105.nc" "lai2009113.nc" "lai2009121.nc"
>> [385] "lai2009129.nc" "lai2009137.nc" "lai2009145.nc" "lai2009153.nc"
>> [389] "lai2009161.nc" "lai2009169.nc" "lai2009177.nc" "lai2009185.nc"
>> [393] "lai2009193.nc" "lai2009201.nc" "lai2009209.nc" "lai2009217.nc"
>> [397] "lai2009225.nc" "lai2009233.nc" "lai2009241.nc" "lai2009249.nc"
>> [401] "lai2009257.nc" "lai2009265.nc" "lai2009273.nc" "lai2009281.nc"
>> [405] "lai2009289.nc" "lai2009297.nc" "lai2009305.nc" "lai2009313.nc"
>> [409] "lai2009321.nc" "lai2009329.nc" "lai2009337.nc" "lai2009345.nc"
>> [413] "lai2009353.nc" "lai2009361.nc" "lai2010001.nc" "lai2010009.nc"
>> [417] "lai2010017.nc" "lai2010025.nc" "lai2010033.nc" "lai2010041.nc"
>> [421] "lai2010049.nc" "lai2010057.nc" "lai2010065.nc" "lai2010073.nc"
>> [425] "lai2010081.nc" "lai2010089.nc" "lai2010097.nc" "lai2010105.nc"
>> [429] "lai2010113.nc" "lai2010121.nc" "lai2010129.nc" "lai2010137.nc"
>> [433] "lai2010145.nc" "lai2010153.nc" "lai2010161.nc" "lai2010169.nc"
>> [437] "lai2010177.nc" "lai2010185.nc" "lai2010193.nc" "lai2010201.nc"
>> [441] "lai2010209.nc" "lai2010217.nc" "lai2010225.nc" "lai2010233.nc"
>> [445] "lai2010241.nc" "lai2010249.nc" "lai2010257.nc" "lai2010265.nc"
>> [449] "lai2010273.nc" "lai2010281.nc" "lai2010289.nc" "lai2010297.nc"
>> [453] "lai2010305.nc" "lai2010313.nc" "lai2010321.nc" "lai2010329.nc"
>> [457] "lai2010337.nc" "lai2010345.nc" "lai2010353.nc" "lai2010361.nc"
>> [461] "lai2011001.nc" "lai2011009.nc" "lai2011017.nc" "lai2011025.nc"
>> [465] "lai2011033.nc" "lai2011041.nc" "lai2011049.nc" "lai2011057.nc"
>> [469] "lai2011065.nc" "lai2011073.nc" "lai2011081.nc" "lai2011089.nc"
>> [473] "lai2011097.nc" "lai2011105.nc" "lai2011113.nc" "lai2011121.nc"
>> [477] "lai2011129.nc" "lai2011137.nc" "lai2011145.nc" "lai2011153.nc"
>> [481] "lai2011161.nc" "lai2011169.nc" "lai2011177.nc" "lai2011185.nc"
>> [485] "lai2011193.nc" "lai2011201.nc" "lai2011209.nc" "lai2011217.nc"
>> [489] "lai2011225.nc" "lai2011233.nc" "lai2011241.nc" "lai2011249.nc"
>> [493] "lai2011257.nc" "lai2011265.nc" "lai2011273.nc" "lai2011281.nc"
>> [497] "lai2011289.nc" "lai2011297.nc" "lai2011305.nc" "lai2011313.nc"
>> [501] "lai2011321.nc" "lai2011329.nc" "lai2011337.nc" "lai2011345.nc"
>> [505] "lai2011353.nc" "lai2011361.nc"
>>
>> Below is the sample of a unique file:
>>
>>> r<- raster(files[1])
>>
>> Loading required package: ncdf
>>
>>> r
>>
>> class : RasterLayer
>> dimensions : 4361, 4342, 18935462 (nrow, ncol, ncell)
>> resolution : 0.00898, 0.00898 (x, y)
>> extent : -73.8467, -34.85554, -33.7775, 5.38428 (xmin, xmax, ymin,
>> ymax)
>> coord. ref. : +proj=longlat +datum=WGS84
>> values : /mnt/disco3/MODIS/MOSAIC/LAI/netcdf/lai2001001.nc
>> layer name : variable
>> zvar : lai_modis
>>
>> The only indication of date in these files is the name: no date
>> information is present in the header of the netcdf.
>>
>> What I need to do is to plot a time series of these files taking into
>> account the date. In this figure, the x-axis would be the date and the
>> y-axis would show LAI values. I know how to loop over files (thanks Robert),
>> but I do not know how to plot the data sequentially.
>>
>>> years<- substr(files, 4, 7)
>>> for (y in 2001:2011) {
>>
>> f<- files[ years == y ]
>> s<- stack(f)
>> }
>>
>> Any tips on how to plot the entire series in on single plot? I
>> appreciate any help.
>>
>> Thanks in advance,
>> Thiago.
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~
> Matthew Landis, Ph.D.
> Research Scientist
> ISciences, LLC
> 61 Main St. Suite 200
> Burlington VT 05401
> 802.864.2999
> www.isciences.com
> ~~~~~~~~~~~~~~~~~~~~~~~~~~
>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Matthew Landis, Ph.D.
Research Scientist
ISciences, LLC
61 Main St., Suite 200
Burlington VT 05401
802-864-2999
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the R-sig-Geo
mailing list