rotate_2d {rearrr} | R Documentation |
Rotate the values around an origin in 2 dimensions
Description
The values are rotated counterclockwise around a specified origin.
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 rotating around e.g. the centroid
of each group.
Usage
rotate_2d(
data,
degrees,
x_col = NULL,
y_col = NULL,
suffix = "_rotated",
origin = NULL,
origin_fn = NULL,
keep_original = TRUE,
degrees_col_name = ".degrees",
origin_col_name = ".origin",
overwrite = FALSE
)
Arguments
data |
|
degrees |
Degrees to rotate values counterclockwise. In |
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 rotate around.
A |
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. |
degrees_col_name |
Name of new column with the degrees. If |
origin_col_name |
Name of new column with the origin coordinates. If |
overwrite |
Whether to allow overwriting of existing columns. (Logical) |
Details
Applies the following rotation matrix:
[ cos \theta | , -sin \theta | ] |
[ sin \theta | , cos \theta | ] |
That is:
x' = x cos \theta - y sin \theta
y' = x sin \theta + y cos \theta
Where \theta
is the angle in radians.
As specified at Wikipedia/Rotation_matrix.
Value
data.frame
(tibble
) with seven new columns containing
the rotated x-,y- and z-values and the degrees, radiuses and 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_3d()
,
shear_2d()
,
shear_3d()
,
swirl_2d()
,
swirl_3d()
Other rotation functions:
rotate_3d()
,
swirl_2d()
,
swirl_3d()
Examples
# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
"Index" = 1:12,
"A" = c(1:4, 9:12, 15:18),
"G" = rep(1:3, each = 4)
)
# Rotate values around (0, 0)
rotate_2d(df, degrees = 45, x_col = "Index", y_col = "A", origin = c(0, 0))
# Rotate A around the centroid
df_rotated <- df %>%
rotate_2d(
x_col = "Index",
y_col = "A",
degrees = c(0, 120, 240),
origin_fn = centroid
)
df_rotated
# Plot A and A rotated around overall centroid
if (has_ggplot){
ggplot(df_rotated, aes(x = Index_rotated, y = A_rotated, color = factor(.degrees))) +
geom_hline(yintercept = mean(df$A), size = 0.2, alpha = .4, linetype = "dashed") +
geom_vline(xintercept = mean(df$Index), size = 0.2, alpha = .4, linetype = "dashed") +
geom_line(alpha = .4) +
geom_point() +
theme_minimal() +
labs(x = "Index", y = "Value", color = "Degrees")
}
# Rotate around group centroids
df_grouped <- df %>%
dplyr::group_by(G) %>%
rotate_2d(
x_col = "Index",
y_col = "A",
degrees = c(0, 120, 240),
origin_fn = centroid
)
df_grouped
# Plot A and A rotated around group centroids
if (has_ggplot){
ggplot(df_grouped, aes(x = Index_rotated, y = A_rotated, color = factor(.degrees))) +
geom_point() +
theme_minimal() +
labs(x = "Index", y = "Value", color = "Degrees")
}