# 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:50,
"z" = 1:50,
"r1" = runif(50),
"r2" = runif(50) * 35,
"o" = 1,
"g" = rep(1:5, each = 10)
)
# Swirl values around (0, 0, 0)
swirl_3d(
data = df,
x_radius = 45,
x_col = "x",
y_col = "y",
z_col = "z",
origin = c(0, 0, 0)
)
# Swirl around the centroid
df_swirled <- swirl_3d(
data = df,
x_col = "x",
y_col = "y",
z_col = "z",
x_radius = c(100, 0, 0),
y_radius = c(0, 100, 0),
z_radius = c(0, 0, 100),
origin_fn = centroid
)
df_swirled
# Plot swirls
if (has_ggplot){
ggplot(df_swirled, aes(x = x_swirled, y = y_swirled, color = .radius_str, alpha = z_swirled)) +
geom_vline(xintercept = mean(df$x), size = 0.2, alpha = .4, linetype = "dashed") +
geom_hline(yintercept = mean(df$y), size = 0.2, alpha = .4, linetype = "dashed") +
geom_path(alpha = .4) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "radius", alpha = "z (opacity)")
}
if (FALSE) {
# Plot 3d with plotly
plotly::plot_ly(
x = df_swirled$x_swirled,
y = df_swirled$y_swirled,
z = df_swirled$z_swirled,
type = "scatter3d",
mode = "markers",
color = df_swirled$.radius_str
)
}
# Swirl around the centroid
df_swirled <- swirl_3d(
data = df,
x_col = "x",
y_col = "y",
z_col = "z",
x_radius = c(50, 0, 0),
y_radius = c(0, 50, 0),
z_radius = c(0, 0, 50),
origin_fn = centroid
)
df_swirled
# Plot swirls
if (has_ggplot){
ggplot(df_swirled, aes(x = x_swirled, y = y_swirled, color = .radius_str, alpha = z_swirled)) +
geom_vline(xintercept = mean(df$x), size = 0.2, alpha = .4, linetype = "dashed") +
geom_hline(yintercept = mean(df$y), size = 0.2, alpha = .4, linetype = "dashed") +
geom_path(alpha = .4) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "radius", alpha = "z (opacity)")
}
if (FALSE) {
# Plot 3d with plotly
plotly::plot_ly(
x = df_swirled$x_swirled,
y = df_swirled$y_swirled,
z = df_swirled$z_swirled,
type = "scatter3d",
mode = "markers",
color = df_swirled$.radius_str
)
}
# \donttest{
df_swirled <- swirl_3d(
data = df,
x_col = "x",
y_col = "y",
z_col = "z",
x_radius = c(25, 50, 25, 25),
y_radius = c(50, 75, 100, 25),
z_radius = c(75, 25, 25, 25),
origin_fn = centroid,
scale_fn = function(x) {
x^0.81
}
)
# Plot swirls
if (has_ggplot){
ggplot(df_swirled, aes(x = x_swirled, y = y_swirled, color = .radius_str, alpha = z_swirled)) +
geom_vline(xintercept = mean(df$x), size = 0.2, alpha = .4, linetype = "dashed") +
geom_hline(yintercept = mean(df$y), size = 0.2, alpha = .4, linetype = "dashed") +
geom_path(alpha = .4) +
geom_point() +
theme_minimal() +
labs(x = "x", y = "y", color = "radius", alpha = "z (opacity)")
}
# }
if (FALSE) {
# Plot 3d with plotly
plotly::plot_ly(
x = df_swirled$x_swirled,
y = df_swirled$y_swirled,
z = df_swirled$z_swirled,
type = "scatter3d",
mode = "markers",
color = df_swirled$.radius_str
)
}
# \donttest{
#
# Swirl random data
# The trick lies in finding the right radiuses
#
# Swirl the random columns
df_swirled <- swirl_3d(
data = df,
x_col = "r1",
y_col = "r2",
z_col = "o",
x_radius = c(0, 0, 0, 0),
y_radius = c(0, 30, 60, 90),
z_radius = c(10, 10, 10, 10),
origin_fn = centroid
)
# Not let's rotate it every 10 degrees
df_rotated <- df_swirled %>%
rotate_3d(
x_col = "r1_swirled",
y_col = "r2_swirled",
z_col = "o_swirled",
x_deg = rep(0, 36),
y_deg = rep(0, 36),
z_deg = (1:36) * 10,
suffix = "",
origin = df_swirled$.origin[[1]],
overwrite = TRUE
)
# Plot rotated swirls
if (has_ggplot){
ggplot(
df_rotated,
aes(
x = r1_swirled,
y = r2_swirled,
color = .degrees_str,
alpha = o_swirled
)
) +
geom_vline(xintercept = mean(df$r1), size = 0.2, alpha = .4, linetype = "dashed") +
geom_hline(yintercept = mean(df$r2), size = 0.2, alpha = .4, linetype = "dashed") +
geom_point(show.legend = FALSE) +
theme_minimal() +
labs(x = "r1", y = "r2", color = "radius", alpha = "o (opacity)")
}
# }
Run the code above in your browser using DataLab