swirl_2d {rearrr}R Documentation

Swirl the values around an origin in 2 dimensions

Description

[Experimental]

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

data.frame or vector.

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 vector with multiple radiuses.

Note: With a custom `scaling_fn`, this might not be the actual swirl radius anymore. Think of it more as a width setting where a larger number leads to fewer full rotations.

x_col

Name of x column in `data`. If NULL and `data` is a vector, the index of `data` is used. If `data` is a data.frame, it must be specified.

y_col

Name of y column in `data`. If `data` is a data.frame, it must be specified.

suffix

Suffix to add to the names of the generated columns.

Use an empty string (i.e. "") to overwrite the original columns.

origin

Coordinates of the origin to swirl around. Vector with 2 elements (i.e. origin_x, origin_y). Ignored when `origin_fn` is not NULL.

origin_fn

Function for finding the origin coordinates.

Input: Each column will be passed as a vector in the order of `cols`.

Output: A vector with one scalar per dimension.

Can be created with create_origin_fn() if you want to apply the same function to each dimension.

E.g. `create_origin_fn(median)` would find the median of each column.

Built-in functions are centroid(), most_centered(), and midrange()

scale_fn

Function for scaling the distances before calculating the degrees.

Input: A numeric vector (the distances).

Output: A numeric vector (the scaled distances) of the same length.

E.g.:

function(d){

⁠ ⁠d ^ 1.5

}

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 NULL, no column is added.

radius_col_name

Name of new column with the radius. If NULL, no column is added.

origin_col_name

Name of new column with the origin coordinates. If NULL, no column is added.

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")
}

[Package rearrr version 0.3.4 Index]