Activity 28 — PCA II: Loadings, Scores, and the Biplot
What PC2 means, finding the interesting points, and reading arrows
Hands-on companion to the second PCA lecture, on all 1,025 Pokemon, written for students with no previous statistics. Reading a loadings table, the mixed-signs rule, naming PC2 and confirming it from Shuckle and Regieleki, the scores plot coloured by legendary status, hovering versus labelling extremes with ggrepel, building a biplot from the loadings, and writing up a PCA with no p-value. Homework adds height and weight and has students name the axes themselves.
In-class Activity 28: Loadings, Scores, and the Biplot 🔴
Today’s Objectives
- Read a loadings table properly
- Learn the one rule that matters: mixed signs mean a trade-off
- Name PC2 from its loadings — then prove it from the data
- Plot the Pokémon in PC space, coloured by status
- Hover to explore, and label only the interesting points with
ggrepel - Build a biplot by hand, so the arrows are not magic
- Write up a PCA when there is no p-value to lean on
One new package. Small and fast, but do it at home:
install.packages("ggrepel")- In-class skeleton:
28_pca_2_skeleton.R→ save it asscripts/28_pca_2.R - Homework:
28_pca_2_homework.R→ save it under that same name. This is what you turn in. - Data:
pokemon_1025.csv— the same file as last class, in yourdata/folder.
- Same as last class. The skeleton has a numbered section for every Part below.
- ▶ Type this — type the code into that section, then run it.
- ▶ Already in your skeleton — written for you. Just run it.
- The explanations live here, not in the script. 💭 Think lines are for talking over — nothing to write down.
- You do not turn in
scripts/28_pca_2.R. You turn in the homework.
Part 1 · Where we left off, and starting again
Last class: six correlated stats on 1,025 Pokémon, impossible to plot. PCA found new axes along the directions of most spread. PC1 = 44.1%, PC2 = 18.4%, together 62.5%. PC1 had all-positive loadings, so we called it overall power — and checked it against Magikarp and the legendaries.
▶ Already in your skeleton (section 1) — just run it:
library(tidyverse)
library(plotly)
library(ggrepel)
pokemon <- read_csv("data/pokemon_1025.csv") %>%
mutate(status = case_when(
is_legendary ~ "legendary",
is_mythical ~ "mythical",
TRUE ~ "regular"))
poke_stats <- pokemon %>%
select(hp, attack, defense, sp_attack, sp_defense, speed)
poke_pca <- prcomp(poke_stats, center = TRUE, scale. = TRUE)
summary(poke_pca)This is last class’s code, so it is written for you. Nothing today depends on objects left over from last time — restart R, run from the top, get the same answer.
Part 2 · The loadings table
▶ Type this (section 2):
round(poke_pca$rotation[, 1:3], 2)One row per original stat, one column per PC. A loading is how much that stat contributes to that axis: a big number matters a lot, near zero is barely involved, and the sign gives the direction.
PC1 — all the same sign
| Stat | PC1 loading |
|---|---|
sp_defense |
0.44 |
attack |
0.43 |
sp_attack |
0.43 |
hp |
0.42 |
defense |
0.39 |
speed |
0.31 |
Every one positive, all about the same size. To score high on PC1 you must be high on everything.
An axis where every loading shares a sign is a “total” axis — overall size, overall strength, overall abundance. PC1 is very often a total, because the biggest way things differ is usually just how much of them there is.
Part 3 · PC2 has mixed signs
No new code — read the PC2 column of the table you just printed.
| Stat | PC2 loading | |
|---|---|---|
defense |
+0.58 | ⬆️ |
sp_defense |
+0.16 | |
hp |
+0.10 | |
attack |
+0.07 | |
sp_attack |
−0.36 | ⬇️ |
speed |
−0.70 | ⬇️ |
Mixed signs mean a trade-off, not a total. PC2 does not measure how much; it measures which kind — strong one way instead of another.
- High PC2 = high
defense - Low PC2 = high
speedandsp_attack
Naming it
High PC2 is tough but slow. Low PC2 is fast and special-attacking but fragile. Players already have names for these: a tank and a glass cannon. So PC2 is a “tank ↔︎ glass cannon” axis, carrying 18.4% of all the variation.
prcomp() returned a column labelled PC2. It has no idea what a tank is. Naming an axis is interpretation, and it is your job — and the easiest place to fool yourself. So next, check it.
💭 Think: hp and attack are near zero on PC2. What does that say about them — are they part of the trade-off, or not?
Part 4 · Prove the name from the data
▶ Type this (section 4):
poke_scores <- pokemon %>%
mutate(PC1 = poke_pca$x[, 1],
PC2 = poke_pca$x[, 2])
poke_scores %>%
arrange(desc(PC2)) %>%
select(name, defense, speed, PC2) %>%
head(4)
poke_scores %>%
arrange(PC2) %>%
select(name, defense, speed, PC2) %>%
head(4)| End of PC2 | Who is there | Defence | Speed |
|---|---|---|---|
| Top | Shuckle, Stakataka, Steelix, Avalugg | 184 – 230 | 5 – 30 |
| Bottom | Regieleki, Pheromosa, Deoxys, Accelgor | 37 – 50 | 145 – 200 |
Shuckle is a turtle that lives inside a rock: defence 230, speed 5. Regieleki is living electricity: speed 200. They sit at opposite ends of an axis built from nothing but six columns of numbers.
- Read the loadings → form a guess about what the axis means
- Look at the extremes → check the guess against things you already know
If step 2 disagrees with step 1, believe step 2.
Part 5 · The picture, coloured by status
▶ Type this (section 5):
ggplot(poke_scores, aes(x = PC1, y = PC2, color = status)) +
geom_point(alpha = 0.6)- Every dot is one Pokémon. Left ↔︎ right is weak ↔︎ strong (PC1, 44.1%). Bottom ↔︎ top is glass cannon ↔︎ tank (PC2, 18.4%)
- (0, 0) is the average Pokémon, not zero power — the scores were centred
- The legendaries and mythicals sit on the far right, and a little below the middle on PC2. The strongest Pokémon lean slightly towards fast rather than tank
status was never in the PCA
Look back at Part 1: poke_stats has six columns, and status is not one of them. prcomp() never saw it; we only used it to colour the finished result.
That is the right way round. Build the axes from the measurements only, then colour by the thing you care about. Put the group into the PCA and you have built the answer you wanted — the same circular trap as the wētā’s head_length.
💭 Think: Suppose you were studying skulls from two species and put “species” into the PCA as a 0/1 column. What would go wrong?
Part 6 · Hover to explore
▶ Type this (section 6):
pca_plot <- ggplot(poke_scores,
aes(x = PC1, y = PC2, color = status, text = name)) +
geom_point(alpha = 0.6)
ggplotly(pca_plot, tooltip = "text")The same plot as last class — now that you know what both axes mean.
- Hover to find Shuckle alone at the top and Regieleki at the bottom
text = nameis ignored by ggplot (it may warn — that is expected) and picked up byggplotly()
💭 Think: Find two dots right next to each other and hover over both. Look up their six stats. Are they similar Pokémon?
Part 7 · Label only the interesting ones
Hovering is for you, exploring. A figure in a report or poster cannot be hovered — it needs names printed on it. But 1,025 labels is a solid black rectangle, so label only the extremes.
abs(PC1)is distance from the centre, ignoring directionquantile(abs(PC1), 0.99)is the value only 1% of Pokémon exceed- The
|means OR — a Pokémon is kept if it is extreme on either axis
▶ Type this (section 7):
extremes <- poke_scores %>%
filter(abs(PC1) > quantile(abs(PC1), 0.99) |
abs(PC2) > quantile(abs(PC2), 0.99))
nrow(extremes)
ggplot(poke_scores, aes(x = PC1, y = PC2)) +
geom_point(color = "grey75") +
geom_point(data = extremes, color = "firebrick") +
geom_text_repel(data = extremes, aes(label = name), size = 3)22 Pokémon make the cut. Three layers, and the order matters — later layers draw on top:
- all 1,025 points, grey, in the background
- the 22 extremes, on top, in red
- names, only for the extremes
geom_text_repel() is ggrepel’s version of geom_text(): it nudges labels apart and draws a little line back to each dot.
data = inside a layer
Each geom_ can take its own data =. Give it one and that layer ignores the data in ggplot(). That is how you draw two subsets on one plot — and it works for every geom.
💭 Think: Change 0.99 to 0.97 in both places and re-run. There is no correct value — how would you decide?
Part 8 · The biplot
Our plot shows where Pokémon sit. It does not show which way each original stat points. A biplot draws both: points = the Pokémon (scores), arrows = the stats (loadings).
▶ The arrows_df block is already in your skeleton (section 8). Type the ggplot() under it:
arrows_df <- as_tibble(poke_pca$rotation[, 1:2], rownames = "stat") %>%
mutate(PC1 = PC1 * 5,
PC2 = PC2 * 5)
arrows_dfggplot(poke_scores, aes(x = PC1, y = PC2)) +
geom_point(color = "grey80") +
geom_segment(data = arrows_df,
aes(x = 0, y = 0, xend = PC1, yend = PC2),
arrow = arrow(length = unit(0.2, "cm"))) +
geom_text_repel(data = arrows_df, aes(label = stat),
fontface = "bold", size = 3.4)arrows_dfis the loadings table turned into a data frame, one row per stat — each row becomes one arrow from (0, 0)geom_segment()draws a line from(x, y)to(xend, yend);arrow =puts a head on it
* 5
Loadings run about −0.7 to +0.6; the scores run about −4 to +6. At true length the arrows vanish into a dot. Multiplying by 5 is purely cosmetic — it changes the arrows’ lengths on screen, never their directions, and the directions are what you read.
Reading the arrows
- Every arrow points right — all six stats increase with PC1 (the all-positive loadings, made visible)
defensepoints up-right,speedpoints down-right — nearly opposite vertically, which is the trade-off.sp_attackleans withspeedhpandattackpoint almost straight right — they are about power, not the trade-offspeedhas the longest arrow;hpandattackthe shortest
| Arrows | Meaning |
|---|---|
| close together | correlated stats |
| at right angles | unrelated |
| opposite | trade-off |
Part 9 · A trap worth naming
Last class, the correlation between speed and defense was 0.01 — completely unrelated. But on this biplot their arrows point in nearly opposite directions, which looks like a strong trade-off.
Both are true. The biplot is a shadow — two of six dimensions. Two arrows can look opposed in the shadow while being unrelated in the full six.
“PC1 and PC2, which together explain 62.5% of the variance”
Without that number a reader cannot tell whether your biplot is the whole story or two-thirds of it. Never show a biplot without its percentages.
Part 10 · Writing up a PCA
There is no p-value, so the structure is different from everything else this term.
Six battle statistics for all 1,025 Pokémon were standardised and reduced by principal component analysis. The first two components explained 62.5% of the total variance (PC1 44.1%, PC2 18.4%). PC1 loaded positively on all six statistics (0.31 to 0.44) and is interpreted as overall power: the 71 legendary Pokémon averaged PC1 = 2.38 against −0.24 for regular Pokémon, and Magikarp sat among the lowest scores. PC2 contrasted
defense(+0.58) againstspeed(−0.70) andsp_attack(−0.36), separating slow, durable species such as Shuckle and Steelix from fast, fragile ones such as Regieleki and Pheromosa.
What that paragraph does:
- Says what went in — six stats, 1,025 Pokémon — and that it was standardised
- Gives variance explained, per axis and cumulative
- Names each axis, with the loadings that justify the name
- Gives evidence from the data — real Pokémon at each end
- Never says “significant”, because nothing was tested
Too little: “We ran a PCA and PC1 explained 44.1%.” Of what? Meaning what?
Too much: “PC1 proves power is one-dimensional.” PCA describes; it proves nothing.
💭 Think: Why is naming actual Pokémon more convincing than just quoting the loadings?
Homework — add the body, name the axes yourself
This is what you turn in: scripts/28_pca_2_homework.R
So far PCA has only seen the six battle stats. The file also has two real physical measurements: height_m and weight_kg. The homework puts all eight into the PCA — and this time no answer is printed anywhere. You read the loadings, you name the axes, you check them against the extremes, and you write it up.
- The questions are at the top, with answer boxes. Answer in
#comments. - Helper code is written for you where something is new — the main one is the
select()with eight columns. - Where it says
YOUR CODE, copy the matching section from today’sscripts/28_pca_2.R. Same code, new PCA.
One Pokémon lands so far out on PC1 that it squashes the whole plot. Hover to find out who — then look at its height and weight.
Session > Restart R, then run the whole file top to bottom. It must run clean, and every plot must draw.