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:

# Clear R environment: 

rm(list = ls())

# Load data: 
library(tidyverse)

read_csv("http://infographics.economist.com/databank/Economist_dogs.csv") -> line_data

# Prepare data for ploting: 

line_data %>% 
  slice(1:10) %>% 
  mutate_all(as.numeric) -> line_data

names(line_data) <- c("year", "weight", "neck_size")

my_labels <- as.character(2006:2015)

case_when(my_labels == "2006" ~ "2006", TRUE ~ str_sub(my_labels, 3, 4)) -> my_labels
  
library(showtext) # Package for using extra fonts. 

my_font <- "Roboto" 

# 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. 

w_color <- "#973d4c"

n_color <- "#2dc0d2"  

bgr_color <- "#d9e9f0"

grid_color <- "#b2c1ca"

icon_color <- "#ed1c24"

a <- 0.4
b <- 0.25

line_data %>% 
  ggplot(aes(year, neck_size)) + 
  geom_hline(yintercept = seq(38, 44, 2), color = grid_color, size = 0.71) + 
  geom_line(color = w_color, size = 1.8) + 
  scale_y_continuous(limits = c(37.5, 45.6), sec.axis = sec_axis(~. / 2), expand = c(0, 0)) + 
  geom_line(data = line_data, aes(year, weight*2.1), color = n_color, size = 1.8) + 
  scale_x_continuous(breaks = seq(2006, 2015), expand = c(0, 0), limits = c(2005.4, 2015.6), labels = my_labels) + 
  labs(title = "Fit as a butcher's dog", 
       subtitle = "Characteristics of dogs registered with the UK's\nKennel Club, average when fully grown", 
       caption = "Source: Kennel Group | Graphic Designer: Nguyen Chi Dung") + 
  theme(plot.background = element_rect(fill = bgr_color, color = NA)) + 
  theme(panel.background = element_rect(fill = bgr_color, color = NA)) + 
  theme(axis.title = element_blank()) + 
  theme(plot.margin = unit(rep(0.7, 4), "cm")) + 
  theme(panel.grid.major = element_blank()) + 
  theme(panel.grid.minor = element_blank()) + 
  theme(axis.ticks.y = element_blank()) +  
  theme(axis.text.y = element_blank()) + 
  annotate("text", x = 2006 - a, y = seq(38, 44, 2) + b, label = seq(38, 44, 2), color = w_color, family = font_y, size = 5) + 
  annotate("text", x = 2015 + a, y = seq(38, 44, 2) + b, label = seq(18, 21, 1), color = n_color, family = font_y, size = 5) + 
  annotate("text", x = 2006 + 0.5, y =  45, label = "Neck size, cm", color = w_color, family = font_y, size = 5.5) + 
  annotate("text", x = 2014 + 0.8, y =  45, label = "Weight, kg", color = n_color, family = font_y, size = 5.5) + 
  geom_segment(aes(x = 2006, xend = 2015, y = 37.5, yend = 37.5), color = "grey30", size = 0.75) + 
  theme(axis.text.x = element_text(size = 15, family = font_y)) + 
  theme(axis.ticks.length.x = unit(0.2, "cm")) + 
  theme(plot.title = element_text(family = font_y, size = 21, face = "bold")) + 
  theme(plot.subtitle = element_text(family = font_y, size = 16, color = "grey10")) + 
  theme(plot.caption = element_text(family = font_y, size = 12, color = "grey40", hjust = 0, vjust = -1))

# 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))