How to modify the scale of x and y axes of image pixel plot object from spatstat package in R - r

I have this image pixel object, created using Spatstat package in R. The range of x-axis is [0,24], the range of y-axis is [28000, 29500]. When I plot it out in R studio, the graph looks very narrow as below:
I guess it is because the range of x-axis is too small compared to y's. What should I do to make the plot wider?

In the spatstat package, spatial objects are always plotted isometrically. The plot uses the same physical scale for the x and y axes.
Spatial objects in spatstat occupy a specific location and size in two-dimensional space. A pixel image (object of class "im") is associated with a rectangular region at a specific location in two-dimensional space with a specific width and height. If this region is long and thin, then the plotted image will be displayed as long and thin, and will not be rescaled to fit the screen or page.
If you want to stretch a pixel image in spatstat, you need to transform it to another image, which will be defined on a different region of two-dimensional space.
You can do that with the spatstat function affine. In your example, if Z is your original pixel image, you could do
A <- affine(Z, diag(c(60,1)))
The second argument is the transformation matrix, which in this case is just a stretch of the x axis by a factor of 60. (The transformation will stretch the domain of the image, and stretch each individual pixel, by a factor of 60 in the x direction. The number of pixels and the pixel values will be unchanged. No interpolation or other fudging will occur.)
See Chapters 3 and 4 in the spatstat book

Related

Scale an SVG object along a user-defined axis in Illustrator

I have an object (see orange arc below) within an SVG that I would like to scale along the axis of the object itself.
I know the "Transform" options in Illustrator allow you to scale along the X or Y axis, but that doesn't work for me. I want to scale the object such that at each cross-section of the arc (depicted as the black lines in the photo), the object is exactly 10% of the width of the original. Therefore, the axis that I want to scale along is the hypothetical curved line which passes perpendicularly through all of the black lines in the photo.
If done correctly, this should reduce the X and Y dimensions of the object only slightly, but will reduce the orange-colored area to be 10% of the original.

Analyse Pixel distribution of a Rasterlayer

I really really need some advice. I have a Raster with many pixels. Each pixel has one value. Now I want to do a spatial analysis of these pixels. I want to see in which region have the most pixels and were not. Sounds simple, but it's not.
I had an idea to do this with the kernal density but it does not work with rasterlayer. It doesn't work either with ppp, because you can't transform a raster into this data type. I'm really lost. I don't know what could work. So I would be very grateful if I could get some help.
My Pixels looks like this:
There must be a way to show the regions with the most pixels and so on. But I don't know how I can do that.
Short answer: convert your raster object to a pixel image of class im in the spatstat package. Then use Smooth.im. Example:
library(spatstat)
Z <- as.im(my_raster_data)
S <- Smooth(Z)
plot(S)
Long answer: you're using the term "pixel" in a nonstandard sense. The pixels are the small squares which make up the image. Your illustration shows a pixel image in which the majority of the pixels have the value 0 (represented by white colour), but a substantial number of individual pixels have values greater than 0 (ranging from 0 to 0.3).
If I understand correctly, you would like to generate a colour image or heat map which has a brighter/warmer colour in those places where more of the pixels have positive values.
The simplest way is to use Gaussian smoothing of the pixel values in the image. This will calculate a spatially-varying average of the values of the nearby pixels, including the zero pixels. To do this, convert the raster to a pixel image of class im in the spatstat package
Z <- as.im(my_raster_object)
then apply Smooth.im
S <- Smooth(Z)
plot(S)
Look at the help for Smooth.im for options to control the degree of smoothing.
If you wanted to ignore the actual colours (pixel values) in the input data, you could just transform them to binary values before smoothing:
B <- (Z > 0)
SB <- Smooth(B)
plot(SB)

Masking a low quality raster with polygons, getting weird overhang

I am pretty new to R, and have been attempting to use the mask function on a raster image of 250mx250m resolution. My problem is that for some reason I am getting overhang, as there are pixels which lie both inside and outside of the polygon. Is there a way to tighten the tolerance level of mask so that only the pixels within a certain percentage inside the polygon are accepted?
green is my polygon, blue is the resulting mask
I am guessing that you are using the rasterize function from the raster package.
The grid cells are rather large relative to the polygons you are using. rasterize uses the center of the cell to determine if it is covered. However, if you use argument getCover=TRUE you will get a value between 1 to 100 indicating the percentage of each cell that is covered. You could then use a threshold of your choice.
Source: Masking low quality raster with polygons in R gives weird overhang?

scatterplot3d package; how do you re-size a regression plane

I am interested in generating a series of simple 3d scatterplots which include regression planes without interactions using the scatterplot3d function in R. The following code generates almost what I am after with one problem- in many cases the regression plane extends outside of the bounding box (e.g. in this case, the corner nearest x, y and z =0). I tried changing the axis limits to increase the box size, but this does not alter the axis ranges as specified (which, according to the package documentation is an unfixed bug). Is there a way to either 1) re-draw the box to include the entire plane or 2) shrink the plane to include only the portion within the box?
example data
bugs<-c(335.20,8.68,1.94,3.22,21.79,11.16,1618.00,108.76,250.59,400.81,233.86,15.05,274.62,419.21)
max_dq<-c(0.015,0.001,0.001,0.001,0.002,0.007,0.04,0.001,0.014,0.003,0.002,0.006,0.004,0.013)
since_dist<-c(21,58,5,1,1,19,42,33,22,300,240,79,327,42)
library(scatterplot3d)
3 d plot
reg_plt<-scatterplot3d(max_dq,since_dist,bugs,angle=50)
regression plane
reg_plt$plane3d(lm(bugs~max_dq+since_dist))

Compute a radius scale factor to construct n-sided reqular convex polygons of equal area

I have a computer graphics plotting application where we often plot regular convex polygon shapes as symbols for different data points. I'd like to scale the radius (aka circumradius, distance from center to vertex) of the polygons so that polygons with different numbers of sides all have equal area (so presumably similar perceptual impact). i.e. if a circle with radius=1 has area Pi*radius^2, how much do I need to scale the radius to get a square or a triangle with the same area? What would the formula be to compute this for regular polygons with arbitrary numbers of sides?
Seems like this should be a simple geometry/algebra problem, but that was a long time ago... :-)
Using the formula below (taken from this site):
one can derive that:
R = sqrt(2*area / (N*sin(2*pi/N)))

Resources