Code
library(tidyverse) # ggplot, lubridate, dplyr, stringr, readr...
library(praise)library(tidyverse) # ggplot, lubridate, dplyr, stringr, readr...
library(praise)This week we are exploring agriculture production statistics in New Zealand using data compiled from from StatsNZ.
Sheep have long outnumbered people in New Zealand, but the ratio of sheep to people peaked in the 1980s and has been in steady decline
The gap between people and sheep in New Zealand is rapidly closing. There’s now about 4.5 sheep to every person in New Zealand compared to a peak of 22 sheep per person in the 1980s, that’s according to figures released by Stats NZ this week.
Thank you to Jen Richmond for curating this week’s dataset.
dataset <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2026/2026-02-17/dataset.csv') |>
mutate(measure = case_when(
measure == "Total goats" ~ "Total Goats",
measure == "Total poultry" ~ "Total Poultry",
measure == "Total Dairy Cattle (including Bobby Calves)" ~ "Total Dairy Cattle",
measure == "Total Lambs Marked and/or Tailed" ~ "Total Lambs",
TRUE ~ measure
))dataset |>
group_by(measure) |>
summarize(count_row = n()) |>
arrange(desc(count_row))# A tibble: 193 × 2
measure count_row
<chr> <int>
1 Total Lambs 86
2 Total Sheep 86
3 Barley (yield) 85
4 Total Area of Farms 85
5 Wheat (yield) 85
6 Maize (yield) 84
7 Oats (yield) 84
8 Breeding Ewes 2 Tooth and Over put to Ram 83
9 Number of Farm Holdings 81
10 Rising 1 Year Old Dairy Heifers and Heifer Calves 73
# ℹ 183 more rows
animals <- dataset |>
filter(measure %in% c("Total Sheep", "Total Beef Cattle",
#"Total Cattle",
"Total Dairy Cattle",
"Total Deer", "Total Goats",
"Total Lambs",
"Total Pigs", "Total Poultry"#,
#"Total Ram and Wether Hoggets"
))library(rvest)
pop_nz <- read_html("https://commons.wikimedia.org/wiki/Data:New_Zealand_Total_Population.tab") |>
html_elements("table") |>
html_table(header = TRUE) |>
as.data.frame() |>
slice(-(1:2)) |>
mutate(across(everything(), as.numeric)) |>
mutate(pop = y1*1000000)
animals_pop <- left_join(animals, pop_nz, by = c("year_ended_june" = "x"))cb10 <- c(
#"#000000", # black
"#E69F00", # orange
"#56B4E9", # sky blue
"#009E73", # bluish green
"#F0E442", # yellow
"#0072B2", # blue
"#D55E00", # vermillion
"#CC79A7", # reddish purple
"#999999", # gray
"#44AA99" # teal
)
library(scales)
scale <- 10
animals_pop |>
ggplot(aes(x = year_ended_june, y = value, color = measure)) +
geom_point() +
geom_line() +
scale_color_brewer(palette = "Dark2") +
#scale_color_manual(values = cb10) +
geom_line(aes(y = pop*scale), size = 1.5, color = "black") +
scale_y_continuous(sec.axis = sec_axis(~./scale, name = "NZ population")) +
labs(x = "",
y = "number of animals",
color = "",
title = "Animal in New Zealand over time",
subtitle = "with black population line superimposed")praise()[1] "You are hunky-dory!"