So I've created a file that reads into a data frame in the same way as yours:
> str(v2)
'data.frame': 360 obs. of 720 variables:
BUT data.frame isn't really the right thing here. Its really meant for record-oriented data, where each row is a record and each column is a potentially different variable for that record (eg each row is a person, the columns are name, age, height, etc).
So you really only need to scan
the data in as one long vector and feed it to a raster.
Step 1, define an empty raster of the right size and shape (note I'm assuming the raster covers the whole world, so the limits are not the cell centres):
> m2=raster(nrow=360,ncol=720,xmn=-180,xmx=180,ymn=-90,ymx=90)
Step 2, read numeric values into the raster data slot:
> m2[]=scan("d.txt",what=1)
Read 259200 items
And give it a projection if needed:
> projection(m2)="+init=epsg:4326"
> plot(m2)
If you want to check that the resolution and the cell centres are as expected, use these functions:
> res(m2)
[1] 0.5 0.5
> xFromCol(m2,1:10)
[1] -179.75 -179.25 -178.75 -178.25 -177.75 -177.25 -176.75 -176.25 -175.75
[10] -175.25
> yFromRow(m2,1:10)
[1] 89.75 89.25 88.75 88.25 87.75 87.25 86.75 86.25 85.75 85.25
which shows the resolution is half a degree and the cell centres (or at least the first 10) are at those specified coordinates.
They are the values in the file. How they correspond to measurements is unknown, and would have to be specified in external metadata.
A GeoTIFF can easily store decimal numbers in bands, and you can get the range simply enough. Here I read in a 3 band raster using stack
and check the first band:
> dd = stack("d.tif")
> range(values(dd)[,1])
[1] 0.0000 801.1061
>
If you have information that says how you map your band-1 values to measurements then you just have to operate on the raster values. For example, to scale values from 0 to max(d1)
to 0 to 2*pi
:
d1 = dd[[1]] # get first band
d1 = d1 * 2 * pi / max(values(d1))
If your TIFF is one byte per value, and you want to scale it to 1 to 20, for example:
d1 = 1 + d1 * (19/255)
should do it.
Best Answer
There are several options depending on which R package you want to use. The following links should give you some insights into some of the possibilities available.
add.pie
function (see this worked example and map below)(Map Sourced from: http://www.molecularecologist.com/2012/09/making-maps-with-r/)