An ode to Statistics Scotland

[This article was first published on R – scottishsnow, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I was talking to my mum this weekend about council home provision. When I looked up how many my local authority provides, I discovered Statistics Scotland publish data on all 32 local authorities in Scotland (some seem more lax than others at sending returns). It’s amazing that all this information is so easily available for the public to use. Chapeau to open public data and those who provide it!

I wrote a little bit of code to grab the council home data, along with population, and make a plot of provision in Scotland.

library(tidyverse)
library(ggrepel)

housing = read_csv("https://statistics.gov.scot/slice/observations.csv?&dataset=http%3A%2F%2Fstatistics.gov.scot%2Fdata%2Flocal-authority-housing-stock-by-type&http%3A%2F%2Fpurl.org%2Flinked-data%2Fcube%23measureType=http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fmeasure-properties%2Fcount&http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fdimension%2FtypeOfDwelling=http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fconcept%2Ftype-of-dwelling%2Fall", skip = 7) %>% 
  rename(id = `http://purl.org/linked-data/sdmx/2009/dimension#refArea`) %>% 
  pivot_longer(!c(`Reference Area`, id), names_to = "year", values_to = "homes")

population = read_csv("https://statistics.gov.scot/slice/observations.csv?&dataset=http%3A%2F%2Fstatistics.gov.scot%2Fdata%2Fpopulation-estimates-2011-datazone-linked-dataset&http%3A%2F%2Fpurl.org%2Flinked-data%2Fcube%23measureType=http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fmeasure-properties%2Fcount&http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fdimension%2Fage=http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fconcept%2Fage%2Fall&http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fdimension%2Fsex=http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fconcept%2Fsex%2Fall", skip = 8) %>% 
  rename(id = `http://purl.org/linked-data/sdmx/2009/dimension#refArea`) %>% 
  pivot_longer(!c(`Reference Area`, id), names_to = "year", values_to = "population")

df = housing %>% 
  left_join(population) %>% 
  select(-id, area = `Reference Area`) %>% 
  drop_na() %>% 
  mutate(year = as.numeric(year),
         homes_per_100cap = 100 * homes / population)

# split by regions
df_reg = tibble(area = c("Aberdeen City", "Aberdeenshire", "Angus", "Argyll and Bute", "City of Edinburgh", "Clackmannanshire", "Dumfries and Galloway", "Dundee City", "East Ayrshire", "East Dunbartonshire", "East Lothian", "East Renfrewshire", "Falkirk", "Fife", "Glasgow City", "Highland", "Inverclyde", "Midlothian", "Moray", "Na h-Eileanan Siar", "North Ayrshire", "North Lanarkshire", "Orkney Islands", "Perth and Kinross", "Renfrewshire", "Scottish Borders", "Shetland Islands", "South Ayrshire", "South Lanarkshire", "Stirling", "West Dunbartonshire", "West Lothian"),
       region = c("North East", "North East", "North East", "Highlands and Islands","Lothians", "Strathclyde", "Southern", "North East", "Southern", "Strathclyde", "Lothians", "Strathclyde", "Lothians", "North East", "Strathclyde", "Highlands and Islands", "Strathclyde", "Lothians", "North East", "Highlands and Islands", "Highlands and Islands", "Strathclyde", "Highlands and Islands", "North East", "Strathclyde", "Southern", "Highlands and Islands", "Southern", "Southern", "Highlands and Islands", "Strathclyde", "Lothians")) %>% 
  mutate(region = factor(region, levels = c("Highlands and Islands", "North East", "Strathclyde", "Lothians", "Southern")))

df %>% 
  inner_join(df_reg) %>% 
  group_by(area) %>% 
  mutate(label = if_else(year == max(year), as.character(area), NA_character_)) %>%
  ungroup() %>% 
  ggplot(aes(year, homes_per_100cap,
             group = area, colour = area)) +
  geom_line(linetype = 2) +
  geom_label_repel(aes(label = label),
                   nudge_x = 2,
                   na.rm = TRUE) +
  facet_wrap(~region, ncol = 2) +
  labs(title = "How many council homes do local authorities in Scotland provide?",
       subtitle = "Created by @MikeRSpencer with data from statistics.gov.scot",
       x = "Year",
       y = "Homes per 100 people") +
  theme_bw() +
  theme(legend.position = "none",
        text = element_text(size = 15),
        plot.subtitle=element_text(size=10),
        panel.grid.minor = element_blank())

5 small multiple plots showing the temporal change of council home provision in Scotland, by region.
To leave a comment for the author, please follow the link and comment on their blog: R – scottishsnow.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)