[R-sig-Geo] Overlaying time designated spatial points from a data frame to match the time of a raster image in a rasterStack

Navinder Singh Navinder.Singh at slu.se
Thu Oct 23 08:52:30 CEST 2014


Works great Frede. Didn’t think that xyplot could just be overlaid like this on a raster stack. Got carried away by the example Oscar has in the rasterVis tutorial, where "sp. points" is used within a layer.
Thanks a lot.
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 23 Oct 2014, at 08:18, Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>> wrote:

Hi

Is it something like this you want to have?


library(raster)
library(latticeExtra)

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(rr, by = function(x)format(x, '%m'), fun = mean, name = "Month")
names(rr.mean) <- month.abb

### For each date sample 10 points from the rasterbrick
pdat <- NULL
for (i in 1:length(d)){
   cells <- sample(1:ncell(rr[[1]]), 10)
   xy <- xyFromCell(rr, cells)
   tmp <- data.frame(x = xy[,"x"], y = xy[, "y"], Month = format(d[i], '%m'), CellId = cells)
   pdat <- rbind(pdat, tmp)
}

levelplot(rr.mean) + xyplot(y ~ x|Month, data = pdat, pch = 13, col = "black")

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<http://project.org/>] On Behalf Of Navinder Singh
Sent: 22. oktober 2014 23:57
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>
Subject: [R-sig-Geo] Overlaying time designated spatial points from a data
frame to match the time of a raster image in a rasterStack

Hi,

I have a level plot with a number of rasters (monthly rasters for a year-
generated with kind help from Oscar). Basically a raster stack.

I would like to overlay spatial points on this raster stack, but would like to
overlay only those points that belong to the month matching the raster stack.
For e.g. The points form January should fall on the raster from January, those
from february should fall on February raster as so on. I am drowning in the
�lattice' literature but have not succeeded till now.

I am running the following code but not sure how to assign an index which
can delegate the points from respective months to their rasters in the layer +
sp. points argument from rasterVis package.

My data frame has the following columns:
df= X, Y, date (POSIXct), month (month.abb), id (animalID), SITE (a factor
with origin area)

The rasters also have names in the format of month.abb.

And the code i am using is:

rrMean is a raster stack of monthly means. dfSp is a SpatialPointDataFrame
with above fields.

p<-levelplot(rrMean, main="Mean Monthly NDVI (2010-2014)",
par.settings=myTheme)
p+layer(sp.points(dfSp,col=dfSp$SITE))

# I understand that col argument just changes the colours but still plots all
points on all rasters, but i guess i need something specified
in sp.points(dfSp,col=dfSp$SITE).

Thanks for any suggestions.

Best wishes,

Navinder
email: navinder.singh at slu.se<mailto:navinder.singh at slu.se><mailto:navinder.singh at slu.se>





[[alternative HTML version deleted]]


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list