[R-sig-Geo] Overlaying time designated spatial points from a data frame to match the time of a raster image in a rasterStack
Frede Aakmann Tøgersen
frtog at vestas.com
Thu Oct 23 08:18:37 CEST 2014
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
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
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] On Behalf Of Navinder Singh
> Sent: 22. oktober 2014 23:57
> To: r-sig-geo at r-project.org
> Cc: 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>
>
>
>
>
>
> [[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list