Dear colleagues,
 
I am willing to plot the area average of a rasterstack for several variables 
across time.

Let's say I have four variables tempsoi, tempair, gdd and lai. For each of them 
I have a rasterstack with their values for 4 time steps.

What I want to do is create a dataframe with:
 - the name of the variable

 - the year of the measurement
 - the average of the correspondent rasterstack layer

So in the same graph I could plot, say, temporal series of lai vs tempair.

With the code below I *almost* get it:


----------------------------------------------------------------------------------------------------------------------------------------------------------------------

library(raster)
library(ggplot2)

# Create random rasterstack and set names
r1 <- r2 <- r3 <- r4 <- raster(nrow = 50, ncol = 100)
r1[] <- 10
r2[] <- 20
r3[] <- 30
r4[] <- 40
s <- stack(r1, r2, r3, r4)
names(s) <- c('X2005.01.01', 'X2006.01.01', 'X2007.01.01', 'X2008.01.01')

# Create random variable names
vars<-c('tempsoi','tempair','gdd','lai')

for( i in 1:length(vars) ) {
      v <- vars[i]
      
      # Create empty data.frame
      tmp.df1 <- data.frame(g=1, date=rep(NA, length(names(s))), values=NA, 
v=NA)
      
      # Loop over years (names), extract raster average values and put in the 
data.frame
      for ( j in 1:length(names(s)) ) {
            tmp.df1$date[j] <- format(as.Date(gsub(pattern='\\.', 
replacement='-', substring (names(s)[j],2))))
            tmp.df1$values[j] <- mean(getValues(s[[j]]), na.rm = TRUE)
            tmp.df1$v <- v
      } # end loop over names
      
      # plot data
      p <- ggplot(tmp.df1, aes(x=date, y=values, group=g))
      print(p + geom_line() + theme_bw() + labs(title = v))
      
} # end loop vars
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

However, instead of 

> tmp.df1
g       date values     v
1 2005-01-01     10 tempsoi
1 2006-01-01     20 tempsoi
1 2007-01-01     30 tempsoi
1 2008-01-01     40 tempsoi

I would like to have

> tmp.df1

g       date values     v
1 2005-01-01     10 tempsoi
1 2006-01-01     20 tempsoi
1 2007-01-01     30 tempsoi
1 2008-01-01     40 tempsoi
1 2005-01-01     10 tempair
1 2006-01-01     20 tempair
1 2007-01-01     30 tempair
1 2008-01-01     40 tempair
1 2005-01-01     10 gdd
1 2006-01-01     20 gdd
1 2007-01-01     30 gdd
1 2008-01-01     40 gdd
1 2005-01-01     10 lai
1 2006-01-01     20 lai
1 2007-01-01     30 lai
1 2008-01-01     40 lai

Please help me: how, in this script, can I include the vars in the data.frame 
as repetitions?


Thanks in advance,
--
Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
Phone: (612) 323 9898
        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to