Skip to content

Instantly share code, notes, and snippets.

@tomsing1
Last active February 19, 2025 17:13
Show Gist options
  • Select an option

  • Save tomsing1/cc684a3b8a5f09dcaa904012c7239fc3 to your computer and use it in GitHub Desktop.

Select an option

Save tomsing1/cc684a3b8a5f09dcaa904012c7239fc3 to your computer and use it in GitHub Desktop.

Revisions

  1. tomsing1 revised this gist Feb 19, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    library(ggplot2)
    library(hrbrthemes)
    library(janitor)

    read.csv(text='"Party","Votes in all of the U.K.","Votes only in Scotland"
    "Labour",33.7,35.27801359
  2. tomsing1 created this gist Feb 19, 2025.
    118 changes: 118 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    library(ggplot2)
    library(hrbrthemes)

    read.csv(text='"Party","Votes in all of the U.K.","Votes only in Scotland"
    "Labour",33.7,35.27801359
    "Conservative",23.7,12.72746096
    "Reform UK",14.3,6.956199453
    "Liberal\nDemocrats",12.2,9.699645107
    "Greens",6.7,3.838190168
    "Scottish\nNational\nParty",2.5,30.0130445
    "Others",6.8,1.487446217
    ', header=TRUE) |>
    janitor::clean_names() |>
    transform(
    votes_in_all_of_the_u_k = votes_in_all_of_the_u_k/100,
    votes_only_in_scotland = votes_only_in_scotland/100,
    party = factor(party, party)
    ) -> xdf

    c(
    "Labour" = "#dc2c2b",
    "Conservative" = "#013b83",
    "Reform UK" = "#1888a0",
    "Liberal\nDemocrats" = "#b15501",
    "Greens" = "#337f27",
    "Scottish\nNational\nParty" = "#cc9800",
    "Others" = "#a2a2a2"
    ) -> party_cols

    ggplot() +
    geom_col(
    data = xdf,
    aes(party, votes_only_in_scotland, fill = party, alpha = "Votes only in Scotland"),
    position = position_nudge(-0.125),
    width = 0.5
    ) +
    geom_col(
    data = xdf,
    aes(party, votes_in_all_of_the_u_k, fill = party, alpha = "Votes in all of the U.K."),
    position = position_nudge(0.125),
    width = 0.5
    ) +
    geom_text(
    data = xdf,
    aes(party, votes_in_all_of_the_u_k + 0.015, label = scales::percent(votes_in_all_of_the_u_k, 0.1)),
    position = position_nudge(0.125),
    family = font_gs,
    size = 3,
    color = "black",
    fontface = "bold"
    ) +
    geom_text(
    data = xdf |> subset(party %in% c("Labour", "Scottish\nNational\nParty")),
    aes(party, votes_only_in_scotland + 0.015, label = scales::percent(votes_only_in_scotland, 0.1)),
    position = position_nudge(-0.125),
    family = font_gs,
    size = 3
    ) +
    geom_text(
    data = data.frame(
    x = "Scottish\nNational\nParty",
    y = 0.3,
    label = "\n← 30% of Scots\nvoted for the SNP,\nleading to 2.5% total\nU.K. votes."
    ),
    aes(
    x, y, label = label
    ),
    family = font_gs,
    size = 3,
    hjust = 0,
    vjust = "top",
    lineheight = 0.975,
    position = position_nudge(0.18)
    ) +
    scale_alpha_manual(
    name = NULL,
    values = c(
    "Votes only in Scotland" = 1/2,
    "Votes in all of the U.K." = 1
    )
    ) +
    scale_fill_manual(
    values = party_cols,
    ) +
    scale_y_continuous(
    labels = scales::percent_format(),
    breaks = c(0.1, 0.2, 0.3),
    expand = c(0, 0, 0, 0),
    limit = c(0, 0.4)
    ) +
    guides(
    alpha = guide_legend(
    reverse = TRUE,
    override.aes = list(
    fill = "black"
    ),
    theme = theme(
    legend.direction = "horizontal",
    legend.key.width = unit(14, "pt"),
    legend.key.height = unit(14, "pt")
    )
    ),
    fill = "none"
    ) +
    labs(
    x = NULL, y = NULL,
    title = "U.K. election results 2024"
    ) +
    theme_ipsum_gs(grid="Y") +
    theme(
    axis.line.x.bottom = element_line(color = "black"),
    axis.text.x.bottom = element_text(margin = margin(t = 5)),
    legend.justification = "left",
    legend.position = "top",
    legend.location = "plot",
    legend.margin = margin(l = 0),
    plot.title.position = "plot"
    )