Search code examples
rggplot2data-manipulationgganimate

Graph Not Changing Colors in R


I wrote the following procedure in R:

  • Start with a data frame called "giraffe" data

  • Sample 30% of this data and label it "sample"

  • Create a histogram for this data, and color the areas of this histogram that were "sampled" as one color, and the other rows another color

  • Repeat this process 100 times and make an animation of this process

    library(ggplot2)
      library(dplyr)
      library(gganimate)
    
      giraffe_data <- data.frame( a = abs(rnorm(1000,17,10)), b = abs(rnorm(1000,17,10)))
    
      results <- list()
    
    
    
      for( i in 1:100) 
    
      {
    
          giraffe_data_i <- giraffe_data 
          a_i <- c("sample", "not_sampled")
          aa_i <- as.factor(sample(a_i, 1000, replace=TRUE, prob=c(0.3, 0.7)))
          giraffe_data_i $col = cut(giraffe_data_i$a, c(-Inf, 17, Inf))
          giraffe_data_i$sample <- aa_i
          giraffe_data_i$iteration <- i + 1
    
    
    
          results[[i]] <- giraffe_data_i
    
      }
    
      results
      results_df <- do.call(rbind.data.frame, results)
    
    
    
    
      animate(
        ggplot(results_df, aes(x=a, fill = col)) + 
        geom_histogram(binwidth=1) +  
        scale_fill_manual(breaks = levels(results_df$col), values = c('blue', 'red')) +
        transition_states(iteration, state_length = 0.2) +
        labs(title = "Group: {closest_state}"),
        fps = 25)
    

enter image description here

But for some reason, this graph does not change colors in the animation.

Can someone please show me how to fix this?

Thanks

Note: I was able to get the colors to change with the following code:

animate(
    ggplot(results_df, aes(x=a, color = sample)) + 
        geom_histogram(fill="white", position="dodge")+  
        transition_states(iteration, state_length = 0.2) +
        labs(title = "Group: {closest_state}"),
    fps = 5)

enter image description here

But this shows the two colors as two separate "groups". I want there to be only one "group", but there to be different colors within this one "group". Can someone please show me how to fix this?

Thanks


Solution

  • Sometimes I find it easier to do transformations of the data upstream of gganimate. So here's an approach of binning the data and counting for each iteration, and then plotting as a normal column geom.

    library(tidyverse); library(gganimate)
    # bins of width 2
    bin_wid = 2
    results_df_bins <- results_df %>%
      # "col" is set at 17 but my bins are at even #s, so to align 
      #   bins with that I offset by 1
      mutate(a_bin = floor((a + 1)/ bin_wid)*bin_wid) %>%
      count(a_bin, col, sample, iteration) %>%
      mutate(sample = fct_rev(sample)) # put "sample" first
    
    animate(
      ggplot(results_df_bins, aes(x=a_bin, y = n, fill = sample)) + 
        geom_col(position = position_stack(reverse = TRUE)) +
        transition_states(iteration, state_length = 0.2) +
        labs(title = "Group: {closest_state}"),
      fps = 25, nframes = 500, height = 300)
    

    enter image description here