bill999
bill999

Reputation: 2529

How to make one bar appear at a time in gganimate

I have a simple bar chart. How can I make the bars appear sequentially? In other words, the first bar should appear. The second bar should appear (and the first one stays in the place). Then the third bar should appear (and the first two should stay in place).

Say I have this MWE:

library(ggplot2)
library(gganimate)

csv <- "fruit, value Apple, 60 Orange, 51 Watermelon, 50"

data <- read.csv(text=csv, header=TRUE)
ggplot(data, aes(fruit, value)) + 
    geom_bar(stat='identity') +
    transition_reveal(fruit)

This does not work. What should I do?

Upvotes: 0

Views: 645

Answers (2)

a1a5a6
a1a5a6

Reputation: 65

Please run the following code to get your desired output:

ggplot(data, aes(fruit, value)) + 
    geom_bar(stat='identity') + 
    transition_states(fruit, wrap = FALSE) + 
    shadow_mark(past = TRUE) +
    enter_grow()

Hope this helps. Cheers.

Upvotes: 1

Marius
Marius

Reputation: 60160

You can add a column of numbers that gives the order they should appear:

data$fruit_order = 1:3

ggplot(data, aes(fruit, value)) + 
    geom_bar(stat='identity') +
    transition_reveal(fruit_order)

Upvotes: 1

Related Questions