Package and Data Loading
As mentioned within the session setup, load the following packages using the
library() function. Additionally, as we will be using a data set with large numbers, set scipen to 999 using the option function.
library(tidyverse) library(RColorBrewer) library(ggpubr) options(scipen = 999)
Next load in the data, make relevant adjustment converting the Continent String variable into a Factor variable.
WDB_1999 <- read.csv(".../data/WDB_1999.csv") WDB_1999$Continent <- factor(WDB_1999$Continent)
Section 1: Specifying the Visual Space
Exercise 1: Using the example code provided, add two of the themes listed below. How are these themes different? Under what situations would one of these themes be better than another?
ggplot() + geom_point(data = WDB_1999, mapping = aes(x = lifeexp.ma, y = lifeexp.fe, colour = Continent))
Exercise 2: Using your preferred theme function from exercise 1; add another theme layer (using theme()), specifying a novel background colour, you can use the template provided. Simply replace “XXX” with a colour of your choice.
ggplot() + geom_point(data = WDB_1999, mapping = aes(x = lifeexp.ma, y = lifeexp.fe, colour = Continent)) + theme_() + ## Replace this with your theme of choice theme(plot.background = element_rect("XXX"))
Exercise 3: Going Further: run the command “?theme”. When doing so, explore the large range of potential adaptions, spend some time deploying these to understand the potential adaptions you can make. When doing so, do not forget to provide the following where relevant:
Section 2: Specifying the axis’s
Exercise 4: Using an alternative to theme, increase the limits of the x & y axis. Hint: Use lims(y = c(), x = c()), placing the limits at 0 & 100
ggplot() + geom_point(data = WDB_1999, mapping = aes(x = lifeexp.ma, y = lifeexp.fe, colour = Continent)) + theme_minimal() + lims(y = c(,), x = c(,))
Exercise 5: Currently the produced graph breaks the axis scale every 25 years, using the example provided, use scale_y_continuous & scale_x_continuous. To specify both limits and specific breaks (every 20) in the axis, you can use, n.breaks or breaks to specify the breaks.
ggplot() + geom_point(data = WDB_1999, mapping = aes(x = lifeexp.ma, y = lifeexp.fe, colour = Continent)) + theme_minimal() + scale_x_continuous() + scale_y_continuous()
Exercise 6: Using the code you generated in Exercise 5, add in specification of the angle of break labels using axis.text.x and axis.text.y respectively. Ensure to add these in a theme() layer.
ggplot() + geom_point(data = WDB_1999, mapping = aes(x = lifeexp.ma, y = lifeexp.fe, colour = Continent)) + theme_minimal() + theme(,)
Exercise 7: Now considering the relationship between population and life expectancy. Plot these two variables using a scatter plot, using a log10 scale for population.
ggplot() + geom_point(data = WDB_1999, mapping = aes(x = Pop, y = lifeexp, colour = Continent)) + theme_minimal()
Section 3: Specifying Labels
Exercise 8: Using the layer definition labs(), generate names for the x, y and title, for the plot in exercise 7
Exercise 9: Using the plot generated in Exercise 8, indicate the nation with the lowest life expentancy, adding an annotation, labeling “Sierra Leone” at x = 1000000, y = 42
Exercise 10: Add two reference lines (indicating the UK) in life expectancy and Population, build this upon exercise 9, and use x & y intercept points as x = 58682466, y = 77.39024
Section 4: Specifying Layouts
Exercise 11: Using facet_grid(), generate a matrix of panels for the multiple continents, build this upon Exercise 10. Consider the issue(s) with using this facet style?
Exercise 12: Generate the following plots, assigning each to a relevant variable name (plot_1 : plot_4) Using ggarrange, combine these four plots into one visualisation
For each ensure to set colour as continent, and that they share a common legend, setting common.legend = TRUE Throughout, use all your knowledge adding information wherever required