所有作品合集传送门: Tidy Tuesday
2018 年合集传送门: 2018
Comic book characters
欢迎来到ggplot2
的世界!
ggplot2
是一个用来绘制统计图形的 R 软件包。它可以绘制出很多精美的图形,同时能避免诸多的繁琐细节,例如添加图例等。
用 ggplot2 绘制图形时,图形的每个部分可以依次进行构建,之后还可以进行编辑。ggplot2 精心挑选了一系列的预设图形,因此在大部分情形下可以快速地绘制出许多高质量的图形。如果在格式上还有额外的需求,也可以利用 ggplot2 中的主题系统来进行定制, 无需花费太多时间来调整图形的外观,而可以更加专注地用图形来展现你的数据。
1. 一些环境设置
# 设置为国内镜像, 方便快速安装模块
options("repos" = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
2. 设置工作路径
wkdir <- '/home/user/R_workdir/TidyTuesday/2018/2018-05-29_Comic_book_characters/src-c'
setwd(wkdir)
3. 加载 R 包
library(tidyverse)
# 导入字体设置包
library(showtext)
# font_add_google() showtext 中从谷歌字体下载并导入字体的函数
# name 中的是字体名称, 用于检索, 必须严格对应想要字体的名字
# family 后面的是代码后面引用时的名称, 自己随便起
# 需要能访问 Google, 也可以注释掉下面这行, 影响不大
# font_families_google() 列出所有支持的字体, 支持的汉字不多
# http://www.googlefonts.net/
font_add_google(name = "Karantina", family = "ka")
font_add_google(name = "Cutive", family = "albert")
font_add_google(name = "ZCOOL XiaoWei", family = "zxw")
# 后面字体均可以使用导入的字体
showtext_auto()
4. 加载数据
df_input <- readr::read_csv("../data/week9_comic_characters.csv", show_col_types = FALSE)
# 简要查看数据内容
glimpse(df_input)
## Rows: 23,272
## Columns: 17
## $ ...1 <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16…
## $ publisher <chr> "Marvel", "Marvel", "Marvel", "Marvel", "Marvel", "Ma…
## $ page_id <dbl> 1678, 7139, 64786, 1868, 2460, 2458, 2166, 1833, 2948…
## $ name <chr> "Spider-Man (Peter Parker)", "Captain America (Steven…
## $ urlslug <chr> "\\/Spider-Man_(Peter_Parker)", "\\/Captain_America_(…
## $ id <chr> "Secret Identity", "Public Identity", "Public Identit…
## $ align <chr> "Good Characters", "Good Characters", NA, "Good Chara…
## $ eye <chr> "Hazel Eyes", "Blue Eyes", "Blue Eyes", "Blue Eyes", …
## $ hair <chr> "Brown Hair", "White Hair", "Black Hair", "Black Hair…
## $ sex <chr> "Male Characters", "Male Characters", "Male Character…
## $ gsm <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ alive <chr> "Living Characters", "Living Characters", "Living Cha…
## $ appearances <dbl> 4043, 3360, 3061, 2961, 2258, 2255, 2072, 2017, 1955,…
## $ first_appearance <chr> "1962, August", "1941, March", "1974, October", "1963…
## $ month <chr> "August", "March", "October", "March", "November", "N…
## $ year <dbl> 1962, 1941, 1974, 1963, 1950, 1961, 1961, 1962, 1963,…
## $ date <date> 1962-08-01, 1941-03-01, 1974-10-01, 1963-03-01, 1950…
# 检查数据的列名
colnames(df_input)
## [1] "...1" "publisher" "page_id" "name"
## [5] "urlslug" "id" "align" "eye"
## [9] "hair" "sex" "gsm" "alive"
## [13] "appearances" "first_appearance" "month" "year"
## [17] "date"
5. 数据预处理
publisher. = c("DC电影宇宙", "漫威漫画公司")
df_albert <- df_input %>%
# group_by() 以指定的列进行分组
group_by(publisher, year) %>%
# summarise() 用于对数据进行统计描述, 这里根据上一步分组进行计数
summarise(count = n()) %>%
# ungroup() 取消分组
ungroup() %>%
# 因子化 publisher 列
mutate(publisher = factor(publisher, labels = publisher.))
# 去除缺失值
df_plot <- na.omit(df_albert)
# 简要查看数据内容
glimpse(df_plot)
## Rows: 154
## Columns: 3
## $ publisher <fct> DC电影宇宙, DC电影宇宙, DC电影宇宙, DC电影宇宙, DC电影宇宙, …
## $ year <dbl> 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, …
## $ count <int> 1, 9, 4, 10, 18, 64, 61, 52, 14, 15, 7, 9, 20, 20, 6, 9, 12,…
6. 利用 ggplot2 绘图
# PS: 方便讲解, 我这里进行了拆解, 具体使用时可以组合在一起
gg <- df_plot %>% ggplot(aes(x = year, y = count, fill = publisher))
gg <- gg + geom_bar(stat = "identity")
# scale_x_continuous() 对连续变量设置坐标轴显示范围
gg <- gg + scale_x_continuous(labels = c(1940, "'60", "'80", 2000), breaks = seq(1940, 2000, 20))
# scale_y_continuous() 对连续变量设置坐标轴显示范围
gg <- gg + scale_y_continuous(labels = seq(0, 500, 100), breaks = seq(0, 500, 100))
# scale_fill_manual() 采取的是手动赋值的方法, 也就是直接把颜色序列赋值给它的参数 value
gg <- gg + scale_fill_manual(values = c("#0000FF", "#FF0000"))
# labs() 对图形添加注释和标签(包含标题 title、子标题 subtitle、坐标轴 x & y 和引用 caption 等注释)
gg <- gg + labs(title = "每年推出的新漫画书人物数量 (1936-2013)",
subtitle = NULL,
x = NULL,
y = NULL,
caption = "资料来源: FiveThirtyEight.com · graph by 萤火之森 · 2022-10-17")
# theme_minimal() 去坐标轴边框的最小化主题
gg <- gg + theme_minimal()
# theme() 实现对非数据元素的调整, 对结果进行进一步渲染, 使之更加美观
gg <- gg + theme(
# panel.border 面板背景 数据上面
panel.border = element_blank(),
# plot.background 图片背景
plot.background = element_blank(),
# plot.margin 调整图像边距, 上-右-下-左
plot.margin = margin(12, 10, 2, 15),
# plot.title 主标题
plot.title = element_text(hjust = 0., color = "black", size = 32, face = "bold", family = 'zxw'),
# plot.subtitle 次要标题
plot.subtitle = element_text(hjust = 0., color = "black", size = 24, face = "bold"),
# strip.text.x 自定义分面图每个分面标题的文字
strip.text.x = element_text(size = 24, hjust = 0.5, face = "bold", family = 'zxw', color = 'red'),
# text 设置文本格式
text = element_text(size = 18, hjust = 0, face = "bold", color = 'black'),
# axis.text 坐标轴刻度文本
axis.text = element_text(size = 24, hjust = 0, face = "bold", color = 'black', family = 'ka'),
# legend.direction 设置图例的方向, horizontal 表示水平方向摆放
legend.direction = 'horizontal',
# legend.title 设置图例标题
legend.title = element_blank(),
# legend.position 设置图例位置, "none" 表示不显示图例
legend.position = 'none')
7. 保存图片到 PDF 和 PNG
gg + theme(legend.position = c(0.31234, 0.834))
filename = '20180529-C-01'
ggsave(filename = paste0(filename, ".pdf"), width = 11.2, height = 6.2, device = cairo_pdf)
ggsave(filename = paste0(filename, ".png"), width = 11.2, height = 6.2, dpi = 100, device = "png", bg = 'white')
gg + facet_wrap(~ publisher)
filename = '20180529-C-02'
ggsave(filename = paste0(filename, ".pdf"), width = 11.2, height = 6.2, device = cairo_pdf)
ggsave(filename = paste0(filename, ".png"), width = 11.2, height = 6.2, dpi = 100, device = "png", bg = 'white')
8. session-info
sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] showtext_0.9-5 showtextdb_3.0 sysfonts_0.8.8 forcats_0.5.2
## [5] stringr_1.4.1 dplyr_1.0.10 purrr_0.3.4 readr_2.1.2
## [9] tidyr_1.2.1 tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2
##
## loaded via a namespace (and not attached):
## [1] lubridate_1.8.0 assertthat_0.2.1 digest_0.6.29
## [4] utf8_1.2.2 R6_2.5.1 cellranger_1.1.0
## [7] backports_1.4.1 reprex_2.0.2 evaluate_0.16
## [10] highr_0.9 httr_1.4.4 pillar_1.8.1
## [13] rlang_1.0.6 curl_4.3.2 googlesheets4_1.0.1
## [16] readxl_1.4.1 rstudioapi_0.14 jquerylib_0.1.4
## [19] rmarkdown_2.16 textshaping_0.3.6 googledrive_2.0.0
## [22] bit_4.0.4 munsell_0.5.0 broom_1.0.1
## [25] compiler_4.2.1 modelr_0.1.9 xfun_0.32
## [28] systemfonts_1.0.4 pkgconfig_2.0.3 htmltools_0.5.3
## [31] tidyselect_1.1.2 fansi_1.0.3 crayon_1.5.1
## [34] tzdb_0.3.0 dbplyr_2.2.1 withr_2.5.0
## [37] grid_4.2.1 jsonlite_1.8.2 gtable_0.3.1
## [40] lifecycle_1.0.3 DBI_1.1.3 magrittr_2.0.3
## [43] scales_1.2.1 vroom_1.5.7 cli_3.4.1
## [46] stringi_1.7.8 cachem_1.0.6 farver_2.1.1
## [49] fs_1.5.2 xml2_1.3.3 bslib_0.4.0
## [52] ragg_1.2.3 ellipsis_0.3.2 generics_0.1.3
## [55] vctrs_0.4.2 tools_4.2.1 bit64_4.0.5
## [58] glue_1.6.2 hms_1.1.2 parallel_4.2.1
## [61] fastmap_1.1.0 yaml_2.3.5 colorspace_2.0-3
## [64] gargle_1.2.1 rvest_1.0.3 knitr_1.40
## [67] haven_2.5.1 sass_0.4.2
测试数据
配套数据下载:Comic book characters