class: center, middle, inverse, title-slide .title[ # Visualising gapminder data with R ] .author[ ### HASS Taskforce ] .date[ ### 2025-05-29 ] --- # First Install and load packages, read gapminder data into memory ``` r # If not already installed, run # `install.packages('gapminder')` first library(gapminder) # If not already installed, run # `install.packages('tidyverse')` first library(tidyverse) # Load gapminder data into memory, save # it in dataframe called `gm` gm <- gapminder ``` --- # Make the figure canvas .left-code[ ``` r # the first argument ggplot takes is # the dataframe ggplot(gm) ``` ] .right-plot[  ] --- # Add x-axis .left-code[ ``` r ggplot(gm, aes(x = continent)) ``` `aes` specifies the mapping of dataframe variables to different layers of the figure, such as axes, geometries, color, etc. ] .right-plot[  ] --- # One dimension plot .left-code[ ``` r ggplot(gm, aes(x = continent)) + geom_bar() ``` `geom_bar()` specifies the `geometry`, or type of plot (in this case, a bar plot) ] .right-plot[  ] --- # Two dimensions .left-code[. Plus: add `theme_minimal` to remove ugly default gray background ``` r ggplot(gm, aes(x = year, y = lifeExp)) + geom_point() + theme_minimal() ``` `theme_minimal()` is one of [many](https://ggplot2.tidyverse.org/reference/ggtheme.html) in-built themes to explore ] .right-plot[  ] --- # Three dimensions .left-code[ Plus: add x and y labels ``` r ggplot(gm, aes(x = year, y = lifeExp, color = continent)) + geom_point() + theme_minimal() + labs(x = "Year", y = "Life expectancy") ``` ] .right-plot[  ] --- # Four dimensions .left-code[ ``` r ggplot(gm, aes(x = year, y = lifeExp, color = continent, size = pop)) + geom_point() + theme_minimal() + labs(x = "Year", y = "Life expectancy") ``` ] .right-plot[  ] --- # 4.5 dimensions: lines through countries .left-code[ The `size` aesthetic only applies to the points, not to the lines, so we move it into the call to `geom_point`. ``` r ggplot(gm, aes(x = year, y = lifeExp, color = continent)) + geom_point(aes(size = pop)) + geom_line() + theme_minimal() + labs(x = "Year", y = "Life expectancy") ``` Why did that not work? ] .right-plot[  ] --- # Group country data together .left-code[ To draw lines, we need the `group` aesthetic, otherwise ggplot doesn't know which rows are connected due to belonging to the same country ``` r ggplot(gm, aes(x = year, y = lifeExp, color = continent, group = country)) + geom_point(aes(size = pop)) + geom_line() + theme_minimal() + guides(size = FALSE) + labs(x = "Year", y = "Life expectancy") ``` `guides(size = FALSE)` hides the legend for size, as it isn't very informative ] .right-plot[  ] --- # Five dimensions .left-code[ Introducing facets ``` r ggplot(gm, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + facet_wrap(~year) + geom_point() + theme_minimal() + guides(size = FALSE) + labs(x = "GDP per capita", y = "Life expectancy") ``` Facet wrap allows you to make a subplot for each value of a variable, in this case, year. ] .right-plot[  ] --- # Can we go higher? .left-code[ What about the cool animated visualisation Hans Rosling shows in his video? If not already installed, run `install.packages("gganimate")` and `install.packages("gifski")` ``` r library(gganimate) library(gifski) animation <- gm %>% ggplot(aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() + theme_minimal() + guides(size = FALSE) + transition_time(year) + labs(title = "Year: {frame_time}", x = "GDP per capita", y = "Life expectancy") animate(animation, duration = 10, fps = 20, renderer = gifski_renderer()) ``` `transition_time` allows you to animate the plot over time, in this case, over the variable year ] .right-plot[  ]