Dear List,

Thank you Robert for your answer, I've found that my white lines were 
coming from an inappropriate numbers of ncol/nrow.
Looking at the 'brick' help page I've found another solution:

I put all the data in one data frame ('y70') with columns 3 and 4 being 
the coordinates of the center of the grid cells and all the later 
columns being monthly mean temperature from January to December.

pixel<-SpatialPixelsDataFrame(y70[,3:4],y70[,5:16],proj4string=CRS("+proj=longlat 
+datum=WGS84"))
b<-brick(pixel)
b
plot(b)

This solution avoid junggling around with the nrow/ncol numbers.

  Thanks again.
Lionel


Le 02/02/2013 05:30, Robert J. Hijmans a écrit :
> Lionel,
>
> The CRU data is gridded. Therefore, your approach to rasterize points 
> seems odd. Here is a sketch of what I think you should do:
>
> library(raster)
> r <- raster(xmn=-11, xmx=32, ymn=34, ymx=72, ncol=258, nrow=228)
> # or if you want multiple layers, e.g. 12
> b <- brick(xmn=-11, xmx=32, ymn=34, ymx=72, ncol=258, nrow=228, nl=12)
>
> cells <- cellFromXY(r, coords)
> b[coords] <- values
>
> where in this case values is a matrix with nl columns and nrow(coords) 
> rows
>
> Robert
>
>
> On Fri, Feb 1, 2013 at 8:30 AM, Lionel <s6lihert@uni-bonn.de 
> <mailto:s6lihert@uni-bonn.de>> wrote:
>
>     Jon,
>
>     Thank you very much for your reply/time spent thinking on the issue.
>     Unfortunately since I'm working on climatic time-series it's
>     rather hard
>     to find relevant data in the built-in datasets. Here is a link to the
>     dataset
>     (http://www.cru.uea.ac.uk/cru/data/hrg/timm/grid/CRU_TS_1_2.htm
>     <http://www.cru.uea.ac.uk/cru/data/hrg/timm/grid/CRU_TS_1_2.html>l, I
>     don't know if I can attached files to this mailing list I will check
>     that), then here is the all code used and the results from a
>     R.version call;
>     temp<-readLines("path_to_file/obs.1901-2000.tmp")
>     coords<-grep("Grid-ref",temp,value=TRUE)
>     coord<-gsub("^\\w+\\W\\w+\\W\\s+(\\d+)\\D\\s+(\\d+)$","\\1,\\2",coords,perl=TRUE)
>     coord<-unlist(strsplit(coord,","))
>     coord<-as.numeric(coord)
>     coord<-matrix(coord,ncol=2,byrow=TRUE)
>     coord<-as.data.frame(coord)
>     coord$Long<-(-11.0833334)+(coord$V1*0.1666667)
>     coord$Lat<-(34.0833334)+(coord$V2*0.1666667)
>     coord$Values<-rnorm(length(coord[,1]))
>     point<-coord[,3:5]
>     coordinates(point)<-~Long+Lat
>     proj4string(point)<-CRS("+proj=longlat +datum=WGS84")
>     r<-raster(ncols=(bbox(point)[1,2]-bbox(point)[1,1])/0.1666667,nrows=(bbox(point)[2,2]-bbox(point)[2,1])/0.1666667,xmn=bbox(point)[1,1],xmx=bbox(point)[1,2],ymn=bbox(point)[2,1],ymx=bbox(point)[2,2])
>     ras<-rasterize(point,r)
>     plot(ras)
>     R.version
>     platform       i686-pc-linux-gnu
>     arch           i686
>     os             linux-gnu
>     system         i686, linux-gnu
>     status
>     major          2
>     minor          15.0
>     year           2012
>     month          03
>     day            30
>     svn rev        58871
>     language       R
>     version.string R version 2.15.0 (2012-03-30)
>     nickname
>
>     Thank you for your time,
>     Lionel
>
>     On 01/02/2013 15:46, Jon Olav Skoien wrote:
>     > Lionel,
>     >
>     > You are not providing a reproducible example (y70 is not given), and
>     > you are not telling the versions of R and the packages you are
>     using,
>     > so I am not sure if my comments here will solve your case.
>     >
>     > 1. The issue with the lines is difficult to answer without knowing
>     > more, but it could be a version issue. At least one of the older
>     > R-versions (I think it was one of the 2.14-versions) tended to add
>     > white lines to many gridded plots. Try with a more current
>     version if
>     > this could be your case.
>     >
>     > 2. For the disappearing points, you might end up with more than
>     > observation location in some of the raster cells. rasterize will
>     > compute the cell value from the fun argument, and rasterToPoints
>     will
>     > only return one point back. In the example below, 10 rows of meuse
>     > have been duplicated before the second call to rasterize and
>     > rasterToPoints. You can see that it returns only the original 155
>     > points in this case. If meuse.grid had been coarser, also mnew would
>     > have fewer point than the original meuse-object. You can check how
>     > many points you have within the distance of a grid cell with
>     zerodist().
>     >
>     > loadMeuse()
>     > r = raster(meuse.grid)
>     > ras = rasterize(meuse, r)
>     > mnew = rasterToPoints(ras, spatial = TRUE)
>     > summary(meuse)
>     >
>     > meuse = rbind(meuse, meuse[1:10,])
>     > ras = rasterize(meuse, r)
>     > mnew2 = rasterToPoints(ras, spatial = TRUE)
>     > summary(meuse)
>     > summary(mnew)
>     > summary(mnew2)
>     >
>     > Cheers,
>     > Jon
>     >
>     >
>     > On 01-Feb-13 13:32, Lionel Hertzog wrote:
>     >> Dear List,
>     >>
>     >> I am having troubles trying to turn a spatialpointsdataframe into a
>     >> raster, when I use the rasterize function from the raster
>     packages I get
>     >> strange white lines in certain area of the map and when turning
>     values
>     >> back to spatialpointsdataframe some of them disappeared. Below
>     is a code
>     >> example
>     >>
>     >> #create a list of points from coordinates of grid points in
>     europe at a
>     >> 10 arc-minute resolution
>     >>
>     test_point<-data.frame(Long=y70$Long,Lat=y70$Lat,Values=rnorm(length(y70[,1])))
>     >>
>     >> coordinates(test_point)<-~Long+Lat
>     >> proj4string(test_point)<-CRS("+proj=longlat +datum=WGS84")
>     >> summary(test_point)
>     >> Object of class SpatialPointsDataFrame
>     >> Coordinates:
>     >>     min      max
>     >> Long -10.41667 31.91668
>     >> Lat   34.25000 71.25001
>     >> Is projected: FALSE
>     >> proj4string : [+proj=longlat +datum=WGS84]
>     >> Number of points: 31143
>     >> Data attributes:
>     >>     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
>     >> -4.278000 -0.672700 -0.003040 -0.000353  0.668600  3.915000
>     >> #create a raster from the points extent and resolution (since 60
>     >> arc-minutes make a degree, we have a resolution of #10/60)
>     >>
>     r<-raster(nrows=(bbox(test_point)[2,2]-bbox(test_point)[2,1])/0.1666667,ncols=(bbox(test_point)[1,2]-bbox(test_point)[1,1])/0.1666667,xmn=bbox(test_point)[1,1],xmx=bbox(test_point)[1,2],ymn=bbox(test_point)[2,1],ymx=bbox(test_point)[2,2],crs=CRS("+proj=longlat
>     >>
>     >> +datum=WGS84"))
>     >> #then rasterize the points
>     >> ras<-rasterize(x=test_point,y=r)
>     >> #then when ploting white lines appears
>     >> plot(ras)
>     >> #and if we retrieve the non-NA values from this raster
>     >> new_points<-rasterToPoints(ras,spatial=TRUE)
>     >> summary(new_points)
>     >> Object of class SpatialPointsDataFrame
>     >> Coordinates:
>     >>     min      max
>     >> x -10.33333 31.83334
>     >> y  34.33333 71.16667
>     >> Is projected: FALSE
>     >> proj4string : [+proj=longlat +datum=WGS84]
>     >> Number of points: 29373
>     >> Data attributes:
>     >>     Min. 1st Qu.  Median    Mean 3rd Qu.  Max.
>     >> 1    7515   15420   15410   23220   31140
>     >>
>     >> the number of points went from 31143 to 29373, what is the problem?
>     >> Thank you,
>     >
>     >
>
>
>             [[alternative HTML version deleted]]
>
>     _______________________________________________
>     R-sig-Geo mailing list
>     R-sig-Geo@r-project.org <mailto:R-sig-Geo@r-project.org>
>     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>


	[[alternative HTML version deleted]]

