Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Download claimant count data from the Nomis API to R, and plot a chart using ggplot2.
library(ggplot2)
nomis <- read.csv('http://www.nomisweb.co.uk/api/v01/dataset/NM_1_1.data.csv?geography=1941962826...1941962836&date=latestMINUS156,latestMINUS144,latestMINUS132,latestMINUS120,latestMINUS108,latestMINUS96,latestMINUS84,latestMINUS72,latestMINUS60,latestMINUS48,latestMINUS36,latestMINUS24,latestMINUS12,latest&sex=5...7&item=1&measures=20100,20203&select=date_name,geography_name,geography_code,sex_name,item_name,measures_name,obs_value,obs_status_name')
dates <- paste("1", nomis$DATE_NAME)
nomis$date <- as.Date(dates, format <- "%d %B %Y")
cc_rate <- subset(nomis, MEASURES_NAME=='Proportion of resident population aged 16-64 estimates')
# reorder the data
most_recent_cc_rates <- subset(cc_rate, date==max(date) & SEX_NAME=='Total')
order_for_names <- rev(order(most_recent_cc_rates$OBS_VALUE))
ordered_names <- most_recent_cc_rates$GEOGRAPHY_NAME[order_for_names]
cc_rate$GEOGRAPHY_NAME <- factor(cc_rate$GEOGRAPHY_NAME, levels=ordered_names)
png(filename='cc.png', width=640, height=400
ggplot(cc_rate, aes(x=date, y=OBS_VALUE, colour=SEX_NAME, group=SEX_NAME)) +
  facet_wrap(~ GEOGRAPHY_NAME) +
  geom_line() +
  theme_bw() +
ylab('Claimant count rate (ages 16-64)')
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment