Search code examples
rplotspatialgridlines

R: Is it possible to plot a grid from x, y spatial coordinates?


I've been working with a spatial model which contains 21,000 grid cells of unequal size (i by j, where i is [1:175] and j is[1:120]). I have the latitude and longitude values in two seperate arrays (lat_array,lon_array) of i and j dimensions.

Plotting the coordinates:

> plot(lon_array, lat_array, main='Grid Coordinates')

Result: enter image description here My question: Is it possible to plot these spatial coordinates as a grid rather than as points? Does anyone know of a package or function that might be able to do this? I haven't been able to find anything online to this nature.

Thanks.


Solution

  • First of all it is always a bit dangerous to plot inherently spherical coordinates (lat,long) directly in the plane. Usually you should project them in some way, but I will leave it for you to explore the sp package and the function spTransform or something like that.

    I guess in principle you could simply use the deldir package to calculate the Dirichlet tessellation of you points which would give you a nice grid. However, you need a bounding region for this to avoid large cells radiating out from the border of your region. I personally use spatstat to call deldir so I can't give you the direct commands in deldir, but in spatstat I would do something like:

    library(spatstat)
    plot(lon_array, lat_array, main='Grid Coordinates')
    W <- clickpoly(add = TRUE) # Now click the region that contains your grid
    i_na <- is.na(lon_array) | is.na(lat_array) # Index of NAs
    X <- ppp(lon_array[!i_na], lat_array[!i_na], window = W)
    grid <- dirichlet(X)
    plot(grid)
    

    I have not tested this yet and I will update this answer once I get the chance to test it with some artificial data. A major problem is the size of your dataset which may take a long time to calculate the Dirichlet tessellation of. I have only tried to call dirichlet on dataset of size up to 3000 points...