# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
"Index" = 1:12,
"A" = c(1:4, 9:12, 15:18),
"G" = rep(1:3, each = 4)
)
# Rotate values around (0, 0)
rotate_2d(df, degrees = 45, x_col = "Index", y_col = "A", origin = c(0, 0))
# Rotate A around the centroid
df_rotated <- df %>%
rotate_2d(
x_col = "Index",
y_col = "A",
degrees = c(0, 120, 240),
origin_fn = centroid
)
df_rotated
# Plot A and A rotated around overall centroid
if (has_ggplot){
ggplot(df_rotated, aes(x = Index_rotated, y = A_rotated, color = factor(.degrees))) +
geom_hline(yintercept = mean(df$A), size = 0.2, alpha = .4, linetype = "dashed") +
geom_vline(xintercept = mean(df$Index), size = 0.2, alpha = .4, linetype = "dashed") +
geom_line(alpha = .4) +
geom_point() +
theme_minimal() +
labs(x = "Index", y = "Value", color = "Degrees")
}
# Rotate around group centroids
df_grouped <- df %>%
dplyr::group_by(G) %>%
rotate_2d(
x_col = "Index",
y_col = "A",
degrees = c(0, 120, 240),
origin_fn = centroid
)
df_grouped
# Plot A and A rotated around group centroids
if (has_ggplot){
ggplot(df_grouped, aes(x = Index_rotated, y = A_rotated, color = factor(.degrees))) +
geom_point() +
theme_minimal() +
labs(x = "Index", y = "Value", color = "Degrees")
}
Run the code above in your browser using DataLab