Creating a visual representation of Parliament data in R
R is super useful software for visualizations. It has powerful tools like the ggplot2
, plotly
, and leaflet
for graphics. There are even more specific libraries that are specially made for certain visual purposes. One such library is ggparliament
, which can be used to show the distribution of party seats in a parliament. ggparliment
is not actually a graphical library, but it helps out to produce wonderful graphical output.
With the help of ggparliament
package, I will show you how to make the semi-circle parliament graphs that we often see. While I will be using the standard ggplot2 for graphics, I will push one step further with BBC-style graphics for my final output. Here, I do a step-by-step run-through of developing such a graph in R which you can replicate for your work later.
I will be using the data from Nepal’s Federal Election of 2022. The output of my visualization will look like this-
Let me get started with making this graph in R. To find the data set, click here. Download the 2079 NP Election.xlxs
.Make a folder and save the R workspace there. Also, set the working directory on that path so that accessing and saving files will be organized.
First, we will have to import a series of libraries that are required for this work. If you have not downloaded these libraries, you have to download and install them before calling the library
functions. Please do it.
#Load libraries
library(readxl)
library(ggparliament)
library(tidyverse)
library(ggplot2)
library(bbplot)
#bbplot is not in CRAN. Use devtools::install_github('bbc/bbplot') to install
# it in the console
With all libraries loaded in the system, I will import the Excel file into my R console. I named the data frame asNP_Election_2079
.
NP_Election_2079<- read_excel("2079 NP Election.xlsx")
The file has four variables and 13 observations. The first variable is the name of the party, followed by the seats it won on FPTP and PR (the two types of election methods in Nepal). The last variable is the total number of seats that the party won combining both results.
I will assign a color to each party based on how their color scheme is(as far as I know). This will help me generate a customized output later.
NP_Election_2079 %>%
mutate(party_color = case_when(
Party == "Nepali Congress" ~ "#66f04a",
Party == "CPN-UML" ~ "#fa7619",
Party == "Maoist" ~ "#910101",
Party == "CPN(US)" ~ "#d98a52",
Party == "Rastriya Prajantantra Party" ~ "#ebe710",
Party == "Rastriya Swatantra Party" ~ "#59320b",
Party == "Loktantrik Smajbadi Party" ~ "#11d19b",
Party == "Nagarik Unmukti Party" ~ "#3b2208",
Party == "Nepal Workers and Peasants Party" ~ "#c71c44",
Party == "Janamat Party" ~ "#4a2d34",
Party == "Rastriya Janamorcha" ~ "#e80004",
Party == "Independent" ~ "#a5d9fa",
Party == "Janata Samajbadi Party" ~ "9053e6")) -> NP_Election_2079
These color codes will be used in the final step. Remember the variable is named party_color
, which is also the new variable that is added to the data frame NP_Election_2079.
Now, with the help of parliament_data
a function from the ggparliament
package, I will make a new data frame NP_Election_2079_Graph
.
NP_Election_2079_Graph <- parliament_data(election_data = NP_Election_2079,
type = "semicircle",
parl_rows = 8,
party_seats = NP_Election_2079$Total)
The function will generate a coordinate in the data frame that will be used to make a semi-circle graph. Note that I input the type= “semi-circle” to generate a graph like the above, but I can also assign other types here. Also, if you are trying out this graph with any other dataset, please play around with the number in parl_rows as the number may not work properly for visualization from other datasets.
The new dataset will look like this. The four new variables are added by the function. The details are rather difficult to understand, so let it be for now.
Now I will create a ggplot graph with the NP_Election_2079_Graph.
This ggplot will be required further for the final output in the BBC-style plot. For now, I will save the graph as NP_Election_2079_GraphOutput
. The graph will look like the figure below.
NP_Election_2079_Graph %>%
ggplot(aes(x = x,
y = y,
colour = Party)) +
geom_parliament_seats(size=7) -> NP_Election_2079_GraphOutput
While the ggplot output gives me the visualization of semi-circle output, the output is still not very attractive. For once, I do not like the colors that represent a party, and as of now, it is not according to the colors that I had assigned to each party previously. I can always play around with ggplot2
to make this more informative and customized, but for now, as I mentioned at the beginning, I will focus on making it in the BBC-Style.
Finally at the last step, I will make use of bbplot:: bbc_style()
from thebbplot
library. The function will build upon the ggplot graph, with similar syntax.
NP_Election_2079_GraphOutput + bbplot::bbc_style() +
ggtitle("2nd Nepal Parliament") +
theme(text = element_text(size = 30),
legend.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank()) +
scale_colour_manual(values = NP_Election_2079$party_color,
limits = NP_Election_2079$Party)
If you look at the code, it is the same as any ggplot code. The bbplot::bbc_style()
helps in the aesthetics of the output. Note the use of NP_Election_2079$party_color
, the variable I made to assign each party with the color. If you don’t like the color, you can always go up and change it. I have created a new graph NP_Election_2079_GraphOutput
.
Here, calling the NP_Election_2079_GraphOutput
should give me the semi-circle parliament that I developed. The final output looks exactly like the first image posted in this blog.
So with these simple steps, I show you how a semi-circle parliament seats can be shown in R with the help of ggparliament
, ggplot2
, and bbplot
. With the codes, I hope that you can generate such graphics on your own. Note that in the function parliment_data()
[third box], you can add other types like “horseshoe”, “opposing_benches”, “circle”, etc. to generate the visual that best suits your data.