How to use the image function in R

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.

ggdogs on ggplot2 – Data Science Tutorials

# 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)

How to perform the Kruskal-Wallis test in R? – Data Science Tutorials

Color customization

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.

Best Books on Data Science with Python – Data Science Tutorials

image(x, y, z)
contour(x, y, z, add = TRUE)

How to Use “not in” operator in Filter – Data Science Tutorials

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × two =