Skip to content

Instantly share code, notes, and snippets.

@jonocarroll
Created November 12, 2018 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonocarroll/ac43d2d98b2e011cda8befc8cfd026da to your computer and use it in GitHub Desktop.
Save jonocarroll/ac43d2d98b2e011cda8befc8cfd026da to your computer and use it in GitHub Desktop.
Rotating Cube
library(threed)
library(dplyr)
library(gganimate)
library(ggplot2)
angles <- tail(seq(0, pi/2, length.out = 15), -1)
camera_to_world <- threed::look_at_matrix(eye = c(1.5, 1.75, 4), at = c(0, 0, 0))
objlist <- vector("list", length(angles))
for (i in seq_along(angles)) {
objlist[[i]] <- threed::mesh3dobj$cube %>%
rotate_by(angle = angles[i], v = c(0, 1, 0)) %>%
transform_by(invert_matrix(camera_to_world)) %>%
orthographic_projection() %>%
threed::fortify.mesh3d() %>%
mutate(frame = i)
}
obj <- bind_rows(objlist)
ggplot(obj, aes(x, y, group = element_id)) +
geom_polygon(aes(fill = fnx), colour = 'black') +
scale_linetype_manual(values = c('TRUE' = "dotted", 'FALSE' = 'solid')) +
scale_size_manual(values = c('TRUE' = 0.2, 'FALSE' = 0.5)) +
scale_fill_continuous(limits = c(-1, 1)) +
theme_void() +
theme(legend.position = 'none') +
coord_equal(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
transition_states(frame, transition_length = 1, state_length = 1)
@jonocarroll
Copy link
Author

Currently gganimate throws an error when trying to use geom_polygon or geom_line:

Error in is_quosure(e2) : argument "e2" is missing, with no default

Without transitions, it seems to work:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment