(Context: I'm using the soc.ca package and hoping to illustrate the differences in angles between the overall MCA results and each of the class-specific results.)
I have a data frame of pairs of slopes, and I'm hoping to illustrate how they differ from axes where x = 0 and y = 0. This works OK in still images. However, I'd ideally like to use gganimate to illustrate each pair of slopes rotating from those axes.
In order to do so, I think I need to use geom_abline. This works fine to illustrate the difference between the different slopes, but not for the difference between those slopes and x = 0 and y = 0.
My data is currently of the following shape:
# set seed
set.seed(24601)
# load packages
library(tidyverse)
library(gganimate)
# generate vector of slopes
slopes <-
runif(12,
-2,
2)
# generate vector of cases i'm cycling through
cases <-
c(rep("a", 2),
rep("b", 2),
rep("c", 2),
rep("d", 2),
rep("e", 2),
rep("f", 2)
)
# generate vector of primary or secondary axes
axes <-
(rep(c("primary",
"secondary"),
6))
# generate vector of zeroes
zeroes <-
rep(0, 12)
# combine into a data frame
df <-
data.frame(slopes,
cases,
axes,
zeroes)
The still image can be generated as follows
ggplot(df) +
geom_abline(aes(slope = slopes,
linetype = axes,
intercept = 0)) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
facet_wrap(~ cases)
while the animation is currently as follows:
ggplot(df) +
geom_abline(aes(intercept = zeroes,
slope = slopes,
linetype = axes)) +
transition_states(cases) +
scale_x_continuous(limits = c(-1, 1)) +
scale_y_continuous(limits = c(-1, 1))
What I ideally want is for the axes to "snap" back to x = 0 and y = 0 between each phase. However, in order to do so I need the slope of geom_abline to be infinity for the relevant cases.
Any suggestions for how to do this very welcome!
mhovd has shown me geom_spoke, which addresses the problem that I had. They were having a couple of issues with the animation, which I think I've managed to resolve: below is my tweaking of their code such that the axes rotate in the ways that I was hoping they would.
library(tidyverse)
library(gganimate)
# Seed
set.seed(24601)
# Given the following slopes
# nb I diverge from the helpful solution here
# by having the x and y slopes as separate variables
x_slopes <-
runif(6,
-2,
2)
y_slopes <-
runif(6,
-2,
2)
# What are the angles?
df = data.frame(
x = 0, # Draw from zero
y = 0, # Draw from zero
angle_1 = atan(x_slopes), # generate primary axis slopes
angle_2 = atan(y_slopes) + pi/2, # generate secondary axis slopes
radius = 5, # Make sufficiently long lines
cases =c("a", "b", "c", "d", "e", "f")
) %>%
mutate( angle_3 = angle_1 + pi, # opposite angle for primary axis
angle_4 = angle_2 + pi, # opposite angle for secondary axis
)
df
# Add horizontal and vertical lines
# again this differs from the solution as i have
# separate variables for the (originally) horizontal and vertical lines
original_axis_data = data.frame(x = 0,
y = 0,
angle_1 = atan(0),
angle_2 = atan(0) + pi/2,
angle_3 = atan(0) + pi,
angle_4 = atan(0) + pi*1.5,
radius = 5,
cases = "reset")
# combine these objects
df = rbind(df, original_axis_data)
# generate list (from solution)
animation_list = list(
df %>% filter(cases == "a") %>% mutate(event = 1),
df %>% filter(cases == "reset") %>% mutate(event = 2),
df %>% filter(cases == "b") %>% mutate(event = 3),
df %>% filter(cases == "reset") %>% mutate(event = 4),
df %>% filter(cases == "c") %>% mutate(event = 5),
df %>% filter(cases == "reset") %>% mutate(event = 6),
df %>% filter(cases == "d") %>% mutate(event = 7),
df %>% filter(cases == "reset") %>% mutate(event = 8),
df %>% filter(cases == "e") %>% mutate(event = 9),
df %>% filter(cases == "reset") %>% mutate(event = 10),
df %>% filter(cases == "f") %>% mutate(event = 11),
df %>% filter(cases == "reset") %>% mutate(event = 12)
)
# convert to data frame (again from solution)
animation_data = bind_rows(animation_list) %>%
mutate(cases = factor(cases)) # To make ggplot respect the order
# animate!
animation_data %>%
ggplot(aes(x = x, y = y, radius = radius)) + # i have simplified these aes
geom_spoke(aes(angle = angle_1)) + # primary axis, original
geom_spoke(aes(angle = angle_2),
linetype = "dashed") + # secondary axis, original, linetype explicit
geom_spoke(aes(angle = angle_3)) + # primary axis plus pi
geom_spoke(aes(angle = angle_4),
linetype = "dashed") + # secondary axis plus pi
coord_cartesian(ylim=c(-1, 1), xlim = c(-1, 1)) + # Zoom in without removing data like scale_*_continous does
theme(legend.position = NULL, aspect.ratio = 1) +
transition_states(event) +
labs(title = "{closest_state}")
Instead of working with slopes, maybe you can achieve what you want with angles using geom_spoke
.
The angle inclination of a line is theta = atan(slope)
. The opposite angle would be theta + pi
.
For the horizontal and vertical lines, would you pass a slope of 0
and Inf
, respectively.
EDIT: I made a poor attempt at showing how you could build the animation, but I was unable to make it transition smoothly. Hopefully you will be able to build on this.
library(tidyverse)
library(gganimate)
# Seed
set.seed(24601)
# Given the following slopes
slopes <- runif(12,-2, 2)
# What are the angles?
df = data.frame(
x = 0, # Draw from zero
y = 0, # Draw from zero
angle1 = atan(slopes), # First segment, actual angle
angle2 = atan(slopes) + pi, # Second segment, opposite angle
radius = 5, # Make sufficiently long lines
cases =c(rep("a", 2), rep("b", 2), rep("c", 2), rep("d", 2), rep("e", 2), rep("f", 2)),
axes = c("primary", "secondary")
)
# Add horizontal and vertical lines
hline_data = data.frame(x = 0, y = 0, angle1 = atan(0), angle2 = atan(0) + pi, radius = 5, cases = "reset", axes = "primary")
vline_data = data.frame(x = 0, y = 0, angle1 = atan(Inf), angle2 = atan(Inf) + pi, radius = 5, cases = "reset", axes = "primary")
df = rbind(df, vline_data, hline_data)
df %>%
ggplot(aes(x = x, y = y, radius = radius, col = cases, linetype = axes)) +
geom_spoke(aes(angle = angle1)) +
geom_spoke(aes(angle = angle2)) +
coord_cartesian(ylim=c(-1, 1), xlim = c(-1, 1)) + # Zoom in without removing data like scale_*_continous does
theme(legend.position = NULL, aspect.ratio = 1)
# Poor attempt at building the animation
animation_list = list(
df %>% filter(cases == "a") %>% mutate(event = 1),
df %>% filter(cases == "reset") %>% mutate(event = 2),
df %>% filter(cases == "b") %>% mutate(event = 3),
df %>% filter(cases == "reset") %>% mutate(event = 4),
df %>% filter(cases == "c") %>% mutate(event = 5),
df %>% filter(cases == "reset") %>% mutate(event = 6),
df %>% filter(cases == "d") %>% mutate(event = 7),
df %>% filter(cases == "reset") %>% mutate(event = 8),
df %>% filter(cases == "e") %>% mutate(event = 9),
df %>% filter(cases == "reset") %>% mutate(event = 10),
df %>% filter(cases == "f") %>% mutate(event = 11),
df %>% filter(cases == "reset") %>% mutate(event = 12)
)
animation_data = bind_rows(animation_list) %>%
mutate(cases = factor(cases)) # To make ggplot respect the order
animation_data %>%
ggplot(aes(x = x, y = y, radius = radius, linetype = axes, group = event)) +
geom_spoke(aes(angle = angle1)) +
geom_spoke(aes(angle = angle2)) +
coord_cartesian(ylim=c(-1, 1), xlim = c(-1, 1)) + # Zoom in without removing data like scale_*_continous does
theme(legend.position = NULL, aspect.ratio = 1) +
transition_states(event) +
labs(title = "{closest_state}")
Created on 2021-07-09 by the reprex package (v2.0.0)