Motivations

The Economist là tạp chí hàng đầu với những chart rất ấn tượng. Tuy vậy tạp chí này cũng tạo ra những misleading charts. Shara Leo - một trong những Visual data journalist của tạp chí này list một loạt những chart gây hiểu lầm này (cũng như phương án thay thế) tại đây.

Bằng R/ggplot2 chúng ta có thể tạo barplot với những hiệu chỉnh/sửa đổi như sau:

R codes

Dưới đây là R codes để tạo ra barplot trên:

# Load data: 
library(tidyverse)

read_csv("http://infographics.economist.com/databank/Economist_corbyn.csv") -> data_bar

# Prepare data for ploting: 

data_bar %>% 
  mutate(n_likes = `Average number of likes per Facebook post 2016` / 1000) %>% 
  filter(!is.na(n_likes)) %>% 
  mutate(fontface = case_when(Page == "Jeremy Corbyn" ~ "bold", 
                              Page == "Owen Smith" ~ "plain", 
                              Page == "Andy Burnham" ~ "plain", 
                              TRUE ~ "italic")) %>% 
  mutate(color_y_text = case_when(Page == "Jeremy Corbyn" ~ "grey10", 
                                  TRUE ~ "grey30")) %>% 
  arrange(n_likes) %>% 
  mutate(Page = factor(Page, Page)) -> data_bar

#------------------------------
#           Barplot
#------------------------------

library(showtext) # Package for using extra fonts. 

my_font <- "Lato" 

# Load font for ploting: 

font_add_google(name = my_font, family = my_font) 

font_y <- "Roboto Condensed" 

font_add_google(name = font_y, family = font_y)

showtext_auto() # Automatically render text. 


bar_color <- "#116ea1"

bgr_color <- "#d9e9f0"

red_icon <- "#ed1b24"

jeremy_color <- "#c1dae6"

grid_color <- "#b6c5ce"

p_title <- "Left-click"

p_subtitle <- "Average number of likes per Facebook post\n2016, '000"

p_caption <- "Source: Facebook"


data_bar %>% 
  ggplot(aes(y = Page, x = n_likes)) + 
  geom_col(fill = bar_color, width = 0.6) + 
  geom_col(data = data_bar %>% filter(Page == "Jeremy Corbyn"), fill = bar_color, width = 0.6) + 
  # geom_rect(aes(ymin = 5.6, ymax = 6.4, xmin = 0, xmax = 6), fill = jeremy_color) + 
  labs(title = p_title, subtitle = p_subtitle, caption = p_caption) + 
  theme(plot.background = element_rect(fill = bgr_color, color = NA)) + 
  theme(panel.background = element_rect(fill = bgr_color, color = NA)) + 
  theme(panel.grid.major.y = element_blank()) + 
  theme(panel.grid.minor.x = element_blank()) + 
  theme(axis.title = element_blank()) + 
  theme(axis.ticks = element_blank()) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  scale_x_continuous(limits = c(0, 6), breaks = seq(0, 6, 1), expand = c(0, 0), position = "top") +  
  theme(plot.title.position = "plot") + 
  theme(plot.caption.position = "plot") + 
  theme(plot.title = element_text(family = my_font, size = 18, face = "bold", hjust = 0, margin = margin(b = 0.2, unit = "cm"))) +  
  theme(plot.subtitle = element_text(family = my_font, size = 14, color = "grey10", hjust = 0)) + 
  theme(plot.caption = element_text(family = my_font, color = "grey40", size = 12, hjust = 0, vjust = -3)) + 
  theme(axis.line.y = element_line(size = 0.8, color = "grey40")) + 
  theme(axis.text.x = element_text(size = 14, color = "grey25", family = my_font)) + 
  theme(axis.text.y = element_text(size = 14, color = data_bar$color_y_text, family = font_y, face = data_bar$fontface, hjust = 0)) + 
  theme(panel.grid.major.x = element_line(color = grid_color, size = 0.8)) 

# Make The Economist icon: 

library(grid)

grid.rect(x = 0.045, y = 1, width = 0.05*1.5, height = 0.028, just = c("left", "top"), gp = gpar(fill = red_icon, col = red_icon))

# Ref: https://evamaerey.github.io/flipbooks/ggtext/ggtext#32
# https://github.com/wilkelab/ggtext
# https://wilkelab.org/ggtext/articles/introduction.html
# https://www.r-bloggers.com/2022/02/4-ways-to-use-colors-in-ggplot-more-efficiently/
# https://typethepipe.com/post/analyzing-data-covid19-r-package/
# https://meghan.rbind.io/blog/color-legend/
# https://thomasadventure.blog/posts/mdthemes-is-on-cran-markdown-powered-themes-for-ggplot2/
# https://boingboing.net/2019/03/28/the-economists-visual-data-j.html
# https://medium.economist.com/the-challenges-of-charting-regional-inequality-a9376718348
# https://medium.economist.com/mistakes-weve-drawn-a-few-8cdd8a42d368