shear_2d {rearrr} | R Documentation |
Shear the values around an origin in 2 dimensions
Description
Shear a set of 2d points around an origin. The shearing formulas (excluding the origin movements) is:
x' = x + x_shear * y
y' = y + y_shear * x
The data points in `data`
are moved prior to the shearing, to bring
the origin to 0
in all dimensions. After the shearing, the
inverse move is applied to bring the origin back to its original position.
The origin can be supplied as coordinates or as a function that returns coordinates. The
latter can be useful when supplying a grouped data.frame
and shearing around e.g. the centroid
of each group.
Usage
shear_2d(
data,
x_shear,
y_shear = 0,
x_col = NULL,
y_col = NULL,
suffix = "_sheared",
origin = NULL,
origin_fn = NULL,
keep_original = TRUE,
shear_col_name = ".shear",
origin_col_name = ".origin",
overwrite = FALSE
)
Arguments
data |
|
x_shear |
Shear factor for the x dimension ( |
y_shear |
Shear factor for the y dimension ( |
x_col |
Name of x column in |
y_col |
Name of y column in |
suffix |
Suffix to add to the names of the generated columns. Use an empty string (i.e. |
origin |
Coordinates of the origin to shear around.
|
origin_fn |
Function for finding the origin coordinates. Input: Each column will be passed as a Output: A Can be created with E.g. Built-in functions are |
keep_original |
Whether to keep the original columns. (Logical) Some columns may have been overwritten, in which case only the newest versions are returned. |
shear_col_name |
Name of new column with the shearing factors.
If Also adds a string version with the same name + |
origin_col_name |
Name of new column with the origin coordinates.
If |
overwrite |
Whether to allow overwriting of existing columns. (Logical) |
Value
data.frame
(tibble
) with sheared columns,
the shearing factors and the origin coordinates.
Author(s)
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
See Also
Other mutate functions:
apply_transformation_matrix()
,
cluster_groups()
,
dim_values()
,
expand_distances()
,
expand_distances_each()
,
flip_values()
,
roll_values()
,
rotate_2d()
,
rotate_3d()
,
shear_3d()
,
swirl_2d()
,
swirl_3d()
Other shearing functions:
shear_3d()
Examples
# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Create a data frame
df <- data.frame(
"x" = rep(1:6, each = 2),
"y" = rep(c(1, 4), 6),
"g" = rep(1:2, each = 6)
)
# Shear the x variable with regards to y
# around the centroid
df_sheared <- shear_2d(
data = df,
x_shear = 2.5,
x_col = "x",
y_col = "y",
origin_fn = centroid
)
# Plot sheared data
# Black: original points
# Red: sheared points
if (has_ggplot){
df_sheared %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_point(aes(x = x_sheared, y = y_sheared, color = "red")) +
theme_minimal()
}
# Shear in both dimensions
df_sheared <- shear_2d(
data = df,
x_shear = 2.5,
y_shear = 2.5,
x_col = "x",
y_col = "y",
origin_fn = centroid
)
# Plot sheared data
# Black: original points
# Red: sheared points
if (has_ggplot){
df_sheared %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_point(aes(x = x_sheared,y = y_sheared, color = "red")) +
theme_minimal()
}
# Shear grouped data frame
# Affects the calculated origin
df_sheared <- shear_2d(
data = dplyr::group_by(df, g),
x_shear = 2.5,
x_col = "x",
y_col = "y",
origin_fn = centroid
)
# Plot sheared data
# Black: original points
# Red: sheared points
if (has_ggplot){
df_sheared %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_point(aes(x = x_sheared, y = y_sheared, color = "red")) +
theme_minimal()
}
# Shear a vector with multiple shearing factors
shear_2d(
data = c(1:10),
x_shear = c(1, 1.5, 2, 2.5),
origin = c(0, 0)
)