How to use the image function in R, When displaying spatial data (pictures), the image function can be used to generate a grid of coloured rectangles based on the values of the z matrix.
The grid on which the values of z are measured can be specified using the variables x and y.
# Data x <- -10:10 y <- -10:10 z <- sqrt(outer(x ^ 2, y ^ 2, "+")) image(x, y, z) # You can also type, the following # but the axes will be between 0 and 1 image(z)
The image’s colour scheme can be altered with the col argument. A function like gray.colors, topo.colors, hcl.colors, or a related function can be passed as a variable. hcl.colors(12, “YlOrRd”, rev = TRUE) is the default value.
image(x, y, z, col = gray.colors(12))
Keep in mind that the colour image will become smoother as you increase the amount of values.
Overlaying a contour
By supplying the same data to the function and specifying add = TRUE, it is feasible to overlay the contour lines over the colour image.
image(x, y, z) contour(x, y, z, add = TRUE)