Search code examples
rreshaperaster

Converting table with coordinates raster plot


I'm a total beginner and have what I believe is a rather simple problem, but I am kind of stuck...

I have a data table with three columns: x, y and height values. I've imported the csv file and extracted the values:

data <- read.csv("C:/Data/heights.csv")
mydata <- data[, c("x", "y", "height")]

I then need to put the height values into a grid with these coordinates, which I created as a raster:

grid <- raster(ncol=2001, nrow=2001, xmn=479975, xmx=500025, ymn=119975, ymx=140025)

Solution

  • I think you are very close. I'd suggest you reading the raster package material, the vignette and description.

    You'll have to pay attention to your data and how it is organized. By row? Column? raster cells values will be populated by row.

    Using something simmilar to the example provided in the help ?raster

    library(raster)
    vals <- 1:100
    r1 <- raster(nrows=10, ncols=10, xmn=479975, xmx=500025, ymn=119975, ymx=140025)
    r1[] <- vals
    plot(r1)
    

    enter image description here

    But the procedure may be different approaching with a spatial object

    library(sp)
    x <- 1:10
    y <- 1:10
    df <- data.frame(expand.grid(x=x,y=y), v=1:100)
    coordinates(df)=~x+y
    gridded(df) = TRUE
    r2 <- raster(df)
    plot(r2)
    

    raster from a spatial object

    I think this may be the best approach but it would be better to have an idea about your data and how it is organized.