2  R Playground

library(ggplot2)
library(palmerpenguins)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
βœ” dplyr     1.1.4     βœ” readr     2.1.5
βœ” forcats   1.0.0     βœ” stringr   1.5.1
βœ” lubridate 1.9.3     βœ” tibble    3.2.1
βœ” purrr     1.0.2     βœ” tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
βœ– dplyr::filter() masks stats::filter()
βœ– dplyr::lag()    masks stats::lag()
β„Ή Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

2.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.

2.2 Playground

2.3 Things you can do

This WebR instance has three packages installed:

  • ggplot2
  • 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")