0

I am using ggplot2 3.0.0 and gganimate 0.1.1. Here is a MWE:

library(ggplot2)
library(gganimate)

df <- data.frame(year = rep(2000:2010, each = 50),
                 value = rnorm(550))

ggplot(df, aes(x = 0, y = value, frame = factor(year))) +
  geom_violin() -> p
gganimate(p)

This creates an animation where every frame looks like this

current output

Whereas I would like each frame to occupy the whole plot like this

desired output

How could I go about tweaking my code to get this result?

EDIT

This is what I would like to produce, this time using the actual data have. I made this by manually stitching together each frame of animation but this way is not sustainable for more complicated plots so I wish to use gganimate instead.

Tim Hargreaves
  • 292
  • 3
  • 11
  • You're creating an animation of violin plots as a function of `year`. As you can see from the resulting animated GIF, plots are shown with increasing year along the x axis. I'm not sure how you expect a single violin plot to span the entire plotting area; the first plot on the left corresponds to the first year, the last plot on the right to the last year. Plot space has to be divided equally for all years. – Maurits Evers Jul 08 '18 at 14:26
  • I'm not sure exactly what point you're trying to make so perhaps I should elaborate. I want each year to be shown in its own frame. So the first frame would be a single violin plot using the data from 2000, then the next frame, the same but for 2001's data. Since the year variable is encoded in the frame aesthetic, I don't want it to be encoded in the x aesthetic too as that's redundant. So I would like every frame to have its plot centred so that you can compare them year by year. – Tim Hargreaves Jul 08 '18 at 15:01
  • *"Since the year variable is encoded in the frame aesthetic, I don't want it to be encoded in the x aesthetic too as that's redundant."* But that's not what you're doing! Take a look at your GIF: the position of every violin plot changes along the x axis with increasing years. I'm very confused as to what you're trying to achieve. This seems more of an issue with understanding how `gganimate` works than with "position of violin plots"? – Maurits Evers Jul 08 '18 at 21:20
  • I'm aware that is what I've done but that is not what I intended to do. I'm not sure of why I get this result though, hence why I ask the question. I've edited to give an example of exactly what I want. Perhaps I am misunderstanding how gganimate works. I was under the impression that for each value of the frame aesthetic the plot is regenerated using a dataset filtered to that frame value and then these are combined together to make an animation. This is why I set x = 0 so that hopefully each frame will be centred. If this is not the use of gganimate, is there another function more suited? – Tim Hargreaves Jul 09 '18 at 06:56

1 Answers1

2

1

Just use transition_states() or transition_time().

Code

ggplot(df, aes(x = 0, y = value)) +
    geom_violin() +
    transition_states(year, transition_length = 3, state_length = 1) +
    labs(title = "{closest_state}")

Data

set.seed(1701)
df <- data.frame(year = c(rep(2000:2010, each = 50)),
                 value = rnorm(550))
Roman
  • 4,744
  • 2
  • 16
  • 58