ropensci / av

Working with Video in R
https://docs.ropensci.org/av
Other
92 stars 10 forks source link

Bug in animating stacked bar graph #5

Closed rdatasculptor closed 5 years ago

rdatasculptor commented 5 years ago

First of all: Thank you for this wonderful package!

I am trying to make a transition between four stacked bar graphs. The output is not quite what I expected. Probably it's a bug.

This is the data frame I use:

 df <- structure(list(name = c("variable", "variable", "variable", "variable",    
        "variable", "variable", "variable", "variable", "variable", "variable", 
        "variable", "variable", "variable"), groups = structure(c(3L, 
        3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L), .Label = c("group 1", 
        "group 2", "group 3", "group 4"), class = "factor"), score = structure(c(4L, 
        3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), .Label = c("4", 
        "3", "2", "1"), class = c("ordered", "factor")), percentage = c(8, 
        38, 38, 16, 17.1428571428571, 40, 42.8571428571429, 40, 20, 40, 
        5, 65, 30), percentage2 = c("8%", "38%", "38%", "16%", "17.1%", 
        "40%", "42.9%", "40%", "20%", "40%", "5%", "65%", "30%"), label = c(0.04, 
        0.27, 0.65, 0.92, 0.0857142857142857, 0.371428571428571, 0.785714285714286, 
        0.2, 0.5, 0.8, 0.025, 0.375, 0.85)), row.names = c(NA, -13L), class = "data.frame")

When I make a stacked bar graph of just one stage of the groups variable, I get e.g. this:

    library(ggplot2)
    library(dplyr)

    ggplot(filter(df, groups == "group 3"),
           aes(x = name, y = percentage, fill = score)) +
        geom_bar(stat = "identity", position = "fill", width = 0.8) +
        geom_text(aes(y = label, label = percentage2), color = "grey25") +
        coord_flip() +
        scale_fill_manual(values=c("darkgreen", "lightgreen", "yellow", "red"), 
                          guide = guide_legend(reverse = TRUE), drop=FALSE) 

enter image description here

But When I try to add a gganimate animation of the four different group stages I get this:

library(gganimate)

ggplot(df, aes(x = name, y = percentage, fill = score)) +
    geom_bar(stat = "identity", position = "fill", width = 0.8) + 
    geom_text(aes(y = label, label = percentage2), color = "grey25") +
    coord_flip() +
    scale_fill_manual(values = c("darkgreen", "lightgreen", "yellow", "red"), 
                      guide= guide_legend(reverse = TRUE), drop = FALSE) + 
    transition_states(groups, transition_length = 2, state_length =  1)

enter image description here

It seems to add all the percentages (bar lengths) of all the groups to the animation at the same time. I want see a transition between the stacked bar graphs of the four different groups without the gaps.

jeroen commented 5 years ago

I think you need to post this in the gganimate repo. The av package only deals with formatting the video file.

rdatasculptor commented 5 years ago

I am sorry, wrong screen.... Can we delete this issue?