# 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(
"x" = runif(20),
"y" = runif(20),
"g" = rep(1:4, each = 5)
)
# Expand values in the two dimensions (x and y)
# With the origin at x=0.5, y=0.5
# We expand x by 2 and y by 4
expand_distances_each(
data = df,
cols = c("x", "y"),
multipliers = c(2, 4),
origin = c(0.5, 0.5)
)
# Expand values in the two dimensions (x and y)
# With the origin at x=0.5, y=0.5
# We expand both by 3
expand_distances_each(
data = df,
cols = c("x", "y"),
multipliers = 3,
origin = 0.5
)
# Expand values in one dimension (x)
# With the origin at x=0.5
# We expand by 3
expand_distances_each(
data = df,
cols = c("x"),
multipliers = 3,
origin = 0.5
)
# Expand x and y around the centroid
# We use exponentiation for a more drastic effect
# The add_one_exp makes sure it expands
# even when x or y is in the range [>-1, <1]
# To compare multiple exponents, we wrap the
# call in purrr::map_dfr
df_expanded <- purrr::map_dfr(
.x = c(1, 2.0, 3.0, 4.0),
.f = function(exponent) {
expand_distances_each(
data = df,
cols = c("x", "y"),
multipliers = exponent,
origin_fn = centroid,
exponentiate = TRUE,
add_one_exp = TRUE
)
}
)
df_expanded
# Plot the expansions of x and y around the overall centroid
if (has_ggplot){
ggplot(df_expanded, aes(x = x_expanded, y = y_expanded, color = factor(.exponents))) +
geom_vline(
xintercept = df_expanded[[".origin"]][[1]][[1]],
size = 0.2, alpha = .4, linetype = "dashed"
) +
geom_hline(
yintercept = df_expanded[[".origin"]][[1]][[2]],
size = 0.2, alpha = .4, linetype = "dashed"
) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "Exponent")
}
# Expand x and y around the centroid using multiplication
# To compare multiple multipliers, we wrap the
# call in purrr::map_dfr
df_expanded <- purrr::map_dfr(
.x = c(1, 2.0, 3.0, 4.0),
.f = function(multiplier) {
expand_distances_each(df,
cols = c("x", "y"),
multipliers = multiplier,
origin_fn = centroid,
exponentiate = FALSE
)
}
)
df_expanded
# Plot the expansions of x and y around the overall centroid
if (has_ggplot){
ggplot(df_expanded, aes(x = x_expanded, y = y_expanded, color = factor(.multipliers))) +
geom_vline(
xintercept = df_expanded[[".origin"]][[1]][[1]],
size = 0.2, alpha = .4, linetype = "dashed"
) +
geom_hline(
yintercept = df_expanded[[".origin"]][[1]][[2]],
size = 0.2, alpha = .4, linetype = "dashed"
) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "Multiplier")
}
# Expand x and y with different multipliers
# around the centroid using multiplication
df_expanded <- expand_distances_each(
df,
cols = c("x", "y"),
multipliers = c(1.25, 10),
origin_fn = centroid,
exponentiate = FALSE
)
df_expanded
# Plot the expansions of x and y around the overall centroid
# Note how the y axis is expanded a lot more than the x-axis
if (has_ggplot){
ggplot(df_expanded, aes(x = x_expanded, y = y_expanded)) +
geom_vline(
xintercept = df_expanded[[".origin"]][[1]][[1]],
size = 0.2, alpha = .4, linetype = "dashed"
) +
geom_hline(
yintercept = df_expanded[[".origin"]][[1]][[2]],
size = 0.2, alpha = .4, linetype = "dashed"
) +
geom_line(aes(color = "Expanded")) +
geom_point(aes(color = "Expanded")) +
geom_line(aes(x = x, y = y, color = "Original")) +
geom_point(aes(x = x, y = y, color = "Original")) +
theme_minimal() +
labs(x = "x", y = "y", color = "Multiplier")
}
#
# Contraction
#
# Group-wise contraction to create clusters
df_contracted <- df %>%
dplyr::group_by(g) %>%
expand_distances_each(
cols = c("x", "y"),
multipliers = 0.07,
suffix = "_contracted",
origin_fn = centroid
)
# Plot the clustered data point on top of the original data points
if (has_ggplot){
ggplot(df_contracted, aes(x = x_contracted, y = y_contracted, color = factor(g))) +
geom_point(aes(x = x, y = y, color = factor(g)), alpha = 0.3, shape = 16) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "g")
}
Run the code above in your browser using DataLab