Today we will use the cats dataset to explore different
ways of visualising data in R.The cats dataset from the MASS package
contains measurements from 144 adult cats. For each cat, researchers
recorded its sex, body weight, and heart weight.
The dataset contains three variables:
Sex — sex of the cat (F = female, M = male) Bwt — body weight in kilograms (kg) Hwt — heart weight in grams (g)
Because the dataset contains both categorical and continuous variables, we can use it to explore several different types of data visualisation.
We will look at:
library(MASS)
library(ggplot2)
library(ggcats)
data(cats)
head(cats)
## Sex Bwt Hwt
## 1 F 2.0 7.0
## 2 F 2.0 7.4
## 3 F 2.0 9.5
## 4 F 2.1 7.2
## 5 F 2.1 7.3
## 6 F 2.1 7.6
The dataset contains three variables:
Sex — Female (F) or Male
(M)Bwt — Body weight (kg)Hwt — Heart weight (g)There are 144 cats in the dataset.
Sex is a categorical variable.
ggplot(
cats,
aes(x = Sex)
) +
geom_bar(
width = 0.6
) +
labs(
title = "Number of Male and Female Cats",
x = "Sex",
y = "Number of Cats"
) +
theme_classic(base_size = 14)
Here we have:
Sex → categoricalBwt → continuousset.seed(123)
ggplot(
cats,
aes(
x = Sex,
y = Bwt
)
) +
geom_boxplot(
width = 0.5,
outlier.shape = NA
) +
geom_cat(
cat = "mouth",
size = 1.5,
position = position_jitter(
width = 0.12,
height = 0
)
) +
labs(
title = "Body Weight of Male and Female Cats",
subtitle = "Each cat represents one observation",
x = "Sex",
y = "Body Weight (kg)"
) +
theme_classic(base_size = 14)
A boxplot shows the:
We can visualise the same variables using a violin plot.
set.seed(123)
ggplot(
cats,
aes(
x = Sex,
y = Bwt
)
) +
geom_violin(
trim = FALSE
) +
geom_cat(
cat = "grumpy",
size = 1.5,
position = position_jitter(
width = 0.10,
height = 0
)
) +
labs(
title = "Distribution of Cat Body Weight",
subtitle = "Each cat represents one observation",
x = "Sex",
y = "Body Weight (kg)"
) +
theme_classic(base_size = 14)
The width of the violin represents the estimated density of observations.
Wider areas indicate where observations are more concentrated.
Here we have:
Bwt → continuousHwt → continuousggplot(
cats,
aes(
x = Bwt,
y = Hwt
)
) +
geom_cat(
cat = "shironeko",
size = 1.5
) +
labs(
title = "Do Heavier Cats Have Heavier Hearts?",
subtitle = "Each cat represents one observation",
x = "Body Weight (kg)",
y = "Heart Weight (g)"
) +
theme_classic(base_size = 14)
| Variables | Plot |
|---|---|
| One categorical | 📊 Bar plot |
| Categorical + continuous | 📦 Boxplot |
| Categorical + continuous | 🎻 Violin plot |
| Two continuous | 📈 Scatterplot |
In this exercise, we used:
MASS::cats as our datasetggplot2 to create graphsggcats to represent observations with catsgeom_bar() for a categorical variablegeom_boxplot() for categorical + continuous
variablesgeom_violin() to visualise distributionsgeom_cat() for individual observationsNow it’s your turn to create a graph from scratch.
Do male and female cats differ in their heart weight?
Using the cats dataset, write your own
ggplot2 code to create an appropriate visualisation.
Your graph should include:
SexHwtgeomgeom_cat()# Write your code below!