class: center, middle, inverse, title-slide .title[ # Visualising gapminder data with R ] .author[ ### Dr Maria del Mar Quiroga ] .date[ ### 2023-09-18 ] --- # 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 ``` --- # Making the figure canvas .left-code[ ```r # the first argument ggplot takes is # the dataframe ggplot(gm) ``` ] .right-plot[ ![](index_files/figure-html/canvas-1.png) ] --- # Adding an axis .left-code[ ```r # but the call to ggplot can also be # piped to the dataframe, as so: gm %>% ggplot(aes(x = continent)) ``` `aes` specifies the mapping of dataframe variables to different layers of the figure, such as axes, geometries, color, etc. ] .right-plot[ ![](index_files/figure-html/axis-1.png) ] --- # One dimension plot .left-code[ ```r gm %>% ggplot(aes(x = continent)) + geom_bar() ``` `geom_bar()` specifies the `geometry`, or type of plot (in this case, a bar plot) ] .right-plot[ ![](index_files/figure-html/oned-1.png) ] --- # Two dimensions .left-code[ ```r gm %>% ggplot(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[ ![](index_files/figure-html/twod-1.png) ] --- # Three dimensions .left-code[ ```r gm %>% ggplot(aes(x = year, y = lifeExp, color = continent)) + geom_point() + theme_minimal() + labs(x = "Year", y = "Life expectancy") ``` ] .right-plot[ ![](index_files/figure-html/threed-1.png) ] --- # Four dimensions .left-code[ ```r gm %>% ggplot(aes(x = year, y = lifeExp, color = continent, size = pop)) + geom_point() + theme_minimal() + labs(x = "Year", y = "Life expectancy") ``` ] .right-plot[ ![](index_files/figure-html/fourd-1.png) ] --- # 4.5 dimensions: lines through countries .left-code[ ```r gm %>% ggplot(aes(x = year, y = lifeExp, color = continent)) + geom_point(aes(size = pop)) + geom_line() + theme_minimal() + labs(x = "Year", y = "Life expectancy") ``` This doesn't look right... why? ] .right-plot[ ![](index_files/figure-html/four.5d-1.png) ] --- # 4.5 dimensions: lines through countries .left-code[ Group data together: 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 gm %>% ggplot(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[ ![](index_files/figure-html/lines-1.png) ] --- # "Facets" as another dimension .left-code[ ```r gm %>% ggplot(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[ ![](index_files/figure-html/fived-1.png) ] --- # Can we go higher? .left-code[ 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[ ![](index_files/figure-html/animate-1.gif) ]