[R-sig-Geo] Order raster images in a levelplot
Frede Aakmann Tøgersen
frtog at vestas.com
Tue Oct 21 21:12:53 CEST 2014
Hi
I give up. You'll have to approach the maintainer of the raster package directly.
As far as I can see there is no difference in the order of your stack(files) and 'd'.
Here is my documentation of that:
ndvif <- c( # output from names(modis)
"MOD13C2.A2010001.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010032.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010060.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010091.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010121.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010152.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010182.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010213.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010244.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010274.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010305.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2010335.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011001.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011032.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011060.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011091.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011121.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011152.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011182.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011213.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011244.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011274.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011305.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2011335.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012001.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012032.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012061.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012092.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012122.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012153.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012183.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012214.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012245.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012275.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012306.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2012336.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013001.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013032.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013060.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013091.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013121.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013152.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013182.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013213.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013244.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013274.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013305.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2013335.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014001.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014032.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014060.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014091.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014121.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014152.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014182.CMG_0.05_Deg_Monthly_NDVI",
"MOD13C2.A2014213.CMG_0.05_Deg_Monthly_NDVI")
ftimes <- substring(ndvif, 10, 16)
ftimes <- strptime(ftimes, format = "%Y%j")
d <- seq(as.Date("2010-01-01"), as.Date("2014-08-01"), by="month")
m1 <- months(ftimes)
m2 <- months(d)
cbind(m1, m2)
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
From: Navinder Singh [mailto:Navinder.Singh at slu.se]
Sent: 21. oktober 2014 15:45
To: Frede Aakmann Tøgersen
Cc: Oscar Perpiñán Lamigueiro; r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Order raster images in a levelplot
Here's what the stack looks like:
setwd("C:\\Users\\nasi\\Google<smb://Users/nasi/Google> Drive\\5KMNDVI_Monthly")
modis = stack(list.files(pattern='NDVI.tif'))
> names(modis)
[1] "MOD13C2.A2010001.CMG_0.05_Deg_Monthly_NDVI"
[2] "MOD13C2.A2010032.CMG_0.05_Deg_Monthly_NDVI"
[3] "MOD13C2.A2010060.CMG_0.05_Deg_Monthly_NDVI"
[4] "MOD13C2.A2010091.CMG_0.05_Deg_Monthly_NDVI"
[5] "MOD13C2.A2010121.CMG_0.05_Deg_Monthly_NDVI"
[6] "MOD13C2.A2010152.CMG_0.05_Deg_Monthly_NDVI"
[7] "MOD13C2.A2010182.CMG_0.05_Deg_Monthly_NDVI"
[8] "MOD13C2.A2010213.CMG_0.05_Deg_Monthly_NDVI"
[9] "MOD13C2.A2010244.CMG_0.05_Deg_Monthly_NDVI"
[10] "MOD13C2.A2010274.CMG_0.05_Deg_Monthly_NDVI"
[11] "MOD13C2.A2010305.CMG_0.05_Deg_Monthly_NDVI"
[12] "MOD13C2.A2010335.CMG_0.05_Deg_Monthly_NDVI"
[13] "MOD13C2.A2011001.CMG_0.05_Deg_Monthly_NDVI"
[14] "MOD13C2.A2011032.CMG_0.05_Deg_Monthly_NDVI"
[15] "MOD13C2.A2011060.CMG_0.05_Deg_Monthly_NDVI"
[16] "MOD13C2.A2011091.CMG_0.05_Deg_Monthly_NDVI"
[17] "MOD13C2.A2011121.CMG_0.05_Deg_Monthly_NDVI"
[18] "MOD13C2.A2011152.CMG_0.05_Deg_Monthly_NDVI"
[19] "MOD13C2.A2011182.CMG_0.05_Deg_Monthly_NDVI"
[20] "MOD13C2.A2011213.CMG_0.05_Deg_Monthly_NDVI"
[21] "MOD13C2.A2011244.CMG_0.05_Deg_Monthly_NDVI"
[22] "MOD13C2.A2011274.CMG_0.05_Deg_Monthly_NDVI"
[23] "MOD13C2.A2011305.CMG_0.05_Deg_Monthly_NDVI"
[24] "MOD13C2.A2011335.CMG_0.05_Deg_Monthly_NDVI"
[25] "MOD13C2.A2012001.CMG_0.05_Deg_Monthly_NDVI"
[26] "MOD13C2.A2012032.CMG_0.05_Deg_Monthly_NDVI"
[27] "MOD13C2.A2012061.CMG_0.05_Deg_Monthly_NDVI"
[28] "MOD13C2.A2012092.CMG_0.05_Deg_Monthly_NDVI"
[29] "MOD13C2.A2012122.CMG_0.05_Deg_Monthly_NDVI"
[30] "MOD13C2.A2012153.CMG_0.05_Deg_Monthly_NDVI"
[31] "MOD13C2.A2012183.CMG_0.05_Deg_Monthly_NDVI"
[32] "MOD13C2.A2012214.CMG_0.05_Deg_Monthly_NDVI"
[33] "MOD13C2.A2012245.CMG_0.05_Deg_Monthly_NDVI"
[34] "MOD13C2.A2012275.CMG_0.05_Deg_Monthly_NDVI"
[35] "MOD13C2.A2012306.CMG_0.05_Deg_Monthly_NDVI"
[36] "MOD13C2.A2012336.CMG_0.05_Deg_Monthly_NDVI"
[37] "MOD13C2.A2013001.CMG_0.05_Deg_Monthly_NDVI"
[38] "MOD13C2.A2013032.CMG_0.05_Deg_Monthly_NDVI"
[39] "MOD13C2.A2013060.CMG_0.05_Deg_Monthly_NDVI"
[40] "MOD13C2.A2013091.CMG_0.05_Deg_Monthly_NDVI"
[41] "MOD13C2.A2013121.CMG_0.05_Deg_Monthly_NDVI"
[42] "MOD13C2.A2013152.CMG_0.05_Deg_Monthly_NDVI"
[43] "MOD13C2.A2013182.CMG_0.05_Deg_Monthly_NDVI"
[44] "MOD13C2.A2013213.CMG_0.05_Deg_Monthly_NDVI"
[45] "MOD13C2.A2013244.CMG_0.05_Deg_Monthly_NDVI"
[46] "MOD13C2.A2013274.CMG_0.05_Deg_Monthly_NDVI"
[47] "MOD13C2.A2013305.CMG_0.05_Deg_Monthly_NDVI"
[48] "MOD13C2.A2013335.CMG_0.05_Deg_Monthly_NDVI"
[49] "MOD13C2.A2014001.CMG_0.05_Deg_Monthly_NDVI"
[50] "MOD13C2.A2014032.CMG_0.05_Deg_Monthly_NDVI"
[51] "MOD13C2.A2014060.CMG_0.05_Deg_Monthly_NDVI"
[52] "MOD13C2.A2014091.CMG_0.05_Deg_Monthly_NDVI"
[53] "MOD13C2.A2014121.CMG_0.05_Deg_Monthly_NDVI"
[54] "MOD13C2.A2014152.CMG_0.05_Deg_Monthly_NDVI"
[55] "MOD13C2.A2014182.CMG_0.05_Deg_Monthly_NDVI"
[56] "MOD13C2.A2014213.CMG_0.05_Deg_Monthly_NDVI"
d<-seq(as.Date("2010-01-01"),as.Date("2014-08-01"),by="month")
d
Navinder J Singh
Associate Professor
Department of Wildlife, Fish, and Environmental Studies
Faculty of Forest Sciences
Swedish University of Agricultural Sciences
SE-901 83 Umea, Sweden
O: +46 (0)90 786 8538;
M: +46 (0)70 676 0103
email: navinder.singh at slu.se<mailto:navinder.singh at slu.se>
Web: navinderjsingh.weebly.com<http://navinderjsingh.weebly.com/>
www.slu.se/viltfiskmiljo/navinder_singh<http://www.slu.se/viltfiskmiljo/navinder_singh>
http://slu-se.academia.edu/NSingh
On 21 Oct 2014, at 15:27, Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>> wrote:
Hi
Could it be that you are stacking the tif files in a wrong order. So that it does not correspond to your 'd' vector? Please show us the result from list.files(pattern='NDVI.tif').
I suppose that the file names will tell us something about the date for each particular file. We need to compare that to you 'd' vector.
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
From: Navinder Singh [mailto:Navinder.Singh at slu.se]
Sent: 21. oktober 2014 14:30
To: Frede Aakmann Tøgersen; Oscar Perpiñán Lamigueiro
Cc: r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>
Subject: Re: [R-sig-Geo] Order raster images in a levelplot
Dear Frede,
The problem still persists. It does make a plot, but then summarises using the wrong layers.
setwd("/Users/nasi/Google Drive/5KMNDVI_Monthly")
modis = stack(list.files(pattern='NDVI.tif'))
modis
names(modis)
d<-seq(as.Date("2010-01-01"),as.Date("2014-08-01"),by="month")
d
[1] "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" "2010-05-01" "2010-06-01"
[7] "2010-07-01" "2010-08-01" "2010-09-01" "2010-10-01" "2010-11-01" "2010-12-01"
[13] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01" "2011-06-01"
[19] "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01" "2011-11-01" "2011-12-01"
[25] "2012-01-01" "2012-02-01" "2012-03-01" "2012-04-01" "2012-05-01" "2012-06-01"
[31] "2012-07-01" "2012-08-01" "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01"
[37] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01"
[43] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01"
[49] "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01"
[55] "2014-07-01" "2014-08-01"
names(modis)<-d
NDVImod<-setZ(modis,d, name="months")
names(NDVImod)
[1] "X2010.01.01" "X2010.02.01" "X2010.03.01" "X2010.04.01" "X2010.05.01" "X2010.06.01"
[7] "X2010.07.01" "X2010.08.01" "X2010.09.01" "X2010.10.01" "X2010.11.01" "X2010.12.01"
[13] "X2011.01.01" "X2011.02.01" "X2011.03.01" "X2011.04.01" "X2011.05.01" "X2011.06.01"
[19] "X2011.07.01" "X2011.08.01" "X2011.09.01" "X2011.10.01" "X2011.11.01" "X2011.12.01"
[25] "X2012.01.01" "X2012.02.01" "X2012.03.01" "X2012.04.01" "X2012.05.01" "X2012.06.01"
[31] "X2012.07.01" "X2012.08.01" "X2012.09.01" "X2012.10.01" "X2012.11.01" "X2012.12.01"
[37] "X2013.01.01" "X2013.02.01" "X2013.03.01" "X2013.04.01" "X2013.05.01" "X2013.06.01"
[43] "X2013.07.01" "X2013.08.01" "X2013.09.01" "X2013.10.01" "X2013.11.01" "X2013.12.01"
[49] "X2014.01.01" "X2014.02.01" "X2014.03.01" "X2014.04.01" "X2014.05.01" "X2014.06.01"
[55] "X2014.07.01" "X2014.08.01"
e<-extent(-17.96, 43.135, -30.055, 67.08)
NDVImodis<-crop(NDVImod,e) # cropping the layer to extent
NDVImodis[NDVImodis<0]<-0 # setting values below 0 as 0.
NDVImodis<-setZ(NDVImodis,d,name="months")
year.mon.mean<-zApply(NDVImodis, by=months, fun=mean, name="months")
levelplot(year.mon.mean, main="meanNDVI 2010-2014)", par.settings=myTheme)
everything seems to be going all right with the code, but i am not sure where things are going wrong- in picking out the right months to merge together.
If i were to do it manually- it works very well-
mean.jan<-mean(NDVImodis[[c(1,11,24,36,48)]])
#plot(mean.jan)
mean.feb<-mean(NDVImodis[[c(2,13,25,37,49)]])
mean.mar<-mean(NDVImodis[[c(3,14,26,38,50)]])
mean.apr<-mean(NDVImodis[[c(4,15,27,39,51)]])
mean.may<-mean(NDVImodis[[c(5,16,28,40,52)]])
mean.jun<-mean(NDVImodis[[c(17,29,41,53)]])
mean.jul<-mean(NDVImodis[[c(6,18,30,42,54)]])
mean.aug<-mean(NDVImodis[[c(7,19,31,43,55)]])
mean.sep<-mean(NDVImodis[[c(8,20,32,44)]])
mean.oct<-mean(NDVImodis[[c(9,21,33,45)]])
mean.nov<-mean(NDVImodis[[c(10,22,34,46)]])
mean.dec<-mean(NDVImodis[[c(11,23,35,47)]])
# Means
mean.year.mon<-stack(mean.jan,mean.feb,mean.mar,mean.apr,mean.may,mean.jun,
mean.jul,mean.aug,mean.sep,mean.oct,mean.nov,mean.dec)
levelplot(mean.year.mon, par.settings=myTheme)
Navinder
On 21 Oct 2014, at 13:30, Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>> wrote:
Please remember to post to the list as well since this was there this thread startet.
Dows it have something to do with you not setting the name argument of setZ()?
You are doing:
NDVImodis<-setZ(NDVImodis,d)
names(NDVImodis)<-d
Perhaps you should do:
NDVImodis<-setZ(NDVImodis,d, name = "Months")
And then e.g.
zApply(NDVImodis, by=months, fun=mean, name="Months")
?????
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
From: Navinder Singh [mailto:Navinder.Singh at slu.se]
Sent: 21. oktober 2014 13:22
To: Frede Aakmann Tøgersen
Subject: Re: [R-sig-Geo] Order raster images in a levelplot
Thanks Frede, it worked perfectly.
However, i have problems using the 'by=months', since it is not estimating the monthly means as i want.
i want the monthly means across years.i.e means of january, feb and so on. And 'by=months' is dining some averaging but it is averaging the wrong months. Perhaps it has o do something with my naming? Below is what i did.
I couldn't send you the entire dataset, as they are global images with each 14MB size and they run monthly for 5 years.
I also posted a message on R-sig.
Thanks for your help.
Best wishes,
Navinder
setwd("/Users/nasi/Google Drive/5KMNDVI_Monthly")
modis = stack(list.files(pattern='NDVI.tif'))
modis
names(modis)
d<-seq(as.Date("2010-01-01"),as.Date("2014-08-01"),by="month")
names(modis)<-d
NDVImod<-setZ(modis,d)
e<-extent(-17.96, 43.135, -30.055, 67.08)
NDVImodis<-crop(NDVImod,e)
NDVImodis[NDVImodis<0]<-0
NDVImodis<-setZ(NDVImodis,d)
names(NDVImodis)<-d
# The first problem occurs below. I am trying to get monthly means and other quantities. Although the function does give me an output, but the results are completely wrong. As it seems to be averaging the wrong months together. Can't really paste the image here i suppose.
year.mon.max<-zApply(NDVImodis, by=months, fun=max, name="months")
year.mon.min<-zApply(NDVImodis, by=months, fun=min, name="months")
year.mon.diff<-year.mon.max-year.mon.min
year.mon.sd<-zApply(NDVImodis, by=months, fun=sd, name="months")
year.mon.cv<-zApply(NDVImodis, by=months, fun=cv, name="months")
# I only manage to rearrange this one using the code after these lines. But since the images are wrong, its not of much use. Perhaps my use of 'by' argument above is not correct. Or my naming of the rasters isn't either.
print(levelplot(year.mon.mean[[c(6,2,12,11,10,3,5,4,8,1,9,7)]], main="Mean Monthly NDVI (2010-2014)", par.settings=myTheme))
# perhaps here is where i need to use the par.cond
# I tried using 'index.cond' here like below:
levelplot(year.mon.mean, index.cond=list(c("January","February","March", "April", "May", "June","July", "August", "Septmeber", "Ocotber","November","December")), par.settings=myTheme)
#But this doesn't plot anything. Even the maps disappear. Perhaps this again has to do with the naming in the rasters.
#Anyhow, i would like to match these monthly images with data on spatial observations of animals where i have an x,y and a date matching the date of the raster.
#I tried doing this below -
df$layer<-lapply(df$month, function(x) which(grepl(tolower(x), tolower(names(year.mon.mean)))))
df$meanNDVI<-sapply(seq_len(nrow(df)), function(x) extract(mean.year.mon[[df$layer[x]]], df[x,1:2])) # 1,2 are columns with x and y.
#This also doesn't work, perhaps also has to do with the names in the raster.
Your help is much appreciated.
Ill keep trying the par.cond argument in the meanwhile.
Best wishes,
Navinder
Navinder J Singh
Associate Professor
Department of Wildlife, Fish, and Environmental Studies
Faculty of Forest Sciences
Swedish University of Agricultural Sciences
SE-901 83 Umea, Sweden
O: +46 (0)90 786 8538;
M: +46 (0)70 676 0103
email: navinder.singh at slu.se<mailto:navinder.singh at slu.se>
Web: navinderjsingh.weebly.com<http://navinderjsingh.weebly.com/>
www.slu.se/viltfiskmiljo/navinder_singh<http://www.slu.se/viltfiskmiljo/navinder_singh>
http://slu-se.academia.edu/NSingh
On 21 Oct 2014, at 13:10, Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>> wrote:
Hi
Since I don't have your data I cannot say anything about your use of zApply. For the levelplot() problem I think you can use the layer arguments to levelplot() from rasterVis package. Something like this:
f <- system.file("external/test.grd", package="raster")
d <- seq(as.Date("2010-01-01"),as.Date("2014-08-01"),by="month")
rr <- stack(rep(f,length(d)))
names(rr) <- d
rr <- setZ(rr, d, name = 'Month')
rr.mean <- zApply(rrr, by = months, fun = mean, name = "Month")
levelplot(rr.mean, margin = FALSE, layer = unique(months(d)), layout = c(3,4))
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
From: Navinder Singh [mailto:Navinder.Singh at slu.se]
Sent: 21. oktober 2014 12:45
To: r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>
Cc: oscar.perpinan at upm.es<mailto:oscar.perpinan at upm.es>; Frede Aakmann Tøgersen
Subject: [R-sig-Geo] Order raster images in a levelplot
Dear Both,
Thank you for your responses.
Oscar- I am still trying to get the indexing going. Hope it will be sorted out soon.
Just to provide some more background on my code- here is an example.
library(raster)
library(rasterVis)
library(raster)
library(rgdal)
library(zoo)
setwd("/Users/nasi/Google Drive/5KMNDVI_Monthly")
modis = stack(list.files(pattern='NDVI.tif'))
modis
names(modis)
d<-seq(as.Date("2010-01-01"),as.Date("2014-08-01"),by="month")
names(modis)<-d
NDVImod<-setZ(modis,d)
e<-extent(-17.96, 43.135, -30.055, 67.08)
NDVImodis<-crop(NDVImod,e)
NDVImodis[NDVImodis<0]<-0
NDVImodis<-setZ(NDVImodis,d)
names(NDVImodis)<-d
# The first problem occurs below. I am trying to get monthly means and other quantities. Although the function does give me an output, but the results are completely wrong. As it seems to be averaging the wrong months together. Can't really paste the image here i suppose.
year.mon.max<-zApply(NDVImodis, by=months, fun=max, name="months")
year.mon.min<-zApply(NDVImodis, by=months, fun=min, name="months")
year.mon.diff<-year.mon.max-year.mon.min
year.mon.sd<-zApply(NDVImodis, by=months, fun=sd, name="months")
year.mon.cv<-zApply(NDVImodis, by=months, fun=cv, name="months")
# I only manage to rearrange this one using the code after these lines. But since the images are wrong, its not of much use. Perhaps my use of 'by' argument above is not correct. Or my naming of the rasters isn't either.
print(levelplot(year.mon.mean[[c(6,2,12,11,10,3,5,4,8,1,9,7)]], main="Mean Monthly NDVI (2010-2014)", par.settings=myTheme))
# perhaps here is where i need to use the par.cond
# I tried using 'index.cond' here like below:
levelplot(year.mon.mean, index.cond=list(c("January","February","March", "April", "May", "June","July", "August", "Septmeber", "Ocotber","November", "December")),par.settings=myTheme)
#But this doesn't plot anything. Even the maps disappear. Perhaps this again has to do with the naming in the rasters.
#Anyhow, i would like to match these monthly images with data on spatial observations of animals where i have an x,y and a date matching the date of the raster.
#I tried doing this below -
df$layer<-lapply(df$month, function(x) which(grepl(tolower(x), tolower(names(year.mon.mean)))))
df$meanNDVI<-sapply(seq_len(nrow(df)), function(x) extract(mean.year.mon[[df$layer[x]]], df[x,1:2])) # 1,2 are columns with x and y.
#This also doesn't work, perhaps also has to do with the names in the raster.
Your help is much appreciated.
Ill keep trying the par.cond argument in the meanwhile.
Best wishes,
Navinder
Navinder J Singh
Associate Professor
Department of Wildlife, Fish, and Environmental Studies
Faculty of Forest Sciences
Swedish University of Agricultural Sciences
SE-901 83 Umea, Sweden
O: +46 (0)90 786 8538;
M: +46 (0)70 676 0103
email: navinder.singh at slu.se<mailto:navinder.singh at slu.se>
Web: navinderjsingh.weebly.com<http://navinderjsingh.weebly.com/>
www.slu.se/viltfiskmiljo/navinder_singh<http://www.slu.se/viltfiskmiljo/navinder_singh>
http://slu-se.academia.edu/NSingh
On 21 Oct 2014, at 08:53, Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>> wrote:
Hi
Since no code example was provided we'll have to guess. I suppose it has to do with the way factor() works.
Here is the built-in list of month names in R:
month.name
[1] "January" "February" "March" "April" "May" "June"
[7] "July" "August" "September" "October" "November" "December"
Now show this as a factor:
factor(month.name)
[1] January February March April May June July
[8] August September October November December
12 Levels: April August December February January July June March ... September
See how factor() is using lexicographical order for the labels of the factor levels.
Now do this instead:
factor(month.name, labels = month.name)
[1] May April August January September July June
[8] February December November October March
12 Levels: January February March April May June July August ... December
Now the order of the labels of the levels is the same as that of month.name.
Here is an example:
testdata <- data.frame(x = 1, y = 1, Month = factor(month.name))
### order of panels is that of order(month.name)
xyplot(y ~ x|Month, data = testdata)
testdata <- data.frame(x = 1, y = 1, Month = factor(month.name, labels = month.name))
### order of panels is that of month.name
### January in bottom left and Deccember in top right panel
xyplot(y ~ x|Month, data = testdata)
### use the as.table argument to get January in top left and December in bottom right
xyplot(y ~ x|Month, data = testdata, as.table = TRUE)
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
-----Original Message-----
From: r-sig-geo-bounces at r-project.org<mailto:r-sig-geo-bounces at r-project.org> [mailto:r-sig-geo-bounces at r-
project.org<mailto:r-sig-geo-bounces at r-%0bproject.org>] On Behalf Of Oscar Perpiñán Lamigueiro
Sent: 21. oktober 2014 07:50
To: Navinder Singh
Cc: r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>
Subject: Re: [R-sig-Geo] Order raster images in a levelplot
Hi,
1. Does someone have any suggestions on how i can order the sequence
of images in a "levelplot" made using rasterVis. Specifically, i
have months i need to arrange in the normal sequence, as the plot
currently aligns them in an alphabetical order. Does "levelplot"
works the same way as xyplots in lattice?
levelplot methods defined in rasterVis are based on lattice graphics.
Most of the arguments provided by lattice::xyplot and
lattice::levelplot are also available. For this issue you could try
these arguments: perm.cond, index.cond, as.table. All of them are
described in the help page of lattice::xyplot.
2. Have there been more developments on the zApply function in the
raster package? I am trying to make basic statistical summaries for
mean monthly (eg. NDVI values) form a multi annual time series. How
does one go about using the "by" Indexing in this case.
Could you post a reproducible example? I have used zApply for similar
tasks without problems.
Best,
Oscar.
--
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://oscarperpinan.github.io<http://oscarperpinan.github.io/>
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list