4  R Playground

4.1 Introduction

This page provides a WebR cell for you to use as a playground to experiment with some example datasets. You can use this page to explore data management and visualisation in R.

4.2 Playground

4.3 Things you can do

This WebR instance has three packages installed:

  • ggplot2
  • GGally
  • tidyverse
  • palmerpenguins

Open the callout boxes below to see some examples you can try in the code cell above.

The penguins dataset contains data about three different species of penguins. Take a look at the format of the dataset:

glimpse(penguins)

You’ll see there are eight variables, including species, weight, sex, etc. - some of these variables are categorical (i.e. a category, like species), and others are continuous (i.e. numerical). You can see a visual overview of how the data is related using the plot() function:

plot(penguins)

We can visualise the number of penguins of each species in a bar chart:

fig <- ggplot(penguins, aes(species, fill=species)) +
         geom_bar()
fig

And break this down in a facet plot, by sex:

fig <- ggplot(penguins, aes(species, fill=species)) +
         geom_bar() +
         facet_wrap(~sex)
fig

We can make a box and whisker plot of penguin body mass by species:

fig <- ggplot(penguins, aes(x=species, y=body_mass_g, fill=species)) +
         geom_boxplot()
fig

And plot the body mass for each sex side-by-side

fig <- ggplot(penguins, aes(x=species, y=body_mass_g, fill=sex)) +
         geom_boxplot()
fig

We can investigate correlations, such as between body mass and flipper length:

fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm)) +
         geom_point()
fig

We can colour datapoints by species:

fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
         geom_point()
fig

And fit a linear regression to each species separately:

fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
         geom_point() +
         geom_smooth(method="lm")
fig
Note

R comes with a number of example datasets you can practice with, including:

  • mtcars: fuel consumption and other statistic for 32 automobiles
  • Titanic: information on the fate of passengers on the fatal maiden voyage of the ocean liner Titanic

You can see a full list by running the command

library(help = "datasets")