| hexagonalize {rearrr} | R Documentation | 
Create x-coordinates so the points form a hexagon
Description
Create the x-coordinates for a vector of y-coordinates such that
they form a hexagon.
This will likely look most like a hexagon when the y-coordinates are somewhat equally distributed, e.g. a uniform distribution.
Usage
hexagonalize(
  data,
  y_col = NULL,
  .min = NULL,
  .max = NULL,
  offset_x = 0,
  keep_original = TRUE,
  x_col_name = ".hexagon_x",
  edge_col_name = ".edge",
  overwrite = FALSE
)
Arguments
data | 
 
  | 
y_col | 
 Name of column in   | 
.min | 
 Minimum y-coordinate. If   | 
.max | 
 Maximum y-coordinate. If   | 
offset_x | 
 Value to offset the x-coordinates by.  | 
keep_original | 
 Whether to keep the original columns. (Logical) Some columns may have been overwritten, in which case only the newest versions are returned.  | 
x_col_name | 
 Name of new column with the x-coordinates.  | 
edge_col_name | 
 Name of new column with the edge identifiers. If  Numbering is clockwise and starts at the upper-right edge.  | 
overwrite | 
 Whether to allow overwriting of existing columns. (Logical)  | 
Value
data.frame (tibble) with the added x-coordinates and an identifier
for the edge the data point is a part of.
Author(s)
Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk
See Also
Other forming functions: 
circularize(),
square(),
triangularize()
Examples
# Attach packages
library(rearrr)
library(dplyr)
library(purrr)
has_ggplot <- require(ggplot2)  # Attach if installed
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
  "y" = runif(200),
  "g" = factor(rep(1:5, each = 40))
)
# Hexagonalize 'y'
df_hex <- hexagonalize(df, y_col = "y")
df_hex
# Plot hexagon
if (has_ggplot){
  df_hex %>%
    ggplot(aes(x = .hexagon_x, y = y, color = .edge)) +
    geom_point() +
    theme_minimal()
}
#
# Grouped hexagonalization
#
# Hexagonalize 'y' for each group
# First cluster the groups a bit to move the
# hexagons away from each other
df_hex <- df %>%
  cluster_groups(
    cols = "y",
    group_cols = "g",
    suffix = "",
    overwrite = TRUE
  ) %>%
  dplyr::group_by(g) %>%
  hexagonalize(
    y_col = "y",
    overwrite = TRUE
  )
# Plot hexagons
if (has_ggplot){
  df_hex %>%
    ggplot(aes(x = .hexagon_x, y = y, color = g)) +
    geom_point() +
    theme_minimal()
}
#
# Specifying minimum value
#
# Specify minimum value manually
df_hex <- hexagonalize(df, y_col = "y", .min = -2)
df_hex
# Plot hexagon
if (has_ggplot){
  df_hex %>%
    ggplot(aes(x = .hexagon_x, y = y, color = .edge)) +
    geom_point() +
    theme_minimal()
}
#
# Multiple hexagons by contraction
#
# Start by hexagonalizing 'y'
df_hex <- hexagonalize(df, y_col = "y")
# Contract '.hexagon_x' and 'y' towards the centroid
# To contract with multiple multipliers at once,
# we wrap the call in purrr::map_dfr
df_expanded <- purrr::map_dfr(
  .x = c(1, 0.75, 0.5, 0.25, 0.125),
  .f = function(mult) {
    expand_distances(
      data = df_hex,
      cols = c(".hexagon_x", "y"),
      multiplier = mult,
      origin_fn = centroid,
      overwrite = TRUE
    )
  }
)
df_expanded
if (has_ggplot){
  df_expanded %>%
    ggplot(aes(
      x = .hexagon_x_expanded, y = y_expanded,
      color = .edge, alpha = .multiplier
    )) +
    geom_point() +
    theme_minimal()
}