Search code examples
rplotlabellegendspatstat

How to make a better plot with label for marked point patterns using spatstat in R


I have marked point pattern data data. I want to plot the marked point pattern where the marks are only valued at 1 and 0 hence there are two colours only. This is what I have done so far

x = c(3,0.5,1,0,0,0,2,2.5,5.5, 16,21,26,28,40, 47, 52, 58, 60, 65, 63, 63, 75, 77, 78, 75)
y = c(116,106,82.5,64,40,35,25,17.5,5,5,5,8,10,8, 14, 14, 10, 0, 0, 17, 20, 24, 30, 50, 116)
p <- owin(poly = cbind(x, y))
point_pattern = as.ppp(deviation_binary_locations_marks, p)
marks(point_pattern) <- deviation_binary_locations_marks[, "RPL32"]
colour <- c("green","black")[marks(point_pattern)]
plot(point_pattern,which.marks="RPL32", bg=colour, main="Point pattern for gene RPL32")

and this is the plot I got gene RPL32

The circle size represents the mark values of either 1 or 0, and the colour represents the values correlated to the circle (green if the circle is 1, and black if the circle is 0). I also want it only shows two sizes of circles (value 1 and 0 since the marks are only 1 and 0). However, the attributes show multiple circle sizes from 0 to 1. How can I do that? Is there a way to make the plot looks better as well?


Solution

  • The help file for plot.ppp explains that, if the marks are numeric values, the plot command will draw symbols of different sizes proportional to the numeric values. This has happened in your example because the code has detected that deviation_binary_locations_marks[, "RPL32"] is a numeric vector.

    In your example, you know that this vector contains only the values 0 and 1, and you want these two values to be represented using different plot characters. To do this you need to convert the marks to categorical values (factor values) which you can do simply as:

    m <- as.integer(deviation_binary_locations_marks[, "RPL32"])
    marks(point_pattern) <- factor(m, levels=c(0,1))