Week 2: Dodgy plots

Find the cheat, then fix it

Introduction

Each box holds the code for a dodgy plot from the slides. The data are real; the figure is not honest.

Run it, find the cheat, and ask: what simple fix will present the data honestly? Make that fix, run it again, then move on to the next plot.

TipStuck?

Ask on the class board, in Code Q&A. Someone else is likely stuck on the same thing, so asking in public helps them too. Demonstrators check it most weekdays, but they give classmates a chance to answer first.

The board is private to this class, so sign in to GitHub first. If the link shows “404 – page not found”, you’re not signed in, or you haven’t accepted the Classroom 50 invitation yet.

NoteComing back to this page?

Your code is kept in this browser. Use Import code to load a file saved using Export all code (bottom of the page).

Dodgy plot #5

NoteHint 1

Where does the y axis start? The eye measures a bar from its base.

TipSolution

Adjust ylim so the bars start at zero:

  coord_cartesian(ylim = c(0, 330)) +

Now each bar’s length is proportional to the generation it stands for. The fall from 2019 to 2024 is 13 %: real, but not a collapse. Once the figure is honest, the title no longer fits it.

Dodgy plot #6

NoteHint 1

How many points are on this plot? Which years does elec cover?

TipSolution

Show all the data:

years_to_show <- min(elec$year):max(elec$year)

Bioenergy grew from 4 to 41 TWh between 2000 and 2025. The 2021–2023 dip is real, but it sits inside a nearly tenfold climb. Cherry-picking needs no fake data, only a well-chosen window.

Dodgy plot #7

This one is trickier: an extension for those who finish early.

NoteHint 1

How many slices contain wind? Add up the slices and compare the sum with y2024$total_twh.

TipSolution

“Low carbon” repeats wind, nuclear, solar and bioenergy, so the pie adds up to 157 % of what was generated. Delete that row (and its twh value):

shares <- data.frame(
  source = c("Gas", "Wind", "Nuclear", "Solar", "Bioenergy", "Coal"),
  twh = c(y2024$gas_twh, y2024$wind_twh, y2024$nuclear_twh,
          y2024$solar_twh, y2024$bioenergy_twh, y2024$coal_twh)
)

Now try comparing gas with wind. For bars, which are much easier to compare, delete the coord_polar() line and change x = "" to x = source.

Save your work

Export all code Import code

Export all code downloads every box on this page as one R file. Each box sits under a line like # Exercise 1: … [w2_1] ----; keep those lines if you edit the file, and Import code can put it back into the boxes on any computer. Your code is also kept in this browser as you type, so reloading the page doesn’t lose it.

Put the exported week2-dodgy-plots.R in your repo, then commit and push via GitHub Desktop.