[R-sig-Geo] Order raster images in a levelplot
Frede Aakmann Tøgersen
frtog at vestas.com
Tue Oct 21 13:10:27 CEST 2014
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
Cc: 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
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
_______________________________________________
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