🐱 Cat Statistics

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:

  • Bar plots
  • Boxplots
  • Violin plots
  • Scatterplots

📦 Load the Data

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.

📊 1. Bar Plot

How many male and female cats are there?

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)

One categorical variable → Bar plot

📦 2. Boxplot

Does body weight differ between male and female cats?

Here we have:

  • Sex → categorical
  • Bwt → continuous
set.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:

  • Median
  • Interquartile range
  • Spread
  • Potential outliers

Categorical + continuous → Boxplot

🎻 3. Violin Plot

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.

Categorical + continuous → Violin plot

❤️ 4. Scatterplot

Do heavier cats have heavier hearts?

Here we have:

  • Bwt → continuous
  • Hwt → continuous
ggplot(
  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)

Two continuous variables → Scatterplot

🧠 Which Plot Should I Use?

Variables Plot
One categorical 📊 Bar plot
Categorical + continuous 📦 Boxplot
Categorical + continuous 🎻 Violin plot
Two continuous 📈 Scatterplot

🎓 What Did We Learn?

In this exercise, we used:

  • RMarkdown to combine text and R code
  • MASS::cats as our dataset
  • ggplot2 to create graphs
  • ggcats to represent observations with cats
  • geom_bar() for a categorical variable
  • geom_boxplot() for categorical + continuous variables
  • geom_violin() to visualise distributions
  • geom_cat() for individual observations

🐱 Your Turn!

Now it’s your turn to create a graph from scratch.

Question

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:

  • Sex
  • Hwt
  • An appropriate geom
  • Individual cats using geom_cat()
  • A clear graph title
  • Labels for both axes
# Write your code below!

MEOW.