Lectorial 2 P3
Lectorial 2 P3
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
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
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
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.
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.
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.
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