Skip to content

Add Color to Rank Abundance Animation #29

@lukepag

Description

@lukepag
gg_scatter <- function(dat, dat_2, yvar, is_abund = TRUE) {
  
  # select fewer generations to make plotting easier
  if (length(unique(dat$gen)) > 150) {
    g <- unique(dat$gen)  
    g_first <- g[1] # keep the first generation
    g_last <- g[length(g)] # keep the last generation
    g_rest <- g[-1] # sample from all except the first generation
    s_g <- sample(g_rest, 100, replace = FALSE) # sample from the generations
    s_g <- c(g_first, s_g, g_last)
    
    dat <- dat[dat$gen %in% s_g,] # filter for the sampled generations
    
  }
  
  
  
  if (is_abund) {
    y_lims <- c(min(dat$abund), max(dat$abund))
    y_lab <- "Abundance"
  } else {
    y_lims <- c(min(dat$traits), max(dat$traits))
    y_lab = "Trait"
  }
  
  p <- ggplot() +
    geom_line(data = dat, aes_string(x = "rank", y = yvar, group = "gen"), color = "lightgrey", alpha = 0.1) +
    geom_point(data = dat, aes_string(x = "rank", y = yvar, group = "gen",  frame = "gen"), color = "#107361", alpha = 1.0) +
    labs(x = "Rank", y = y_lab, color = "Generation") +
    #ylim(y = y_lims) + 
    theme_bw()  +
    theme(legend.key.size = unit(3, "mm"))
  
  p_int <- ggplotly(p) 
  
  l <- ggplot() +
    geom_line(data = dat_2, aes_string(x = "gen", y = "hillAbund_1"), color = "black", alpha = 1.0) +
    geom_point(data = dat_2, aes_string(x = "gen", y = "hillAbund_1", frame = "gen"), color = "#107361", alpha = 1.0) +
    labs(x = "Generation", y = y_lab) +
    theme_bw()  +
    theme(legend.key.size = unit(3, "mm"))
  
  l_int <- ggplotly(l)
  
  
  p_fin <- subplot(p_int, l_int) |> #uses the output of subplot(...) 
    animation_slider(currentvalue = list(prefix = "Gen = ", font = list(color = "black")))
  
  shinybusy::remove_modal_spinner()
  return(p_fin)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions