banner



How To Combine To Animations Plots In A Same Window In Comsol

Leveraging the power of gganimate and magick to combine blithe plots for your viewers

In this tutorial, I'm going to outline the steps necessary to create an blithe, faceted plot in R. Although rare, combining blithe plots tin can be a powerful way to showcase dissimilar elements of the same information (as yous'll see beneath).

In this example, I'k using weightlifting data from the International Powerlifting Federation. For the purposes of this tutorial, we'll look at differences in acme lifts past sex. A faceted, animated plot is a great option because nosotros'd like to observe the magnitude of these differences and how these differences have evolved over fourth dimension.

We'll be creating this GIF:

Surroundings Setup

These are the packages we'll demand to get started. In my case, I apply a custom theme I've developed for stylistic purposes. Experience free to instead run theme_set(theme_minimal()) rather than employ my theme!

Load and Clean Data

I've already done a lot of the data cleaning for you. If you'd like to follow along, here'due south the process (or, skip ahead!).

Here, we'll do some small cleaning then reshape the 3 lifts into i cavalcade:

For my visualization, I'm only concerned with the heaviest lifts from each year:

The commencement visualization we'll create for the final output is a dumbbell plot. Curious what that is, or how to make information technology in R? Check out my other post on the topic.

In order to construct a dumbbell plot, we need both male and female observations in the aforementioned row. For this, we use the spread part.

Now, let's construct a dataframe for each sex:

And bring together them:

Not following along, or desire to check your progress? Here'due south what our information looks like in its final grade:

Visualize!

Finally, we can construct the visualization.

First, nosotros tin can create a static visualization using ggalt (once more, my blog post covers the details of this step).

You lot can fast forward the creation of individual plots if y'all're only interested in the combination of the two. You'll find that at the terminate of this post!

Finally, we animate, using Thomas Pedersen's wonderful gganimate packet. This is a relatively piece of cake pace, because gganimate only requires two extra lines of lawmaking: transition_states and ease_aes. Then, nosotros pass it into the animate role!

Merely in our case, we'd like to include some other GIF: a line nautical chart of differences over time.

Finally, nosotros'll combine them using magick (thanks to this post). This is where you'll find the code necessary to do the combination (no matter what your animated plots look like, this should work!).

What's happening here? Essentially, nosotros're using the power of magick to:

  1. (Lines 2–3) Read in all of the private images (image_read) from each GIF (subsequently all, a GIF is simply a series of images!).
  2. (Line 5) For the first frame, stack the 2 images on top of each other (image_append), so plot i is in a higher place plot ii.
  3. (Lines 6–ix) For the rest of the frames (in my example, the next 249, because my GIF had 250 frames), replicate this and combine it with the first frame (this is the for loop).

Here, nosotros specify stack = TRUE so that one plot is to a higher place the other. If yous'd similar to place them side-by-side, specify stack = Faux.

In combination, the process results in our final output:

In this view, nosotros can run into the magnitude of the differences both relatively and admittedly (elevation nautical chart), and we can run into how these differences modify over time (bottom chart). The power of an animated, combined chart!

Source: https://towardsdatascience.com/how-to-combine-animated-plots-in-r-734c6c952315

Posted by: allsupwhady1948.blogspot.com

0 Response to "How To Combine To Animations Plots In A Same Window In Comsol"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel