Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
With the recent resignation announcement from United Kingdom (UK) Prime Minister Keir Starmer, there have been a flurry of people talking about how many UK prime ministers there have been in the past decade, short terms for prime ministers, and so on. I wanted a historical perspective and so grabbed the data from Wikipedia. Wikipedia has a convenient single table list of all of the UK prime ministers since the term began being used informally by Robert Walpole. Walpole was effectively prime minister of the Kingdom of Great Britain from 1721 onwards.
The first prime minister of the United Kingdom of Great Britain and Ireland was William Pitt in 1801; and of United Kingdom of Great Britain and Northern Ireland was Andrew Bonar Law in 1922. But these distinctions will be largely disregarded for the purpose of this blog post.
Downloading prime ministerial data from Wikipedia
Here’s code to download and import that list from Wikipedia. This worked as at 23 June 2026, but Wikipedia pages are known to change in format so it’s brittle about whether it will work forever:
library(rvest)
library(tidyverse)
library(janitor)
library(slider) # for rolling sum
library(scales)
library(ggrepel)
library(kableExtra)
#-----------------Import and process data------------------------
url <- "https://en.wikipedia.org/wiki/List_of_prime_ministers_of_the_United_Kingdom"
page <- read_html(url)
# The main PM table is the first wikitable on the page
pm_table <- page |>
html_element("table.wikitable") |>
html_table(fill = TRUE) |>
clean_names() |>
select(
pm = prime_minister_office_lifespan,
start = term_of_office,
end = term_of_office_2
) |>
# drop second line of column titles:
slice(-1) |>
# find the PMs' names - everything up to the first [
mutate(pm = str_extract(pm , ".*?\\["),
pm = str_replace(pm, "\\[", ""),
) |>
# strip all the footnotes and stuff from the dates:
mutate(across(everything(), ~ str_remove_all(.x, "\\[.*?\\]"))) |> # remove [1] refs
mutate(across(everything(), str_squish)) |>
mutate(start = as.Date(start, format = "%d %B %Y"),
end = as.Date(end, format = "%d %B %Y"),
end = if_else(is.na(end) & pm == "Keir Starmer",
as.Date("2026-07-10"),
end),
duration = as.numeric(end - start),) |>
distinct() |>
mutate(pm = fct_reorder(pm, start, .desc = TRUE)) |>
group_by(pm) |>
mutate(last_end = max(end)) |>
ungroup()Most and longest serving prime ministers
This lets us do some simple analysis. First, here are the UK prime ministers who have served the most often—that is, had more than one term:
| Prime minister | Terms | Earliest start | Latest finish | Total duration |
|---|---|---|---|---|
| William Ewart Gladstone | 4 | 1868-12-03 | 1894-03-02 | 4508 |
| Edward Smith-Stanley | 3 | 1852-02-23 | 1868-02-25 | 1381 |
| Robert Gascoyne-Cecil | 3 | 1885-06-23 | 1902-07-11 | 5000 |
| Stanley Baldwin | 3 | 1923-05-22 | 1937-05-28 | 2639 |
| Thomas Pelham-Holles | 2 | 1754-03-16 | 1762-05-26 | 2763 |
| Charles Watson-Wentworth | 2 | 1765-07-13 | 1782-07-01 | 478 |
| William Cavendish-Bentinck | 2 | 1783-04-02 | 1809-10-04 | 1178 |
| William Pitt the Younger | 2 | 1783-12-19 | 1806-01-23 | 6917 |
| Arthur Wellesley | 2 | 1828-01-22 | 1834-12-09 | 1051 |
| William Lamb | 2 | 1834-07-16 | 1841-08-30 | 2447 |
| Robert Peel | 2 | 1834-12-10 | 1846-06-29 | 1883 |
| Henry John Temple | 2 | 1855-02-06 | 1865-10-18 | 3429 |
| Benjamin Disraeli | 2 | 1868-02-27 | 1880-04-21 | 2530 |
| Ramsay MacDonald | 2 | 1924-01-22 | 1935-06-07 | 2480 |
| Winston Churchill | 2 | 1940-05-10 | 1955-04-05 | 3160 |
| Harold Wilson | 2 | 1964-10-16 | 1976-04-05 | 2835 |
Since the mid twentieth century, only Churchill and Wilson have had a second chance to be prime minister. In the nineteenth century it was much more common, with big names like Gladstone, Disraeli and Gascoyne-Cecil dominating politics while in government and out.
Here are those who have served the longest durations in total:
| Prime minister | Terms | Earliest start | Latest finish | Total duration |
|---|---|---|---|---|
| Robert Walpole | 1 | 1721-04-03 | 1742-02-11 | 7619 |
| William Pitt the Younger | 2 | 1783-12-19 | 1806-01-23 | 6917 |
| Robert Jenkinson | 1 | 1812-06-08 | 1827-04-09 | 5418 |
| Robert Gascoyne-Cecil | 3 | 1885-06-23 | 1902-07-11 | 5000 |
| William Ewart Gladstone | 4 | 1868-12-03 | 1894-03-02 | 4508 |
| Frederick North | 1 | 1770-01-28 | 1782-03-27 | 4441 |
| Margaret Thatcher | 1 | 1979-05-04 | 1990-11-28 | 4226 |
| Henry Pelham | 1 | 1743-08-27 | 1754-03-06 | 3844 |
| Tony Blair | 1 | 1997-05-02 | 2007-06-27 | 3708 |
| Henry John Temple | 2 | 1855-02-06 | 1865-10-18 | 3429 |
The first UK prime minister, Robert Walpole, was also the longest serving. From the 20th and 21st century, only Thatcher and Blair make the top ten list.
Those two simple tables were produced with this code:
#------------summary highlights----------
# prime ministers number of terms and total duration:
pm_summary <- pm_table |>
rename(`Prime minister` = pm) |>
group_by(`Prime minister`) |>
summarise(Terms = length(`Prime minister`),
`Earliest start` = min(start),
`Latest finish` = max(end),
`Total duration` = sum(duration)) |>
arrange(desc(Terms), `Earliest start`)
# Prime ministers with more than one term:
pm_summary |>
filter(Terms > 1) |>
kable() |>
kable_styling()
# Longest serving prime ministers:
pm_summary |>
arrange(desc(`Total duration`)) |>
slice(1:10) |>
kable() |>
kable_styling() Graphic summaries
Tables are nice but graphics are better. Here is my attempt to summarise all the prime ministers of the UK (and of the predecessor Kingdom of Great Britain) in one picture. You probably need a full-sized screen for this, but with the right display I think the Gantt chartish style works nicely.
That chart produced with this code. There are a few clutter-minimisation polishing details here on top of my usual blog style, like suppressing the y axis labels and adding them instead as text close to the data. Much easier to read. And suppressing the horizontal gridlines.
--------------Draw plots-------------
the_title <- "Prime ministers of the United Kingdom and its predecessors, 1721 to 2026"
pm_table |>
ggplot(aes(y = pm, yend = pm)) +
geom_segment(aes(x = start, xend = end),
linewidth = 2, colour = "steelblue") +
geom_text(data = distinct(pm_table, pm, last_end),
aes(label = pm, x = last_end + 500),
size = 2, hjust = 0, colour = "grey50") +
scale_x_date(
breaks = seq(as.Date("1720-01-01"), as.Date("2035-01-01"), by = "20 years"),
date_labels = "%Y",
sec.axis = sec_axis(~.),
) +
labs(x = "Year",
y = "",
title = the_title) +
theme(axis.text.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank())Secondly, it seems highly relevant to produce a plot of the distribution of durations:
And one showing the trend (or lack of trend) in durations over time:
Those two simple plots produced with this code. Perhaps the only point of particular interest here is how I used a subset of the data to highlight the names of prime ministers with term duration of less than 120 days or more than 3,000:
pm_table |>
ggplot(aes(x = duration)) +
geom_density(colour = "steelblue") +
geom_rug(colour = "steelblue") +
scale_x_continuous(label = comma) +
labs(x = "Duration in days",
title = the_title)
pm_table |>
ggplot(aes(x = start, y = duration)) +
geom_smooth(method = "gam", colour = "white") +
geom_point(colour = "steelblue") +
geom_text_repel(data = filter(pm_table, duration < 120 | duration > 3000),
aes(label = pm), size = 2.8, seed = 123) +
scale_y_sqrt(breaks = c(0.5, 1, 1:4 * 2) * 1000, label = comma) +
labs(x = "Starting date of premiership",
y = "Duration in days",
title = the_title,
subtitle = "Durations shown are of individual periods in office, not lifetime totals.")Finally, the big question that seems to get a lot of attention. How many prime ministers per decade? Below is my effort at calculating and presenting this.
We can see that we are indeed going through a decade that is rich in UK prime ministers (and will be richer still in a month or so). But it’s not unprecedented. We’ve been at similar levels seeral times in the past, and in the politically turbulent 1830s there were even more premierships.
In fact, in the late twentieth century with Thatcher and Blair, the UK faced a period of unusually slow turnover of prime ministers. But that was a formative period in the life of many of today’s political commentators, so its not surprising that the current rapid turnover comes across as a surprise.
Code for this is below. Note that I calculated this on a daily basis. I’m not 100% I’ve got it right, but it passes my simplest reality checks (eg manually counting those we’ve had in the past ten years – six so far, although expeted soon to become seven).
cumulative_pms <- pm_table |>
full_join(tibble(start = seq(from = min(pm_table$start),
to = max(pm_table$end),
by = "1 day"))) |>
arrange(start) |>
mutate(starting_pms = if_else(is.na(pm), 0 , 1),
rolling_pms = slide_sum(starting_pms, before = 3653),
# I'm not sure I've got this right yet, but the idea is that in any given day,
# the number of PMs in thepast 10 years is however many started in those 10 years,
# plus 1 PM that you came into the period with. The exception being the time
# of the very first prime minister, for which time you only have the rolling sum
# of PMs that started:
rolling_pms = if_else(start < (pm_table[1, ]$start + 3654),
rolling_pms,
rolling_pms + 1))
# When was the peak number of PMs in the last decade:
arrange(cumulative_pms, desc(rolling_pms))
cumulative_pms |>
ggplot(aes(x = start, y = rolling_pms)) +
geom_line(colour = "steelblue") +
scale_y_continuous(breaks = 0:max(cumulative_pms$rolling_pms)) +
labs(x = "",
y = "Number of prime ministers in past 10 years",
title = the_title,
subtitle = "Peak prime ministers per decade was in the 1830s")That’s all for now.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
