Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Last active September 7, 2020 13:24
Show Gist options
  • Save dmarcelinobr/039a5c9ba3acead2d953c05990ce6f33 to your computer and use it in GitHub Desktop.
Save dmarcelinobr/039a5c9ba3acead2d953c05990ce6f33 to your computer and use it in GitHub Desktop.
Creating The Economist plot style with the Governism Index from JOTA
date legislator_party media_mes cma tma bancada soma
2020-08-28 AVANTE 0.748039215686275 0.857352941176471 0.761019607843137 6 1916
2020-08-28 CIDADANIA 0.717086834733894 0.775210084033614 0.697221988795518 8 2574
2020-08-28 DEM 0.839740514790993 0.919870257395496 0.862903342104668 28 8380
2020-08-28 MDB 0.879152406248459 0.939576203124229 0.884653145577298 35 10992
2020-08-28 NOVO 0.705882352941177 0.852941176470588 0.782941176470588 8 3086
2020-08-28 PATRI 0.958823529411765 0.979411764705882 0.956078431372549 6 1898
2020-08-28 PCdoB 0.3203125 0.16015625 0.34015625 8 872
2020-08-28 PDT 0.519899948827285 0.445820393581163 0.464454263280975 28 4494
2020-08-28 PL 0.959809897304683 0.96767208153832 0.940172709158139 41 12792
2020-08-28 PODE 0.802614379084967 0.860797930283224 0.799013538748833 10 3033
2020-08-28 PP 0.97335401526578 0.977942439731656 0.954589697285104 39 12075
2020-08-28 PROS 0.788829471182412 0.874176640353111 0.773423393599864 11 2881
2020-08-28 PSB 0.432787213912975 0.433268398831279 0.46272224674664 31 5238
2020-08-28 PSC 0.949929971988795 0.963060224089636 0.93184593837535 9 2757
2020-08-28 PSD 0.928651732723741 0.934937151628328 0.906268705590408 33 11689
2020-08-28 PSDB 0.731024825882539 0.854702341786371 0.763450470757738 31 9842
2020-08-28 PSL 0.930681996910426 0.965340998455213 0.934281260606257 53 19711
2020-08-28 PSOL 0.0824074074074074 0.0412037037037037 0.169179894179894 10 554
2020-08-28 PT 0.213776505051604 0.11185557278724 0.230825927684361 53 3723
2020-08-28 PTB 0.834173669467787 0.917086834733894 0.887392390289449 11 3825
2020-08-28 PV 0.348039215686275 0.45874183006536 0.401519607843137 4 897
2020-08-28 REDE 0.4375 0.385416666666667 0.41875 1 129
2020-08-28 REPUBLICANOS 0.932232828825462 0.966116414412731 0.928238723338546 34 10559
2020-08-28 SOLIDARIEDADE 0.907548334018922 0.953774167009461 0.864278190763485 14 4356
# Creating The Economist plot style with the Governism Index from JOTA
# 1 What am I gonna do?
## I will try to replicate The Economist plot sytle using only R packages. The data I am using is a measure calculated every week by the JOTA Labs team at https://jota.info, which tells us about the governism attachment among members of the Brazilian Congress.
# 2 Why we need to use R anyway?
## R is just a data visualization tool, but it is free and programming oriented. Therefore, you just have to build the plot style once, and you can recreate it as many as you want. In addition, R has lots of great packages, one of them is named ggplot2, which has many varieties of supporting elements that you can use to create charming visualization.
# 3 Preparation
## Let's load the required libraries
library(tidyverse)
library(scales)
library(ggrepel)
library(ggthemes)
library(grid)
options(scipen = 100) # to set the scientific notation
# 4 Data preparation
## The data with the governism index and others importante parameters, as the size of the caucus and the number of votes cast in the House, is read right from the gist repository.
library(readr)
data <-
read_csv(
"https://gist.githubusercontent.com/danielmarcelino/bebe807acaeb8facf2ae2bfcf284e381/raw/dcd13ad53fcff2893fcdad5db47922ee85c89e2f/governismo_partidos.csv"
)
glimpse(data)
# 5 Making the Scatter Plot
## First, I plot the data as scatter plot, with the size of the dot indicating the caucus size.
g <- data %>%
ggplot() +
geom_point(
aes(votes, governismo, size = caucus),
color = "#2fc1d3",
alpha = .5,
show.legend = F
)
g
## Next, I will smooth the data using the log10 scale
g <- g +
scale_x_continuous(
trans = "log10",
expand = c(0.05, 0),
labels = number_format(big.mark = ","),
limits = c(100, 20000)
)
g
## Now, I rescale the size of the dot, so it will look nicely
g <- g +
scale_size_continuous(range = c(2, 8), guide = F)
g
## I also have to scale the y axis
g <- g +
scale_y_continuous(
breaks = seq(0, 1, .2),
limits = c(0, 1),
position = "right",
expand = c(0, 0),
labels = scales::percent
)
g
# 6 Plot Theme
## It's time to clean the background and the grid with this small hack
g <- g +
theme_economist() +
theme(
panel.grid.major.y = element_line(colour = "gray80"),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.background = element_blank(),
plot.background = element_blank()
)
g
## Now adjust the tick mark and the position of the y-axis text
g <- g +
theme(axis.ticks.length.x = unit(2, "mm"),
axis.text.y = element_text(vjust = 0))
g
# 7 Add title and annotations
## I will remove the y-axis title and rename the x-axis title. Also I add the title and caption information
g <- g +
theme(
axis.title.y = element_blank(),
axis.title.x = element_text(colour = "black", size = 12),
plot.title = element_text(size = 18, face = "bold", color = "black"),
plot.subtitle = element_text(size = 14, color = "black"),
plot.caption = element_text(
size = 10,
color = "gray30",
hjust = 0
)
) +
labs(
title = "Like a concert without a crowd",
subtitle = "Governism and roll call votes, August/2020",
caption = "Source: http://jota.info",
x = "Number of votes cast in roll calls, log scale"
)
g
# Next I can add the annotation of governism level and caucus size
g <- g +
geom_hline(
aes(yintercept = .74),
color = "#edb0ad",
linetype = "dashed",
size = .85
) +
geom_text(
aes(100, .765, label = "Average governism level"),
color = "#e07b78",
hjust = "left",
size = 4
) +
geom_text(
aes(100, .225, label = "Circle size = Caucus size, 2020"),
color = "#8e9093",
hjust = "left",
size = 4
)
g
# 8 Highligt selected parties
## I'll highlight and add annotation for selected parties. Since the annotation line is in elbow shape, we need to be creative. Perhaps someone knows a package that can directly make an elbow annotation
### First, I add the smallest political party REDE
data_rede <- data %>% filter(legislator_party == "REDE")
g <- g +
geom_text_repel(
aes(votes, governismo, label = legislator_party),
data = data_rede,
nudge_x = -.15,
direction = "x"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_rede,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
### Next, I add some of the major parties PSL and DEM
data_dem <-
data %>% filter(legislator_party %in% c("DEM", "PSL"))
g <- g +
geom_segment(aes(
x = votes,
xend = votes,
y = governismo,
yend = governismo - .21
),
data = data_dem) +
geom_text_repel(
aes(votes, governismo - .21, label = legislator_party),
data = data_dem,
nudge_x = -.15,
direction = "x"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_dem,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
### Next, we add PSDB
data_psdb <- data %>% filter(legislator_party == "PSDB")
g <- g +
geom_text_repel(
aes(votes, governismo, label = legislator_party),
data = data_psdb,
nudge_y = -.25,
direction = "y"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_psdb,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
### Next, we add some of the opposition parties
data_pt <-
data %>% filter(legislator_party == "PT")
g <- g +
geom_segment(aes(
x = votes,
xend = votes,
y = governismo,
yend = governismo + .1
),
data = data_pt) +
geom_text_repel(
aes(votes, governismo + .1, label = legislator_party),
data = data_pt,
nudge_x = -.25,
direction = "x"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_pt,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
data_psol <- data %>% filter(legislator_party == "PSOL")
g <- g +
geom_text_repel(
aes(votes, governismo, label = legislator_party),
data = data_psol,
nudge_x = .25,
direction = "x"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_psol,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
### We can add a small but very loyal party
data_patri <- data %>% filter(legislator_party == "PATRI")
g <- g +
geom_text_repel(
aes(votes, governismo, label = legislator_party),
data = data_patri,
nudge_y = -.12,
direction = "y"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_patri,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
## Finally, we add PP, the party of the government new floor leader
data_pp <-
data %>% filter(legislator_party == "PP")
g <- g +
geom_segment(aes(
x = votes,
xend = votes,
y = governismo,
yend = governismo - .075
),
data = data_pp) +
geom_text_repel(
aes(votes, governismo - .075, label = legislator_party),
data = data_pp,
nudge_x = -.3,
direction = "x",
fontface = "bold"
) +
geom_point(
aes(votes, governismo, size = caucus),
data = data_pp,
shape = 21,
fill = "#2fc1d3",
color = "black"
)
g
# 9 Final Result: the cherry on the top
## To put the unique red header and red rectangle on the top left side as of The Economist plot, I will use the `grid.rect` twice.
png(
"the_economist_style_plot.png",
width = 9,
height = 6,
units = "in",
res = 300
)
print(g)
grid.rect(
x = 1,
y = .995,
hjust = 1,
vjust = 0,
gp = gpar(fill = '#e5001c', lwd = 0)
)
grid.rect(
x = 0.05,
y = .98,
hjust = 1,
vjust = 0,
gp = gpar(fill = '#e5001c', lwd = 0)
)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment