You've successfully subscribed to MyPad Blog
Great! Next, complete checkout for full access to MyPad Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Creating a Parliament Chart in R

Creating a Parliament Chart in R

The amazing Flerlage Twins put out a cool post on creating a parliament chart in Tableau today.

Motivated by their post, I wanted to attempt to create the chart using concepts similar to the charts in making waffle (square pie) charts in R.

My intial idea was to display the points on a semi-circle and iterate to get outputs similar to those demonstrated by The Flerlage Twins.

However, I casually searched for already available packages, and to my astonishment, there is [ggparliament](https://cran.r-project.org/web/packages/ggparliament/vignettes/basic-parliament-plots_1.html) already available!

Plotting parliament charts is very simple using ggparliament.

Load libraries and data

require(data.table)
require(tidyverse)
require(ggparliament)

# credit: https://cran.r-project.org/web/packages/ggparliament/vignettes/basic-parliament-plots_1.html

us_rep <- election_data %>%
    filter(country == "USA" &
               year == 2016 &
               house == "Representatives")
head(us_rep)

Semicircle Parliament

# credit: https://cran.r-project.org/web/packages/ggparliament/vignettes/basic-parliament-plots_1.html

us_rep <- election_data %>%
    filter(country == "USA" &
               year == 2016 &
               house == "Representatives")
head(us_rep)

# Semicircle Parliament

us_house_semicircle <- parliament_data(election_data = us_rep,
                                       type = "semicircle",
                                       parl_rows = 10,
                                       party_seats = us_rep$seats)
head(us_house_semicircle)

us <- ggplot(us_house_semicircle, aes(x = x, y = y, colour = party_short)) +
geom_parliament_seats() + 
theme_ggparliament() +
labs(colour = NULL, 
     title = "United States Congress") +
scale_colour_manual(values = us_house_semicircle$colour, 
                    limits = us_house_semicircle$party_short) 

us

Horsehoe Parliament

australia <- election_data %>%
    filter(country == "Australia" &
               house == "Representatives" &
               year == 2016) 

australia_horseshoe <- parliament_data(election_data = australia,
                                       party_seats = australia$seats,
                                       parl_rows = 4,
                                       type = "horseshoe")
au <- ggplot(australia_horseshoe, aes(x, y, colour = party_short)) +
    geom_parliament_seats() + 
    theme_ggparliament() +
    labs(colour = NULL, 
         title = "Australian Parliament") +
    scale_colour_manual(values = australia$colour, 
                        limits = australia$party_short) + 
    theme(legend.position = 'bottom')

au

Many thanks to The Flerlage Twins for educating me on the topic of parliament charts. Its actually the first time that I understood the representation!