Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created December 13, 2024 13:15
Show Gist options
  • Save abikoushi/205e8bd3f7b63978db4a9ace937b2171 to your computer and use it in GitHub Desktop.
Save abikoushi/205e8bd3f7b63978db4a9ace937b2171 to your computer and use it in GitHub Desktop.
rotate DataSaurus
library(datasauRus)
library(ggplot2)
library(dplyr)
library(gganimate)
rot <-function(ti, period=16) {
coeff <- 2*pi/period
matrix(c(cos(coeff*ti),-sin(coeff*ti),
sin(coeff*ti), cos(coeff*ti)), byrow = TRUE, nrow = 2)
}
dino <- dplyr::filter(datasaurus_dozen, dataset=="dino") %>%
dplyr::select(-dataset) %>%
as.matrix()
rotate_dino <- function(tim){
X <-dino%*%rot(tim)
m <- colMeans(X)
data.frame(X, ti=tim)
}
dinos <- bind_rows(lapply(0:16, rotate_dino))
dinos_mean <- group_by(dinos,ti) %>%
summarise(X1=mean(X1),X2=mean(X2))
p1 <- ggplot(data = dinos)+
geom_point(aes(x = X1, y = X2))+
theme_classic()+
coord_fixed()+
transition_time(ti)+
labs(title = 'time: {frame_time}')
anim_save("rotate_dino.gif")
psub <- ggplot(data = dinos_mean)+
geom_line(data = dinos_mean, aes(x=ti, y=X1, colour="X1"))+
geom_line(data = dinos_mean, aes(x=ti, y=X2, colour="X2"))+
labs(x="time",y="average", colour="") +
scale_colour_brewer(palette = "Set2")+
theme_classic()
ggsave("marginal_dino.jpeg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment