# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2) # Attach if installed
# Create a data frame
df <- data.frame(
"x" = rep(1:6, each = 2),
"y" = rep(c(1, 4), 6),
"g" = rep(1:2, each = 6)
)
# Shear the x variable with regards to y
# around the centroid
df_sheared <- shear_2d(
data = df,
x_shear = 2.5,
x_col = "x",
y_col = "y",
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()
}
# Shear in both dimensions
df_sheared <- shear_2d(
data = df,
x_shear = 2.5,
y_shear = 2.5,
x_col = "x",
y_col = "y",
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()
}
# Shear grouped data frame
# Affects the calculated origin
df_sheared <- shear_2d(
data = dplyr::group_by(df, g),
x_shear = 2.5,
x_col = "x",
y_col = "y",
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()
}
# Shear a vector with multiple shearing factors
shear_2d(
data = c(1:10),
x_shear = c(1, 1.5, 2, 2.5),
origin = c(0, 0)
)
Run the code above in your browser using DataLab