mdthemes:在 ggplot2 绘图中应用 Markdown 语法

Thomas Neitmann 的 mdthemes 包最近上架 CRAN 了,昨天看了下他博客上的文章:https://thomasadventure.blog/posts/mdthemes-is-on-cran-markdown-powered-themes-for-ggplot2/ 感觉还是蛮有意思的。我就用疫情数据测试了这个包。

首先安装:

install.packages('mdthemes')

加载:

library(mdthemes)
library(tidyverse)
library(ggplot2)

附件中有一份 world-covid19.dta 数据,是全球各国新冠疫情数据,可以使用 haven 包的 read_dta 函数将这份数据读入 R:

# 读入 R
library(haven)
read_dta('world-covid19.dta') -> coviddf
coviddf

#> # A tibble: 28,917 x 7
#> iso country country_en date confirmed recovered deaths
#> <chr> <chr> <chr> <date> <dbl> <dbl> <dbl>
#> 1 BTN 不丹 Bhutan 2020-01-22 0 0 0
#> 2 BTN 不丹 Bhutan 2020-01-23 0 0 0
#> 3 BTN 不丹 Bhutan 2020-01-24 0 0 0
#> 4 BTN 不丹 Bhutan 2020-01-25 0 0 0
#> 5 BTN 不丹 Bhutan 2020-01-26 0 0 0
#> 6 BTN 不丹 Bhutan 2020-01-27 0 0 0
#> 7 BTN 不丹 Bhutan 2020-01-28 0 0 0
#> 8 BTN 不丹 Bhutan 2020-01-29 0 0 0
#> 9 BTN 不丹 Bhutan 2020-01-30 0 0 0
#> 10 BTN 不丹 Bhutan 2020-01-31 0 0 0
#> # … with 28,907 more rows

筛选出数据集中最后一天的数据:

coviddf %>%
top_n(1, date) -> lastdf
lastdf

#> # A tibble: 189 x 7
#> iso country country_en date confirmed recovered deaths
#> <chr> <chr> <chr> <date> <dbl> <dbl> <dbl>
#> 1 BTN 不丹 Bhutan 2020-06-22 68 32 0
#> 2 TMP 东帝汶 East Timor 2020-06-22 24 24 0
#> 3 CHN 中国 People's Republic Of… 2020-06-22 85070 79982 4646
#> 4 CAF 中非 Central Africa 2020-06-22 2963 495 30
#> 5 DNK 丹麦 Denmark 2020-06-22 12727 11547 602
#> 6 UKR 乌克兰 Ukraine 2020-06-22 38056 17211 1022
#> 7 UZB 乌兹别克斯… Uzbekistan 2020-06-22 6461 4450 19
#> 8 UGA 乌干达 Uganda 2020-06-22 774 631 0
#> 9 URY 乌拉圭 Uruguay 2020-06-22 882 815 25
#> 10 TCD 乍得 Chad 2020-06-22 858 755 74
#> # … with 179 more rows

观察病死人数和确诊人数的关系:

ggplot(lastdf, aes(confirmed, deaths)) +
geom_point() +
geom_smooth() +
theme_modern_rc(base_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont) +
labs(title = "各国病死人数和确诊人数的关系:COVID-19",
subtitle = "2020 年 6 月 22 日|绘制:RStata",
x = "确诊", y = "病死",
caption = "数据来源:约翰斯·霍普金斯大学") +
scale_x_continuous(trans = "log",
breaks = c(1, 10, 100, 1000,
10000, 100000, 1000000)) +
scale_y_continuous(trans = "log",
breaks = c(1, 10, 100, 1000,
10000, 100000, 1000000))
图一

使用 md_theme_modern_rc() 代替上面的 theme_modern_rc() 就可以方便的使用 Markdown 语法修改一些元素了。

虽然我们可以使用 Markdown 语法中的 *斜体* **粗体** 等,但是这里我使用了 cnfont,这让字体的调整失去作用,所以我这里不再演示斜体和粗体的设置了。下面的代码演示了颜色的设置:

ggplot(lastdf, aes(confirmed, deaths)) +
geom_point() +
geom_smooth() +
labs(title = "*各国<span style = 'color:#E31A1C'>病死人数</span>和<span style = 'color:#CCBE93'>确诊人数</span>的关系:COVID-19*",
subtitle = "2020 年 6 月 22 日|绘制:RStata",
x = "确诊", y = "病死",
caption = "数据来源:约翰斯·霍普金斯大学") +
scale_x_continuous(trans = "log",
breaks = c(1, 10, 100, 1000,
10000, 100000, 1000000)) +
scale_y_continuous(trans = "log",
breaks = c(1, 10, 100, 1000,
10000, 100000, 1000000)) +
md_theme_modern_rc(base_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont)
图二

该作者还有另外一个很好用的 R 包:ggcharts,可以方便的绘制一些图表,例如这里我展示确诊人数最多的十个国家并且高亮美国和印度:

安装 ggcharts:

install.packages('ggcharts')

绘图:

library(ggcharts)

# 高亮美国和印度的标签
label <- function(x) {
if (x %in% c("美国", "印度")) {
paste0("<span style='color:#D52B1E'>**", x, "**</span>")
} else {
paste0("<span style='color:gray'>", x, "</span>")
}
}

# 设定高亮的两个国家:美国和印度
spec <- highlight_spec(
what = c("美国", "印度"),
highlight_color = "#D52B1E",
other_color = "gray"
)

lastdf %>%
bar_chart(country, confirmed,
highlight = spec,
color = NA,
top_n = 10) +
scale_x_discrete(labels = Vectorize(label)) +
labs(
x = NULL,
y = "截止6 月 22 日确诊数量",
title = glue::glue("截止 6 月 22 日 {shiny::span('**Swiss**', style='color:#D52B1E')} 人数最多的十个国家")
) +
md_theme_modern_rc(base_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont)

作业:使用中国的新冠疫情数据模仿上面的代码进行分析

导入中国疫情数据:

haven::read_dta('china-covid19.dta')

#> # A tibble: 58,102 x 8
#> date provcode prov citycode city confirmed recovered deaths
#> <date> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 2019-12-01 420000 湖北 420100 武汉 1 0 0
#> 2 2019-12-02 420000 湖北 420100 武汉 1 0 0
#> 3 2019-12-03 420000 湖北 420100 武汉 1 0 0
#> 4 2019-12-04 420000 湖北 420100 武汉 1 0 0
#> 5 2019-12-05 420000 湖北 420100 武汉 1 0 0
#> 6 2019-12-06 420000 湖北 420100 武汉 1 0 0
#> 7 2019-12-07 420000 湖北 420100 武汉 1 0 0
#> 8 2019-12-08 420000 湖北 420100 武汉 1 0 0
#> 9 2019-12-09 420000 湖北 420100 武汉 1 0 0
#> 10 2019-12-10 420000 湖北 420100 武汉 1 0 0
#> # … with 58,092 more rows

点击这里跳转到 RStata 短书平台获取附件:mdthemes:在 ggplot2 绘图中应用 Markdown 语法

评论