Created
November 12, 2018 13:10
-
-
Save jonocarroll/ac43d2d98b2e011cda8befc8cfd026da to your computer and use it in GitHub Desktop.
Rotating Cube
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently
gganimate
throws an error when trying to usegeom_polygon
orgeom_line
:Without transitions, it seems to work: