0
  1. I have downloaded MODIS NDVI Aqua 16 days composite for the year 2015 and converted to tiff. There are 23 files.
  2. I want to calculate the average of each raster and plot a graph where I have dates on x and NDVI values on the y axis.
  3. I stacked them together and removed some part of the file names to make it short ( only for visual easyness) and stored in rasterNames
  4. Next, I ran a loop to run through all the files, calculated mean and rescaled by 0.0001 to get the value in valid range (0-1)

Now, I got stock with what I have to do next to plot the graph where I have a vector avg_ndvi with values and rasterNames with the labels.

Is there any other smarter way to achieve the same? Because finally I am interested to add terra product in the same line and get 8 days composite but may show the labels only is some intervals , may be every month, or every 16 days.

#------------ Load the library--------
library(raster)
library(rgdal)
library(rasterVis)

setwd("C:/Working_Folder/R")
aqua_ndvi_path <- "aqua_ndvi_2015"
avg_ndvi<-vector()
date_ndvi <- vector()

#now load the Tiffs:
    all_ndvi <- list.files(aqua_ndvi_path,
                          full.names = TRUE,
                          pattern = ".tif$")
   ndvi_stack <- stack(all_ndvi) 
   # ndvi_stack_brick <- brick(ndvi_stack) 
   rasterNames  <- gsub("MYD13Q1_2015.","", names(ndvi_stack)) # remove some portion of file name
   rasterNames  <- gsub(".250m_16_days_NDVI","",rasterNames)   # remove some portion of the file name
   rasterNames  # This will give me short names so that i can use it as a label in graph/plot later


## --------- scale ndvi value by 10000 and calculate ndvi average of each raster and store it in the vector---------
   for (i in all_ndvi)     {
     j=cellStats(brick(i),mean)
     j=j*0.0001
     avg_ndvi<- c(avg_ndvi,j)
      }
   print(avg_ndvi)
alistaire
  • 42,459
  • 4
  • 77
  • 117
Bhoj Raj
  • 19
  • 4
  • Is `plot(avg_ndvi ~ rasterNames)` not working? – Roman Luštrik Apr 07 '16 at 07:18
  • This plot function plots the result. But the x axis is just a sequence of numbers from 0-12. My raster names are dates like : rasterNames # This will give me short names so that i can use it as a label in graph/plot later [1] "01.09" "01.25" "02.10" "02.26" "03.14" "03.30" "04.15" "05.01" "05.17" "06.02" "06.18" "07.04" "07.20" "08.05" [15] "08.21" "09.06" "09.22" "10.08" "10.24" "11.09" "11.25" "12.11" "12.27. I want them displayed – Bhoj Raj Apr 07 '16 at 07:29
  • Please edit your original question to reflect this. The code that generates the data isn't really helpful in this case. Consider conveying the resulting objects (`avg_ndvi` and `rasterNames`) in an easy to copy&paste form. Please also read [this comprehensive set of answers](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to learn how to properly present your problem. – Roman Luštrik Apr 07 '16 at 07:35
  • @RomanLuštrik Thank you for your suggestion. I will try to format the question better. Looks like I have to spend some time for that before I can ask more questions. BTW, can we edit the question? – Bhoj Raj Apr 07 '16 at 08:36
  • Of course, edit THIS question, don't post a new one. Please study how to ask and format questions. Water everything down to bare necessity which should be reproducible if possible (see [this thread for ideas](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Roman Luštrik Apr 07 '16 at 13:15

0 Answers0