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:
ggplot2GGallytidyversepalmerpenguins
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()
figAnd break this down in a facet plot, by sex:
fig <- ggplot(penguins, aes(species, fill=species)) +
geom_bar() +
facet_wrap(~sex)
figWe 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()
figAnd plot the body mass for each sex side-by-side
fig <- ggplot(penguins, aes(x=species, y=body_mass_g, fill=sex)) +
geom_boxplot()
figWe 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()
figWe can colour datapoints by species:
fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
geom_point()
figAnd 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")
figR comes with a number of example datasets you can practice with, including:
mtcars: fuel consumption and other statistic for 32 automobilesTitanic: 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")