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:
<- ggplot(penguins, aes(species, fill=species)) +
fig geom_bar()
fig
And break this down in a facet plot, by sex:
<- ggplot(penguins, aes(species, fill=species)) +
fig geom_bar() +
facet_wrap(~sex)
fig
We can make a box and whisker plot of penguin body mass by species:
<- ggplot(penguins, aes(x=species, y=body_mass_g, fill=species)) +
fig geom_boxplot()
fig
And plot the body mass for each sex side-by-side
<- ggplot(penguins, aes(x=species, y=body_mass_g, fill=sex)) +
fig geom_boxplot()
fig
We can investigate correlations, such as between body mass and flipper length:
<- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm)) +
fig geom_point()
fig
We can colour datapoints by species:
<- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
fig geom_point()
fig
And fit a linear regression to each species separately:
<- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
fig geom_point() +
geom_smooth(method="lm")
fig
R
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")