Skip to content

Instantly share code, notes, and snippets.

@jonspring
Created July 28, 2020 08:47
Show Gist options
  • Save jonspring/7a5aa689080af603e8f7c9e9db7291be to your computer and use it in GitHub Desktop.
Save jonspring/7a5aa689080af603e8f7c9e9db7291be to your computer and use it in GitHub Desktop.
library(palmerpenguins)
library(tidyverse)
library(umap)
library(gganimate)
penguins_clean <- penguins %>% filter(!is.na(bill_length_mm), !is.na(sex))
penguinos <- function(neigh = 13) {
penguins_clean %>%
select(bill_length_mm:body_mass_g) %>%
scale() %>%
umap(n_neighbors = neigh) -> umapenguins
penguins_clean %>%
mutate(id = row_number()) %>%
bind_cols(umapenguins$layout %>% as_tibble()) %>%
mutate(neighbors = neigh)
}
animate(
map_df(c(2:10, 15, 20, 30, 50), penguinos) %>%
ggplot(aes(V1, V2, color = species, shape = species, group = id)) +
geom_point(size = 2, alpha = 0.7) +
hrbrthemes::theme_ipsum() +
transition_states(neighbors, state_length = 0.1, wrap = TRUE) +
view_follow() +
labs(title = "UMAP(enguins)",
subtitle = "Some Chinstraps numerically similar to Adelies",
caption = "n_neighbors = {closest_state}"),
fps = 25, duration = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment