Search code examples
plotpolygonspatstat

Represent a colored polygon in ggplot2


I am using the statspat package because I am working on spatial patterns. I would like to do in ggplot and with colors instead of numbers (because it is not too readable), the following graph, produced with the plot.quadratest function: Polygone

The numbers that interest me for the intensity of the colors are those at the bottom of each box.
The test object contains the following data: Test object
I have looked at the help of the function, as well as the code of the function but I still cannot manage it.
Ideally I would like my final figure to look like this (maybe not with the same colors haha):

Final object

Thanks in advance for your help.


Solution

  • Please provide a reproducible example in the future. The package reprex may be very helpful.

    To use ggplot2 for this my best bet would be to convert spatstat objects to sf and do the plotting that way, but it may take some time. If you are willing to use base graphics and spatstat you could do something like:

    library(spatstat)
    # Data (using a built-in dataset):
    X <- unmark(chorley)
    plot(X, main = "")
    

    # Test:
    test <- quadrat.test(X, nx = 4)
    # Default plot:
    plot(test, main = "")
    

    # Extract the the `quadratcount` object (regions with observed counts):
    counts <- attr(test, "quadratcount")
    # Convert to `tess` (raw regions with no numbers)
    regions <- as.tess(counts)
    # Add residuals as marks to the tessellation:
    marks(regions) <- test$residuals
    # Plot regions with marks as colors:
    plot(regions, do.col = TRUE, main = "")