I would like to sample points from a normal distribution, and then build up a dotplot one by one using the gganimate
package until the final frame shows the full dotplot.
A solution that works for larger datasets ~5,000 - 20,000 points is essential.
Here is the code I have so far:
library(gganimate)
library(tidyverse)
# Generate 100 normal data points, along an index for each sample
samples <- rnorm(100)
index <- seq(1:length(samples))
# Put data into a data frame
df <- tibble(value=samples, index=index)
The df looks like this:
> head(df)
# A tibble: 6 x 2
value index
<dbl> <int>
1 0.0818 1
2 -0.311 2
3 -0.966 3
4 -0.615 4
5 0.388 5
6 -1.66 6
The static plot shows the correct dotplot:
# Create static version
plot <- ggplot(data=df, mapping=aes(x=value))+
geom_dotplot()
However, the gganimate
version does not (see below). It only puts the dots on the x-axis and doesn't stack them.
plot+
transition_reveal(along=index)
Something similar to this would be ideal: Credit: https://gist.github.com/thomasp85/88d6e7883883315314f341d2207122a1
I think the key here is to imagine how you would create this animation manually, which is to say you would add dots one observation at a time to the resulting dotplot. With this in mind, the approach I used here was to create a
ggplot
object that consisted of plot layers = number of observations, then step through layer by layer viatransition_layer
.Note that I set
keep_layers=FALSE
to avoid overplotting. If you plot the initialggplot
object, you'll see what I mean, since the first observation is plotted 100 times, the second 99 times... etc.What about scaling for larger datasets?
Since number of frames = number of observations, you need to adjust for scalability. Here, just keep the # frames constant, meaning you have to let the code group the frames into segments, which I am doing via the
seq()
function, specifyinglength.out=100
. Note also in the new example, the dataset containsn=5000
. In order to keep the dotplot in frame, you need to make the sizes of the dots really tiny. I probably made the dots a bit too small here, but you ge the idea. Now the # frames = number of groups of observations.Try this. The basic idea is to the group the obs to frames, i.e. split by index and then accumulate the samples to frames, i.e. in frame 1 only the first obs is shown, in frame 2 obs 1 and 2, ..... Perhaps there is a more elegant way to achieve this, but it works:
Created on 2020-04-27 by the reprex package (v0.3.0)
Another option is to draw the points with another geom. you will need to do some counts on your data first (and binning) but it doesn’t require making your data longer.
For example, you can use
geom_point
, but the challenge will be to get the dimensions of your points right, so they touch/do not touch. This depends on the device / file size.But you can also just use
ggforce::geom_ellipse
to draw your dots:)geom_point (trial and error with device dimensions)
geom_ellipse (Full control of point size)
update in the link which you provide to thomas's amazing example, you can see that he uses a similar approach - he uses geom_circle instead of geom_ellipse, which I chose because of better control for both vertical and horizontal radius.
To get the "falling drops" effect, you will need
transition_states
and a long duration and many frames per second.Created on 2020-04-29 by the reprex package (v0.3.0)
some inspiration from: ggplot dotplot: What is the proper use of geom_dotplot?