library(tidyquintro) library(learnr) library(gradethis) knitr::opts_chunk$set(echo = FALSE, exercise.warn_invisible = FALSE) # enable code checking tutorial_options(exercise.checker = grade_learnr)
This is a tutorial page, made specifically for this webinar using the learnr package. Here there are exercises you can work through to help you understand the topics we have covered. Each exercise is in a small R-console within the tutorial. These function as any R console, and you can interact with is as any R-session. The R consoles have all of the tidyverse and the penguins dataset preloaded for you.
You can try that below, just to get acquainted with it.
For instance, try looking at the penguins dataset by typing penguins
, or taking the mean of any column by typing mean(penguins$flipper_length_mm)
# Type in any command you like, and press "run". # continue to the next section when you like
Make a scatter plot of the penguins
data set with bill length on the x-axis and bill depth on the y.
ggplot(data = __) + geom_point(mapping = aes(x = __, y = __))
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm))
grade_code( correct = random_praise(), incorrect = random_encouragement() )
The name of the data object is `penguins`
If you forgot the column names, try looking at the data by typing the
data object name `penguing` in the console and select "run".
Now let us add colour by mapping colour to the species
column.
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm, colour = ___))
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm, colour = species))
grade_code( correct = random_praise(), incorrect = random_encouragement() )
You can set either the `color` or `colour` option.
Since the colour is being based on values in a column of the data set,
the `colour` argument must be placed within the `aes()` function.
Now that the colour is based on species, we'd like to alter these colours to something more exciting than the default ggplot2 colours.
Here, let's try to add the viridis
colour palette for discrete (d) values.
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) + scale_colour____d()
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) + scale_colour_viridis_d()
grade_code( correct = random_praise(), incorrect = random_encouragement() )
When applying colour or fill alterations, make sure you apply the correct type of aesthetic scale for your use (i.e colour and fill are two different things, also for scales)
you are looking for the `scale_colour_viridis_d()` function
The grey background, and dark grey font is a dead give-away that the plot is a ggplot.
Most of us like to adapt the general appearance of plots, either to our own personal taste or to adhere to some journals' requirements.
In ggplot, this is handled by themes
.
There are many built-in different themes within ggplot it self, and there are numerous packages with extra themes, and tutorials on how to adapt your own theme.
Here, try out different themes and see what they look like.
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm, colour = species)) + scale_colour_viridis_d() + theme__()
All the themes start with `theme_`, explore them by typing that then "tab" to see all possible options.
Have a go at just playing around with the ggplot functions and see what different types of plots you can make.
Try different geoms, themes, scales etc.
There are also a lot of other aesthetics you can set, like alpha
, size
, etc.
Different geoms can have somewhat different aesthetics you can set, explore them!
ggplot(data = penguins) + geom__() + theme__()
Try different geoms!
Some geoms have different aesthetics, so just play around.
Try different scales!
There are scales also for x and y, and other aesthetics. See what fun things you can do.
Maybe you want to change the labels? Try adding `labs()` as a layer""
quiz( question("Which of the R packages listed below are also used to create plots?", answer("lattice", correct = TRUE), answer("tools"), answer("stats"), answer("grid", correct = TRUE), allow_retry = TRUE ), question("When you want to fix a ggplot aesthetic to a single value, you do this by...", answer("'mapping' values using the `aes()` function"), answer("adapting extra plot appearence through themes and scales"), answer("'setting' values outside the `aes()` function", correct = TRUE), allow_retry = TRUE ), question("When you want to make a ggplot aesthetic to a vary based on columns in the data set, you do this by...", answer("'mapping' values using the `aes()` function", correct = TRUE), answer("adapting extra plot appearence through themes and scales"), answer("'setting' values outside the `aes()` function"), allow_retry = TRUE ), question("When you want to alter the 'look' a ggplot, you do this by...", answer("'mapping' values using the `aes()` function"), answer("adapting extra plot appearence through themes and scales"), answer("'setting' values outside the `aes()` function", correct = TRUE), allow_retry = TRUE) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.