Image Credit: Marco Oriolesi

Indian Parliament

In this post I show how to make a simple animation depicting the changing composition of Indian Parliament between 1998 and 2014, which was the last election year. In India there are several political parties and two major coalitions: National Democratic Alliance (NDA) and United Progressive Alliance (UPA). NDA leans right while UPA leans left. However, each of the coalitions is made up of several parties that have different ideologies. Therefore, not all the parties in each coalition will be permanent members of that coalition.

In order to make this graph, I manually input the data from Wikipedia into a spreadsheet. You can download the spreadsheet from here. The animation is at the bottom of this post

All the R packages are available on CRAN. For making the graph, the following three packages are critical: ggpol for making the graph of the Parliament, ggplot2 for making the graph, and magick for making the animation.

In addition to these packages, I am also using readxl to read Excel spreadsheet and reshape2 to melt the data frame. As I am going to use only one function each from these packages, I am not loading the full packages and instead calling the relevant functions.

library(ggplot2)
library(ggpol)
library(hrbrthemes)
library(dplyr)
library(magick)
library(here) # This is used for locating the Excel file on my Mac. Not required.
library(kableExtra) # Just to make nice looking table. Not required.

I want to shape the data such that for each year and political party we have only 1 row. Take a look at the Excel file to see how data is arranged currently.

# Read the data

bt <- readxl::read_excel(here::here('static','data','Indian-parliament.xlsx')) %>% 
  mutate(Other = Total - NDA - UPA) %>% # Create an Other column
  reshape2::melt(id.vars = c("Parliament", "Years"),
                 variable = "Party",
                 value.name = "Seats") %>% 
  arrange(Years, Party) %>%
  filter(Party != "Total") %>%  # Drop the row for Total
  mutate(Color = ifelse(Party == "NDA", "darkorange1",
                        ifelse(Party == "UPA", "forestgreen", "gray50")
                        ) 
         ) # Create a column for colors.

# Print the data frame
knitr::kable(bt) %>% 
  kableExtra::kable_styling(bootstrap_options = "striped", full_width = F)
Parliament Years Party Seats Color
12 1998 NDA 258 darkorange1
12 1998 UPA 144 forestgreen
12 1998 Other 143 gray50
13 1999 NDA 270 darkorange1
13 1999 UPA 114 forestgreen
13 1999 Other 161 gray50
14 2004 NDA 181 darkorange1
14 2004 UPA 218 forestgreen
14 2004 Other 144 gray50
15 2009 NDA 159 darkorange1
15 2009 UPA 262 forestgreen
15 2009 Other 122 gray50
16 2014 NDA 315 darkorange1
16 2014 UPA 52 forestgreen
16 2014 Other 178 gray50

Now that we have our data frame in the right shape, we are ready to create the animation.

  1. We first initiate a graphic device using image_graph function from magick package.
img <- image_graph(1200, 680, res = 150)
  1. Next, we create a list of data frames by splitting bt by Years. We have only 5 years data so datalist will comprise 5 data frames.
datalist <- split(bt, bt$Years)
  1. Now we will create a plot for each data frame inside datalist using ggplot2 and ggpol packages.
out <- lapply(datalist, function(data){
  p <- ggplot(data) + 
    ggpol::geom_parliament(aes(seats = Seats, fill = Party), color = NA) + 
    scale_fill_manual(values = bt$Color, labels = bt$Party) +
    coord_fixed() + 
    labs(title = paste("Indian Lok Sabha", data$Years),
         subtitle = "Lower house of the Indian Parliament",
         caption = "Source: Wikipedia (https://en.wikipedia.org/wiki/Lok_Sabha)") +
    theme_ipsum() +
    theme(axis.line = element_blank(),
          axis.text = element_blank())
  print(p)
})
dev.off()
  1. Finally, we will create the animation with 5 frames per second. You can increase the number to speed up the transition.
animation <- image_animate(img, fps = 5)
print(animation)

Go back up

comments powered by Disqus