# Attach packages
library(rearrr)
library(dplyr)
library(purrr)
has_ggplot <- require(ggplot2) # Attach if installed
# Set seed
set.seed(7)
# Create a data frame with clusters
df <- generate_clusters(
num_rows = 70,
num_cols = 3,
num_clusters = 5,
compactness = 1.6
) %>%
dplyr::rename(x = D1, y = D2, z = D3) %>%
dplyr::mutate(o = 1)
# Dim the values in the z column
dim_values(
data = df,
cols = c("x", "y", "z"),
origin = c(0.5, 0.5, 0.5)
)
# Dim the values in the `o` column (all 1s)
# around the centroid
dim_values(
data = df,
cols = c("x", "y"),
dim_col = "o",
origin_fn = centroid
)
# Specify dimming_fn
# around the centroid
dim_values(
data = df,
cols = c("x", "y"),
dim_col = "o",
origin_fn = centroid,
dimming_fn = function(x, d) {
x * 1 / (2^(1 + d))
}
)
#
# Dim cluster-wise
#
# Group-wise dimming
df_dimmed <- df %>%
dplyr::group_by(.cluster) %>%
dim_values(
cols = c("x", "y"),
dim_col = "o",
origin_fn = centroid
)
# Plot the dimmed data such that the alpha (opacity) is
# controlled by the dimming
# (Note: This works because the `o` column is 1 for all values)
if (has_ggplot){
ggplot(
data = df_dimmed,
aes(x = x, y = y, alpha = o_dimmed, color = .cluster)
) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "Cluster", alpha = "o_dimmed")
}
Run the code above in your browser using DataLab