Bump chart of a parliamentary constituency

[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.

The UK has just held a Westminster election to decide the next government. There are some amazing visualisations of what’s happened to the whole nation, but I think there’s a story to tell about my local constituency.

I live in Midlothian, Scotland. It’s a former mining area and this was once the main employment. Miners were a heavily unionised workforce and I think knowing this helps understand the path of voting patterns.

I’ve been reading John Harris recently, and I think his approach to listen to differing views helps understand what is going on around us. With this in mind, I’ve annotated a plot of election results for Midlothian since 1955. I made the plot in R (code below), and annotated in LibreOffice Draw. The dominance of non-voters since 2001 really highlights how important it is to exercise our democratic right!


Here’s the R code I used for the bump chart:


votes %>%
   mutate(party = fct_expand(party, "Cons & Unionist"),
          party = fct_recode(party, `Cons & Unionist` = "Conservative"),
          party = fct_recode(party, `Cons & Unionist` = "Unionist"),
          party = fct_recode(party, Green = "Scottish Green"),
          party = fct_lump(f = party, n = 5),
          party = fct_relevel(party, "Didn't vote")) %>%
   group_by(id, date, party) %>%
   summarise(votes = sum(votes)) %>%
   left_join(x %>%
                select(id, pop)) %>%
   mutate(prop = votes / pop) %>%
   ggplot(aes(x = as.character(date), y = prop, alluvium = party)) +
   geom_alluvium(aes(fill = party, colour = party),
                 alpha = .75, decreasing = FALSE) +
   scale_x_discrete(labels = str_sub(date, 1, 4)) +
   scale_y_continuous(labels = scales::percent) +
   scale_colour_manual(values = pal) +
   scale_fill_manual(values = pal) +
   labs(x = "Election",
        y = "Votes",
        fill = "Party",
        colour = "Party") +
   theme_minimal() +
   theme(text = element_text(size = 20))

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)