DS 4: Graphics with ggplot2

Week 4 · Tuesday, Oct 20 · 9:50-10:55 am · Self-guided

Dr. Marcela Alfaro Córdoba

Goal for this morning

Work through one lesson on the Intro to R site, on your own:

By 10:55 you should have one proof-of-completion PDF downloaded, and a script that re-makes every plot in Positron.

Important

3 points, as always: 2 for the printout, 1 for Thursday’s discussion check.

This afternoon: proposals

1:30 pm, same room. DS 3 check, then every group presents.

  • Your DS 3 printout (Lesson 6) and a pencil: the discussion check is first
  • Slides uploaded to Canvas as one PDF by 1:00 pm
  • Don’t spend this morning on your slides. The DS is due like any other week; the slides should be done already

If you finish the DS early, rehearse your 3 minutes out loud, with a timer, quietly. Not in the room if it disturbs others.

Ground rules

Allowed

  • The R help pages: ?geom_point, ?theme
  • The lesson page itself
  • Thursday’s slides
  • Your own notes and scripts
  • Asking me

Not allowed

  • ChatGPT, Claude, Copilot, Gemini, any chatbot
  • Positron Assistant or inline suggestions
  • Copying a classmate’s code

Talking through an idea with a neighbor is fine. Reading their screen and typing what’s on it is not.

Before every plot: sketch it

Draw the axes on paper before you write the code.

What goes on x, what goes on y, what changes colour, how many marks? Write it as a comment:

# Exercise 3: x = year, y = lifeExp, one line per country,
#   colour = continent, points underneath the lines

Note

Thursday’s discussion check will show you a plot from your printout and ask: “Which line of code made the points that colour? What happens if you move it?”

Work in a script, not just the browser

Open stat266a in Positron and create week04/ds04_notes.R:

# DS 4 · ggplot2 · my notes
source("scripts/load-gapminder.R")
source("scripts/check-gapminder.R")
library(ggplot2)

# Exercise 1 ---------------------------------------------------------
# Sketch:
  • For each exercise, copy your final answer from the browser into this script, under its heading
  • Run it on the full gapminder data, not the lesson’s smaller gm. Does the plot look the way you expected?
  • Write your sketches as comments

Tip

At 10:50, the restart test: Session → Restart R, source week04/ds04_notes.R. Every plot should be re-made, with no errors.

How to work

  1. Read the lesson section above the exercises first
  2. Click Run in the exercise box to see your values (colour, size, shape, year)
  3. Sketch the plot on paper. Write it as a comment
  4. Write your code. Check. If it fails, read the message before changing anything
  5. Copy the working version into week04/ds04_notes.R

If you spend more than 10 minutes on one exercise, raise your hand. Being stuck is useful; being stuck for 40 minutes is not.

Lesson 8: Graphics with ggplot2

What you’re practicing

Exercise The skill
1. GDP over time Changing which column goes to which axis
2. Colour by continent Mapping a column to colour inside aes()
3. Which layer is on top? group, and layer order
4. Fixed colour and size Fixed properties outside aes(); log scale; lm line
5. Shapes and one trend per continent Inherited mappings; fixed shape
6. Boxplots by continent Subsetting first, then plotting; labels and themes

Exercises 3, 5 and 6 are the core of the lesson and the ones we discuss on Thursday. The advanced part of 6 is last.

Tips: Exercises 1 and 2

Exercise 1. You don’t need a new geom. You need to change what goes inside aes(). Which column is “time”? Which axis does time go on?

The plot will look strange: lots of points stacked at each year. That’s expected for a scatterplot of 12 years.

Exercise 2. Start from your p1 code, not from scratch. One addition, inside aes().

Then look at the plot and answer the question in a comment: which continent’s GDP grew fastest? Were you surprised?

Tips: Exercises 3 and 4

Exercise 3. Two things to get right:

  • Without group = country, geom_line() draws one zig-zag through every row. Thursday’s “saw” slide
  • “Lines on top of the points” means the lines are added later

Then swap the two layers and look. Can you see the difference? On which part of the plot?

Exercise 4. Click Run first and write down your colour and size.

Both are the same for every point, so where do they go: inside or outside aes()? If a legend appears with your colour’s name in it, you put it in the wrong place.

Tips: Exercises 5 and 6

Exercise 5. You want one lm line per continent. geom_smooth() splits by whatever grouping it inherits. So where must colour = continent be: in ggplot(), or in geom_point()?

Count the lines when you’re done. How many should there be?

Exercise 6. Two steps, in this order:

  1. gm_year: only the rows of gm from your year. Last week’s subsetting: [condition, ], with the comma. Check nrow(): how many countries should there be?
  2. The boxplot of gm_year: continent on x, GDP per capita on y, and the y label

Advanced: Thursday’s “Themes and tweaks” slide.

Halfway check: 10:25

By now you should be done with Exercises 1 to 3 and working on 4.

Behind? That’s fine. Priorities:

  1. Exercises 3, 5 and 6, the ones we discuss on Thursday
  2. Exercises 1 and 2
  3. Exercise 4, and the advanced part of 6, tonight if needed

Done early? Walk over to someone who’s stuck and ask them to sketch their plot for you, on paper. Don’t tell them the answer.

Finishing up

Get your PDF

At the bottom of the lesson page:

  1. Make sure your name is filled in on the site
  2. Attempt every exercise (an honest wrong attempt counts)
  3. Click Download my proof-of-completion PDF

Warning

Do it now, in the room. The site stores your work in your browser: a cleared cache or a different laptop and it’s gone. Your week04/ds04_notes.R is your backup.

What to submit and bring

Upload to Canvas by Wednesday 11:59 pm

  • The DS PDF

And by 1:00 pm today

  • Your proposal slides, as one PDF

Bring to Thursday’s lecture

  • The DS PDF, printed on paper
  • A pencil

Late DS: up to 3 days, 5% off per 24 hours. The lowest DS grade is dropped. The proposal is a hard deadline.

Before you go: checklist

See you at 1:30 pm for the proposals. Come on time: the DS 3 check starts at 1:30, and the first group presents right after.