Plot của Financial Times được John Burn-Murdoch trích dẫn như sau:
Bằng cách sử dụng R chúng ta có thể “tái tạo” lại plot này như sau:
# R Codes
Dưới đây là R Codes cho area plot ở trên:
# Load original plot from: https://mobile.twitter.com/jburnmurdoch/status/1541097879984537602/photo/1
# Extract color codes from downloaded image by https://imagecolorpicker.com/en
# Clear R Environment:
rm(list = ls())
# Create a data set for ploting:
<- 1:7
my_time
<- c(0.3, 0.29, 0.31, 0.32, 0.35, 0.45, 0.6)
a_lot
<- c(1, 0.95, 1.1, 1.2, 1.25, 1.7, 2.2)
a_little
<- c(1.7, 1.6, 1.75, 1.8, 2.1, 2.6, 3.1)
not_limited
library(tidyverse)
<- tibble(my_time = my_time, a_lot = a_lot, a_little = a_little, not_limited = not_limited)
df_wide
#----------------
# Prepare data
#----------------
<- c("a_lot", "a_little", "not_limited")
my_levels
%>%
df_wide gather(type, value, -my_time) %>%
mutate(type = factor(type, level = my_levels[3:1])) -> df_long
<- c("May\n2021", "Jul", "Sep", "Nov", "Jan\n2022", "Mar", "May")
label_on_x
<- "#c94456"
a_lot_color
<- "#ea9077"
a_little_color
<- "#dfd2c8"
not_limited_color
<- "#eaded2"
grid_color
<- "#908b86"
text_color_grey
<- "#fff1e5"
bgr_color
<- "Rates of self-reported long Covid continue rise in the UK, with 2% of\npopulation now stating the condition is limiting their daily activities"
p_title
<- "Estimated prevalence of self-reported long Covid in the UK, by level of limitation\n(% of total population)"
p_subtittle
<- "Source: ONS Covid-19 infection survey\nFT graphic: Nguyen Chi Dung"
p_caption
library(showtext) # -> Package for using extra fonts.
<- "Roboto Condensed" # -> Use Roboto Condensed font for our plot.
my_font
font_add_google(name = my_font, family = my_font) # -> Load font.
showtext_auto() # -> Automatically render text:
library(ggtext)
%>%
df_long ggplot(aes(x = my_time, y = value, fill = type)) +
geom_area(position = "dodge", show.legend = FALSE, alpha = 0.8) +
theme_minimal() +
labs(title = p_title, subtitle = p_subtittle, caption = p_caption) +
scale_x_continuous(limits = c(1, 7), breaks = seq(1, 7, 1), expand = c(0, 0), labels = label_on_x) +
scale_y_continuous(position = "right", expand = c(0, 0)) +
scale_fill_manual(values = c(not_limited_color, a_little_color, a_lot_color)) +
theme(axis.title = element_blank()) +
theme(plot.margin = unit(rep(0.7, 4), "cm")) +
theme(panel.grid.minor = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.major.y = element_line(size = 0.8, color = grid_color)) +
theme(plot.background = element_rect(color = NA, fill = bgr_color)) +
theme(panel.background = element_rect(color = NA, fill = NA)) +
theme(axis.ticks.length.x = unit(0.2, "cm")) +
theme(axis.ticks.x = element_line(color = "grey50", size = 0.8)) +
theme(axis.text.x = element_text(family = my_font, size = 12, color = "grey40", hjust = 0.13)) +
theme(axis.text.y = element_text(family = my_font, size = 12, color = "grey40")) +
theme(plot.title = element_text(family = my_font, size = 19, color = "black")) +
theme(plot.subtitle = element_text(family = my_font, size = 15, color = "grey30")) +
theme(plot.caption = element_text(family = my_font, size = 10, color = "grey30", hjust = 0)) +
theme(axis.line.x = element_line(size = 0.8, color = "grey40")) +
annotate("text",
label = "Daily activities limited a alot",
family = my_font,
x = 4.2,
y = 0.12,
size = 5.5,
hjust = -0.5,
vjust = 0,
color = "white") +
annotate("text",
label = "Daily activities limited a little",
family = my_font,
x = 4.2,
y = 0.6,
size = 5.5,
hjust = -0.5,
vjust = 0,
color = "white",
angle = "7") +
annotate("text",
label = "Daily activities not limited",
family = my_font,
x = 4.2,
y = 1.25,
size = 5.5,
hjust = -0.5,
vjust = 0,
color = "grey50",
angle = "20")
# Make FT icon:
library(grid)
grid.rect(x = 0, y = 1, width = 0.06, height = 0.01, just = c("left", "top"), gp = gpar(fill = "black", col = "black"))