[R] How to make a raster image in R from my own data set

Jon Olav Skoien jon.skoien at jrc.ec.europa.eu
Fri Apr 26 13:07:56 CEST 2013


Hi Kristi,

it takes a few extra steps to create a raster layer from your example 
data set, as it is not a gridded map in Lat lon (probably in some 
projection though). How exactly to do it depends on your data, but here 
are some hints:

1. If you actually need to read the data set from a link, then 
read.table.url is depreceted, just use read.table. You might need to 
call setInternet2(TRUE) first, as the example data is on an https-url.
2. Raster can read a range of inputs, but I am not sure if .csv is one 
of them, and definitely not if the data is not gridded. You can then 
first do interpolation with a Spatial*-object. Set the coordinates of 
your object, this creates a Spatial*-object, and add the projection:
coordinates(pts) = ~lon+lat
proj4string(pts) = CRS("+proj=longlat +datum=WGS84")
3. You will have to create your reference grid (spsample, from raster, 
or another existing grid you have available), and interpolate to this 
grid, using one of the many interpolation packages, such as geoR, 
automap, gstat, intamap.
4. The resulting object can easily be converted to raster through 
raster(interpolationResult[,resultname])

I hope this can help you getting started.
Generally you get quicker response to spatial questions from the 
r-sig-geo list.

Jon



On 24-Apr-13 16:56, Kristi Glover wrote:
> Hi R-user,
> I was trying to make a raster map with WGS84 projection in R, but I could not make it. I found one data set in Google that data is almost the same format as of mine. I wanted to make a raster map of temperature with 1 degree spatial resolution for the global scale.
> I could make it in GIS software but I do have many variables (to be many raster images) and ultimately I am importing them to R for further analysis. Therefore, I wanted to make them in R, if possible.
>
> It would be great if you give some hints on how script look like  in creating a raster map from my own data set (I have provided link for your references, this is an example data set).
>
> I am really appropriating for your help.
>
> #--------------------------------------------------
> #create a raster map from scratch
>
> install.packages("raster", dependencies=TRUE)
> library(raster)  # raster data
> install.packages("rgdal", dependencies=TRUE)
> library(rgdal)  # input/output, projections
> install.packages("rgeos", dependencies=TRUE)
> library(rgeos)  # geometry ops
> install.packages("spdep", dependencies=TRUE)
> library(spdep)  # spatial dependence
> install.packages("pastecs", dependencies=TRUE)
> library(pastecs)
> pts<-read.table.url("https://www.betydb.org//miscanthusyield.csv", header=T, sep=",")
> proj4string(pts)=<- CRS("+proj=longlat +datum=WGS84")
> #---------------------------------------------------------------------------
>
> Cheers,
> Kristi
>   		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Jon Olav Skøien
Joint Research Centre - European Commission
Institute for Environment and Sustainability (IES)
Land Resource Management Unit

Via Fermi 2749, TP 440,  I-21027 Ispra (VA), ITALY

jon.skoien at jrc.ec.europa.eu
Tel:  +39 0332 789206

Disclaimer: Views expressed in this email are those of the individual and do not necessarily represent official views of the European Commission.



More information about the R-help mailing list