R language dynamic visualization: drawing the cumulative dynamic line graph and animated GIF video graph of historical global average temperature

Time:2021-4-8

Link to the original text:http://tecdat.cn/?p=9766

In some cases, you may want to animate by adding data to each frame and keeping the previously added data.

Now, we’ll explore it by animating the point line diagram.

Here is the code for making a static version of the chart:

#Read data
warming <- read_csv("warming.csv")

#Drawing
warming_plot <- ggplot(warming, aes(x = year, y = value)) +
  geom_line(colour="black") +
  geom_point(shape = 21, colour = "black", aes(fill = value), size=5, stroke=1) +
  scale_x_continuous(limits = c(1880,2017)) +
  scale_y_continuous(limits = c(-0.5,1)) +
  scale_fill_distiller(palette = "RdYlBu", limits = c(-1,1), guide = FALSE) +
  xlab("") +
  ylab("Difference from 1900-2000 (ºC)") +
  theme_minimal(base_size = 16, base_family = "Georgia")

The result should be:

R language dynamic visualization: drawing the cumulative dynamic line graph and animated GIF video graph of historical global average temperature

The documentwarming.csvInclude fieldsyearandvalueThe latter is the global average annual temperature, compared with the average level of 1900-2000.

Because this is a point line diagram, it includesgeom_lineAndgeom_pointLayer. We can set the contourcolorBlack, thenaesUse the map to fill it with color based on the temperaturevalue

This code is used forscale_fill_distillerUse the colorbrewer palette, which ranges from cold blue, neutral yellow to warm red, and apply them to a range of values from – 1 to + 1.

Similarly, we can animate the data:

How code works

  • transition_reveal. WhenalongWhen each value of the time variable is added to the chart, this preserves the previously displayed data.idYou can use it to create separate rows for multiple categories by making it equal to the category variable in question; otherwise, use theid = 1

transition_revealThe default is to display lines and draw only the points of the current frame

R language dynamic visualization: drawing the cumulative dynamic line graph and animated GIF video graph of historical global average temperature

To create a cumulative animation of points, use the following code:

  • shadow_markKeep the data in the previous frame.

R language dynamic visualization: drawing the cumulative dynamic line graph and animated GIF video graph of historical global average temperature

Use the for loop to draw and save annual charts

To make cumulative animation of points and lines, we need to write a loop to create a separate image for each frame.

For loop:

#List the years from 1880 to 2017
years <- c(1880:2017)

#The for loop outputs to the console every year, pausing one second at a time
for (y in years) {
  print(y)
  Sys.sleep(1) 
}

Output:

[1] 1880
[1] 1881
[1] 1882
[1] 1883
[1] 1884
[1] 1885
[1] 1886
[1] 1887

The first line of the code creates a list of integers ranging from 1880 to 2017.

This part of the code will traverse each entry in the list:for (y in years)

The code uses the same principle to plot and save the annual chart:

How does the code work

For every year,yThe code first makes an R object called R.

Then, it creates an R object namedchart, which is a static drawing from that dataggplot2Fig.

Then, use theggsaveFunction to save the chart at the defined size and resolution to update the progress on the loop.

TheggsaveThe code uses the R functionpaste0

Merge frames into GIF and video

First, make GIF with

#Making GIF with ImageMagick
system("convert -delay 10 charts/*.jpg warming2.gif")

#Increase the delay of the last frame of GIF
system("convert warming2.gif ( +clone -set delay 300 ) +swap +delete warming2_with_pause.gif")

This code creates a GIF calledwarming2.gif. Now make the video:

#Making video with ffmpeg
system("ffmpeg -f image2 -start_number 1880 -i charts/%d.jpg -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' warming.mp4")

#Change the speed of the video
system("ffmpeg -i warming.mp4 -vf 'setpts=2*PTS' warming_slow.mp4")

image2Create a video from a sequence of images.

Animation is made to switch between simulating human impact on global average temperature and natural impact

Loop animation can also be used to switch between filtered views of different states or data. To illustrate this, we will load NASA data, which shows simulations from climate models that compare how global mean temperature will change under the influence of natural events (for example, changes in radiation from the sun and cooling effects of soot from volcanic eruptions). The impact on human beings is mainly the emission of carbon dioxide and other greenhouse gases.

How code works

  • transition_state. This switches between different filtering states of the data, defined here by variablestype
  • ease_aesWhen using state transition animation, use options that change the transition speed
  • "{closest_state}"stayggtitleFunction to display the appropriate value of the variable used to define the state heretype

The GIF should look like this:

R language dynamic visualization: drawing the cumulative dynamic line graph and animated GIF video graph of historical global average temperature


R language dynamic visualization: drawing the cumulative dynamic line graph and animated GIF video graph of historical global average temperature

Most popular insights

1.R language dynamic graph visualization: how to create a graph with beautiful animation

2.Visual analysis of R language survival analysis

3.Python data visualization – Seaborn iris iris data

4.R language is used to simulate and dynamically simulate the experiment of Buffon injection

5.R language survival analysis data analysis visualization case

6.R language data visualization analysis case: exploring BRFSS data analysis

7.R language dynamic visualization: making the cumulative dynamic line graph animation GIF video map of historical global average temperature

8.Dimension reduction and visualization analysis of principal component PCA and t-sne algorithm for R language high dimensional data

9.Python topic LDA modeling and t-sne visualization