0% found this document useful (0 votes)
5 views27 pages

Lectorial 2 P3

The document discusses the creation of animated data visualizations (DVs) using R, illustrating the process with a step-by-step guide to animate GDP per capita bar charts from 1960 to 2018. It covers data importation, data manipulation, chart creation, color assignment, and the animation process using loops and the Sys.sleep() function. Additionally, it encourages practice with animation techniques and offers examples of potential applications for animated DVs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Lectorial 2 P3

The document discusses the creation of animated data visualizations (DVs) using R, illustrating the process with a step-by-step guide to animate GDP per capita bar charts from 1960 to 2018. It covers data importation, data manipulation, chart creation, color assignment, and the animation process using loops and the Sys.sleep() function. Additionally, it encourages practice with animation techniques and offers examples of potential applications for animated DVs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

DATA VISUALISATION AND

COMMUNICATION
TERM 3, 2022
LECTORIAL 2 (CONTINUED)
ANIMATED GRAPHICS
HOW DOES ANIMATION WORK?
The Galloping Horse was the first movie ever made (in 1878).
The photographer just took many many consecutive photos of a
galloping horse…

Source:
https://commons.wikimedia.org/
wiki/File:Muybridge_race_horse_
gallop.jpg

When you show all the photos, one after another, very quickly,
you have a movie
HOW DOES ANIMATION WORK?
Here is a gif showing the “moving picture”version of the photos

https://commons.wikimedia.org/wiki/File:Muybridge_race_horse_gallop.jpg
HOW DOES ANIMATION WORK?
We can use the same process to create animated DVs

We can create multiple consecutive graphs


One for year 1, one for year 2, one for year 3, etc

And the display these graphs, one after another, quite rapidly, to make an
animated DV.
HOW DOES ANIMATION WORK?
We can use the same process to create animated DVs

We can create multiple consecutive graphs


One for year 1, one for year 2, one for year 3, etc

And the display these graphs, one after another, quite rapidly, to make an
animated DV.
HOW DOES ANIMATION WORK?
We can do this in R – here is an example of a animated bar chart,
Which shows the ranking of 15 countries, by GDP per capita,
Over the years 1960 to 2018.
HOW DOES ANIMATION WORK?
Now we will go through a step by step demonstration for creating
this animated graph.
STEP 1 – IMPORT YOUR DATA INTO R
First we import the data we want to graph.
This data comes from an online dataset which I downloaded.
The data is stored in CSV file (CSV = comma separated values)
We can use R’s read.csv function to read the data into a dataframe

OurData <- read.csv("C:\\Users\\ferri\\OneDrive\\Desktop\\UNSW 2022\\Data


Visualisation\\Moving Chart Example by Shauna\\gdp-per-capita-maddison-2020.csv")

{Note: If you are using R Studio, it is easier to import your data}


STEP 2 – DISPLAY YOUR DATAFRAME
Show the first few lines of our Dataframe
> head(OurData)
Entity Code Year GDPperCapita
1 Afghanistan AFG 1950 1156
2 Afghanistan AFG 1951 1170
3 Afghanistan AFG 1952 1189
4 Afghanistan AFG 1953 1240
5 Afghanistan AFG 1954 1245
>
STEP 3 – TAKE A SUBSET
Check how many rows are in our Dataframe
> nrow(OurData)
[1] 19876
This is too much data about too many countries!
I will now take a subset of the dataframe, which just has
• a selected group of countries, and
• only stores data from 1960 or thereafter
Q. How can we do this?
STEP 3 – TAKE A SUBSET
I will now take a subset of the dataframe, which just has
• a selected group of countries (as specified in CountrySet), and
• only stores data from 1960 or thereafter
Note the R operator %in% which means “is in the set”

CountrySet <- c("China", "Japan", "United States", "Canada", "Australia", "Italy",


"Germany", "France", "Brazil", "India", "Bolivia", "Botswana", "Mexico")

Subset <- OurData[OurData$Entity %in% CountrySet & OurData$Year >= 1960,]


STEP 4 – CREATE A HORIZONTAL BAR CHART
Now suppose I want to create a horizontal bar chart, showing GDP per Capita, for
the year 1960 (or any other year we like)

ThisYear = 1960
DataThisYear <- Subset[Subset$Year == ThisYear,]
barplot(height = DataThisYear$GDPperCapita,
names.arg = DataThisYear$Code,
horiz = TRUE)
STEP 4 – CREATE A HORIZONTAL BAR CHART
This makes a very ugly graph,
which is not very helpful for
comparing countries
The graph is shown in
alphabetical order by name of
country
.
Q. What can we do to make
this better?
STEP 4 – CREATE A HORIZONTAL BAR CHART
Q. What can we do to make
this better?
• Add a heading
• Turn country names
sideways
• Sort the data to show
ranking of each country
• Add colors
STEP 4 – CREATE A HORIZONTAL BAR CHART
Now sort the dataframe by the GDP per Capita
And add some labels etc
ThisYear = 1960
DataThisYear <- Subset[Subset$Year == ThisYear,]
DataThisYear <- DataThisYear[order(DataThisYear$GDPperCapita),]
barplot(height = DataThisYear$GDPperCapita, names.arg = DataThisYear$Code,
horiz = TRUE, las = 2,
main = paste("GDP PER CAPITA",ThisYear),
xlim = c(0,60000))
STEP 4 – CREATE A HORIZONTAL BAR CHART
STEP 4 – ASSIGN COLORS
In our bar chart, we will want to use a
different color bar for each country.

For example, we might set


China = red, Japan = yellow, USA = pink

So let’s choose 15 different colors, to


represent the 15 different countries.
STEP 2 – ASSIGN COLORS
You can use the colors() function to get a list of all the colors in R (more than 600 colors)
> colors()
[1] "white" "aliceblue" "antiquewhite"
[4] "antiquewhite1" "antiquewhite2" "antiquewhite3"
[7] "antiquewhite4" "aquamarine" "aquamarine1"
[10] "aquamarine2" "aquamarine3" "aquamarine4"
[13] "azure" "azure1" "azure2"
[16] "azure3" "azure4" "beige"
[19] "bisque" "bisque1" "bisque2"
[22] "bisque3" "bisque4" "black"
[25] "blanchedalmond" "blue" "blue1"
[28] "blue2" "blue3" "blue4"
etc etc
STEP 4 – ASSIGN COLORS
We already have a set of 15 countries
CountrySet <- c("China", "Japan", "United States", "Canada", "Australia", "Italy",
"Germany", "France", "Brazil", "India", "Bolivia", "Botswana", "Mexico")

So now let’s choose 15 different colors, and match these colors up with 15 countries
ColorSet <- c("red", "yellow", "pink", "blue", "green", "purple", "brown", "orange", "black",
"white", "light blue", "light green", "dark red")

Now match up each color to one of the countries, using the names function
This R command will match the nth color in our ColorSet to the nth country in our CountrySet.
names(ColorSet) <- CountrySet
STEP 4 – ASSIGN COLORS
We can now easily look up the color which has been assigned to any country

> ColorSet["China"]
China
"red"

> ColorSet["Japan"]
Japan
"yellow"
STEP 4 – PRODUCE A COLORED BAR CHART
ThisYear = 1960
DataThisYear <- Subset[Subset$Year == ThisYear,]
DataThisYear <- DataThisYear[order(DataThisYear$GDPperCapita),]
CountryColor <- ColorSet[DataThisYear$Entity]
barplot(height = DataThisYear$GDPperCapita, names.arg =
DataThisYear$Code, horiz = TRUE,
col = CountryColor, las = 2,
main = paste("GDP PER CAPITA",ThisYear),
xlim = c(0,60000))
STEP 5 – PRODUCE A COLORED BAR GRAPH

No doubt we could
improve this graph, make
it more attractive etc.

More next week!


STEP 6 - ANIMATION
Now to produce an animated bar chart, we just have to produce a bar chart for each year from
1960 to 2018, and display them one after another. Sys.sleep() controls the timing of the display.

for (ThisYear in 1960:2018){


DataThisYear <- Subset[Subset$Year == ThisYear,]
DataThisYear <- DataThisYear[order(DataThisYear$GDPperCapita),]
CountryColor <- ColorSet[DataThisYear$Entity]

barplot(height = DataThisYear$GDPperCapita, names.arg = DataThisYear$Code,


horiz = TRUE, col = CountryColor, las = 2,
main = paste("GDP PER CAPITA",ThisYear), xlim = c(0,60000))

Sys.sleep(0.05)
}
SEE DEMO
As you can see, you could easily change
• the set of countries included in the bar chart
• the color scheme for each country
• The appearance of the bar chart (labels etc)
• The years to be included
• The speed of the display

And this bar chart could easily be adapted to show moving bar charts for
all sorts of other data (most popular singer, number of covid deaths, air
pollution statistics, whatever) – as long as you have a datafame in the
correct format.

This R script just uses basic R – it could be improved by using more


advanced R packages (such as ggplot).
PRACTICE WITH ANIMATION
library(grid)
cc_color = rainbow(10)
for (ctr in 1:100){
# Create an empty picture
frame()
# for each bouncing ball, choose a random color and amount of bounce
for (location in 1:3){
# choose one of the 10 rainbow colors
cc <- cc_color[runif(1,min=1,max = 10)]
# choose the amount of bounciness
bounce <- runif(1,min=0, max = 0.1)
# specify the location, size, and color of each circle
circle1 <- circleGrob(x = 0.1 + 0.2*location, y = 0.5+bounce, r = 0.05,
gp = gpar(col = cc, fill = cc, lty = 3))
# draw the circle
grid.draw(circle1)
}
# and draw one circle which just represents a bowling ball rolling across the bottom of the screen
circle2 <- circleGrob(x = 0.1 + 0.005*ctr, y = 0.3, r = 0.10,
gp = gpar(col = "black", fill = "black", lty = 3))
grid.draw(circle2)
Sys.sleep(0.1)
}
PRACTICE WITH ANIMATION
# Activities
# how would you change the radius of the bouncing balls
# how would you change the speed of the display (faster)?
# how would you make the balls bounce higher?
# How would you make the larger bowling ball red instead of black?
# How would you ensure that the bouncing balls can only be red, green, or yellow
(like traffic lights)
MORE PRACTICE WITH ANIMATION
This DV shows a very simple Markov Model (healthy sick dead)
We initially have 100 people who are healthy
Each healthy person might become sick at any time (probability 10% p.a.)
Each healthy person might die at any time (probability 1% p.a.)

If a person is sick
In any year, that person has a 40% chance of remaining sick
In any year, that person has a 50% chance of recovering (becoming healthy)
In any year, that person has a 10% chance of dying

If a person dies, they stay dead.

Create an animated DV to illustrate what happens as time passes


[Note – in the long run, we are all dead!]
If you change the probabilities, how does this affect the DV?

You might also like