# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Set seed
set.seed(1)
df_square <- square(runif(100)) %>%
rename(x = .square_x,
y = Value) %>%
mutate(z = 1)
# Shear the x and z axes
# around the centroid
df_sheared <- shear_3d(
data = df_square,
x_col = "x",
y_col = "y",
z_col = "z",
x_shear = 2,
z_shear = 4,
origin_fn = centroid
)
# Plot sheared data
# Black: original points
# Red: sheared points
if (has_ggplot){
df_sheared %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_point(aes(x = x_sheared, y = y_sheared, color = "red")) +
theme_minimal()
}
if (FALSE) {
# Plot 3d with plotly
plotly::plot_ly(
x = df_sheared$x_sheared,
y = df_sheared$y_sheared,
z = df_sheared$z_sheared,
type = "scatter3d",
mode = "markers",
color = df_sheared$.shear_str
)
}
# Shear the y and z axes
# around the centroid
df_sheared <- shear_3d(
data = df_square,
x_col = "x",
y_col = "y",
z_col = "z",
y_shear = 2,
z_shear = 4,
origin_fn = centroid
)
# Plot sheared data
# Black: original points
# Red: sheared points
if (has_ggplot){
df_sheared %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_point(aes(x = x_sheared, y = y_sheared, color = "red")) +
theme_minimal()
}
if (FALSE) {
# Plot 3d with plotly
plotly::plot_ly(
x = df_sheared$x_sheared,
y = df_sheared$y_sheared,
z = df_sheared$z_sheared,
type = "scatter3d",
mode = "markers",
color = df_sheared$.shear_str
)
}
# Shear the y and z axes with multiple amounts at once
# around the centroid
df_sheared <- shear_3d(
data = df_square,
x_col = "x",
y_col = "y",
z_col = "z",
y_shear = c(0, 2, 4),
z_shear = c(0, 4, 6),
origin_fn = centroid
)
# Plot sheared data
if (has_ggplot){
df_sheared %>%
ggplot(aes(x = x_sheared, y = y_sheared, color = .shear_str)) +
geom_point() +
theme_minimal()
}
if (FALSE) {
# Plot 3d with plotly
plotly::plot_ly(
x = df_sheared$x_sheared,
y = df_sheared$y_sheared,
z = df_sheared$z_sheared,
type = "scatter3d",
mode = "markers",
color = df_sheared$.shear_str
)
}
Run the code above in your browser using DataLab