swirl_2d {rearrr} | R Documentation |
Swirl the values around an origin in 2 dimensions
Description
The values are swirled counterclockwise around a specified origin. The swirling is done by rotating around the origin with the degrees based on the distances to the origin as so:
degrees = scale_fn(distances) / (2 * radius) * 360
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 swirling around e.g. the centroid
of each group.
Usage
swirl_2d(
data,
radius,
x_col = NULL,
y_col = NULL,
suffix = "_swirled",
origin = NULL,
origin_fn = NULL,
scale_fn = identity,
keep_original = TRUE,
degrees_col_name = ".degrees",
radius_col_name = ".radius",
origin_col_name = ".origin",
overwrite = FALSE
)
Arguments
data |
|
radius |
Radius of the most-inner swirl on the x-axis in the simplest case.
A negative number changes the direction to clockwise rotation.
Can be a Note: With a custom |
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 swirl 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 |
scale_fn |
Function for scaling the distances before calculating the degrees. Input: A Output: A E.g.:
|
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 |
radius_col_name |
Name of new column with the radius. If |
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 three new columns containing
the swirled x- and y-values, the degrees, the radius, 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_2d()
,
shear_3d()
,
swirl_3d()
Other rotation functions:
rotate_2d()
,
rotate_3d()
,
swirl_3d()
Other distance functions:
closest_to()
,
dim_values()
,
distance()
,
expand_distances()
,
expand_distances_each()
,
furthest_from()
,
swirl_3d()
Examples
# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Set seed
set.seed(4)
# Create a data frame
df <- data.frame(
"x" = 1:50,
"y" = 1,
"r1" = runif(50),
"r2" = runif(50) * 35,
"g" = rep(1:5, each = 10)
)
# Swirl values around (0, 0)
swirl_2d(
data = df,
radius = 45,
x_col = "x",
y_col = "y",
origin = c(0, 0)
)
# Swirl around the centroid
# with 6 different radius settings
# Scale the distances with custom function
df_swirled <- swirl_2d(
data = df,
radius = c(95, 96, 97, 98, 99, 100),
x_col = "x",
y_col = "y",
origin_fn = centroid,
scale_fn = function(d) {
d^1.6
}
)
df_swirled
# Plot swirls
if (has_ggplot){
df_swirled %>%
ggplot(aes(x = x_swirled, y = y_swirled, color = factor(.radius))) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = ".radius")
}
#
# Swirl random data
# The trick lies in finding the right radius
#
# Swirl the random columns
df_swirled <- swirl_2d(
data = df,
radius = 5,
x_col = "r1",
y_col = "r2",
origin_fn = centroid
)
# Plot swirls
if (has_ggplot){
df_swirled %>%
ggplot(aes(x = r1_swirled, y = r2_swirled)) +
geom_point() +
theme_minimal() +
labs(x = "r1", y = "r2")
}