所有作品合集传送门: Tidy Tuesday
2018 年合集传送门: 2018
NFL Stats
欢迎来到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-08-28_NFL_Stats/src-b'
setwd(wkdir)
3. 加载 R 包
library(scico)
library(tidyverse)
# 导入字体设置包
library(showtext)
# font_add_google() showtext 中从谷歌字体下载并导入字体的函数
# name 中的是字体名称, 用于检索, 必须严格对应想要字体的名字
# family 后面的是代码后面引用时的名称, 自己随便起
# 需要能访问 Google, 也可以注释掉下面这行, 影响不大
# font_families_google() 列出所有支持的字体, 支持的汉字不多
# http://www.googlefonts.net/
font_add_google(name = "Karantina", family = "albert")
font_add_google(name = "ZCOOL XiaoWei", family = "zxw")
font_add_google(name = "Zhi Mang Xing", family = "zmx")
# 后面字体均可以使用导入的字体
showtext_auto()
4. 加载数据
df_input <- readr::read_csv("../data/nfl_2010-2017.csv", show_col_types = FALSE) %>% select(-1)
# 简要查看数据内容
glimpse(df_input)
## Rows: 81,525
## Columns: 22
## $ name <chr> "Duce Staley", "Lamar Smith", "Tiki Barber", "Stephen Dav…
## $ team <chr> "PHI", "MIA", "NYG", "WAS", "IND", "BAL", "NYJ", "MIN", "…
## $ game_year <dbl> 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 200…
## $ game_week <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ rush_att <dbl> 26, 27, 13, 23, 28, 27, 30, 14, 15, 10, 20, 13, 23, 14, 2…
## $ rush_yds <dbl> 201, 145, 144, 133, 124, 119, 110, 109, 88, 87, 84, 80, 7…
## $ rush_avg <dbl> 7.7, 5.4, 11.1, 5.8, 4.4, 4.4, 3.7, 7.8, 5.9, 8.7, 4.2, 6…
## $ rush_tds <dbl> 1, 1, 2, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 3, …
## $ rush_fumbles <dbl> 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 1, 1, …
## $ rec <dbl> 4, 1, 3, 4, 6, 4, 6, 2, 2, NA, 4, 3, 1, 4, 1, 1, 1, NA, N…
## $ rec_yds <dbl> 61, 12, 25, 37, 40, 32, 34, 3, 20, NA, 29, 10, -2, 100, 1…
## $ rec_avg <dbl> 15.3, 12.0, 8.3, 9.3, 6.7, 8.0, 5.7, 1.5, 10.0, NA, 7.3, …
## $ rec_tds <dbl> 0, 0, 0, 0, 1, 0, 1, 0, 0, NA, 0, 0, 0, 1, 0, 0, 0, NA, N…
## $ rec_fumbles <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, NA, N…
## $ pass_att <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 41, NA, NA, NA, NA, N…
## $ pass_yds <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 290, NA, NA, NA, NA, …
## $ pass_tds <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA…
## $ int <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA…
## $ sck <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA…
## $ pass_fumbles <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA…
## $ rate <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 102.7, NA, NA, NA, NA…
## $ position <chr> "RB", "RB", "RB", "RB", "RB", "RB", "RB", "RB", "RB", "QB…
# 检查数据的列名
colnames(df_input)
## [1] "name" "team" "game_year" "game_week" "rush_att"
## [6] "rush_yds" "rush_avg" "rush_tds" "rush_fumbles" "rec"
## [11] "rec_yds" "rec_avg" "rec_tds" "rec_fumbles" "pass_att"
## [16] "pass_yds" "pass_tds" "int" "sck" "pass_fumbles"
## [21] "rate" "position"
5. 数据预处理
df_ert <- df_input %>%
# group_by() 以指定的列进行分组
dplyr::group_by(team, game_year, game_week) %>%
# summarise() 用于对数据进行统计描述
dplyr::summarise(rush_total = sum(rush_yds, na.rm = TRUE),
pass_total = sum(pass_yds, na.rm = TRUE)) %>%
dplyr::group_by(game_year, game_week) %>%
dplyr::summarise(rush_mean = mean(rush_total, na.rm = TRUE),
pass_mean = mean(pass_total, na.rm = TRUE))
# 简要查看数据内容
glimpse(df_ert)
## Rows: 288
## Columns: 4
## Groups: game_year [18]
## $ game_year <dbl> 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, …
## $ game_week <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2,…
## $ rush_mean <dbl> 114.53333, 94.30000, 105.28571, 102.25000, 122.82143, 107.03…
## $ pass_mean <dbl> 208.1000, 249.2000, 212.5714, 223.5714, 232.1786, 219.8214, …
6. 利用 ggplot2 绘图
hh <- ggplot(df_ert, aes(game_year, game_week))
# geom_tile() 构建热图
hh <- hh + geom_tile(aes(fill = rush_mean), colour = "grey50")
# scico::scale_fill_scico() 针对连续型数据, 自定义图像的色彩梯度, scico::scico_palette_names() 可以查看所有支持的 palette 名称
hh <- hh + scale_fill_scico()
# scale_x_continuous() 对连续变量设置坐标轴显示范围
hh <- hh + scale_x_continuous(limits = c(1999.5, 2017.5), expand = c(0, 0), breaks = seq(2000, 2016, by = 2))
# scale_y_continuous() 对连续变量设置坐标轴显示范围
hh <- hh + scale_y_continuous(limits = c(0.5, 16.5), expand = c(0, 0), breaks = 1:16)
# guides() 设置图例信息
hh <- hh + guides(fill = guide_legend(title = '次数', title.position = "top", label.position = "bottom"))
# labs() 对图形添加注释和标签(包含标题 title、子标题 subtitle、坐标轴 x & y 和引用 caption 等注释)
hh <- hh + labs(title = "NFL 不想想什么标题了, 我是主标题, 占个位置",
subtitle = '其他绘图示例可以浏览我博客的其它内容',
x = '年份',
y = '比赛场数',
caption = "资料来源: Pro-Football-Reference.com · graph by 萤火之森 · 2022-10-22")
# scale_color_manual() 采取的是手动赋值的方法, 也就是直接把颜色序列赋值给它的参数 values, 也可以根据 breaks, labels 设定图例标签顺序
hh <- hh + scale_color_manual(values = c("#DC143C", "#FF8C00"), breaks = c('#DC143C', '#FF8C00'), labels = c('Group`A', 'Group`B'))
# theme_minimal() 去坐标轴边框的最小化主题
hh <- hh + theme_minimal()
# theme() 实现对非数据元素的调整, 对结果进行进一步渲染, 使之更加美观
hh <- hh + theme(
# panel.border 面板背景 数据上面
panel.border = element_blank(),
# panel.background 面板背景 数据下面
panel.background = element_rect(fill = '#E6E6FA', color = '#E6E6FA', size = 0),
# plot.background 图片背景
plot.background = element_rect(fill = '#E6E6FA', color = '#E6E6FA', size = 0),
# plot.margin 调整图像边距, 上-右-下-左
plot.margin = margin(12, 10, 12, 15),
# plot.title 主标题
plot.title = element_text(hjust = 0., color = "black", size = 28, face = "bold", family = 'zxw'),
# plot.subtitle 次要标题
plot.subtitle = element_text(hjust = 0., color = "black", size = 20, face = "bold", family = 'zmx'),
# plot.caption 说明文字
plot.caption = element_text(hjust = 0.85, vjust = -.57, size = 10, color = 'red'),
# axis.text 坐标轴刻度文本
axis.text = element_text(size = 14, face = "bold", family = 'albert'),
# axis.title 坐标轴标题
axis.title = element_text(size = 20, face = "bold", family = 'zxw'),
# legend.direction 设置图例的方向, horizontal 表示水平方向摆放
legend.direction = 'horizontal',
# legend.title 设置图例标题
legend.title = element_blank(),
# legend.text 设置图例文本格式
legend.text = element_text(size = 14, family = 'albert'),
# legend.background 设置图例的背景
legend.background = element_blank(),
# legend.position 设置图例位置, 这里用坐标来指定图例具体的摆放位置
legend.position = c(0.15, -0.12))
7. 保存图片到 PDF 和 PNG
hh
filename = '20180828-B-01'
ggsave(filename = paste0(filename, ".pdf"), width = 10.2, height = 6.5, device = cairo_pdf)
ggsave(filename = paste0(filename, ".png"), width = 10.2, height = 6.5, dpi = 100, device = "png", bg = '#E6E6FA')
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
## [13] scico_1.3.1
##
## loaded via a namespace (and not attached):
## [1] httr_1.4.4 sass_0.4.2 bit64_4.0.5
## [4] vroom_1.5.7 jsonlite_1.8.2 modelr_0.1.9
## [7] bslib_0.4.0 assertthat_0.2.1 highr_0.9
## [10] googlesheets4_1.0.1 cellranger_1.1.0 yaml_2.3.5
## [13] pillar_1.8.1 backports_1.4.1 glue_1.6.2
## [16] digest_0.6.30 rvest_1.0.3 colorspace_2.0-3
## [19] htmltools_0.5.3 pkgconfig_2.0.3 broom_1.0.1
## [22] haven_2.5.1 scales_1.2.1 tzdb_0.3.0
## [25] googledrive_2.0.0 generics_0.1.3 farver_2.1.1
## [28] ellipsis_0.3.2 cachem_1.0.6 withr_2.5.0
## [31] cli_3.4.1 magrittr_2.0.3 crayon_1.5.2
## [34] readxl_1.4.1 evaluate_0.16 fs_1.5.2
## [37] fansi_1.0.3 xml2_1.3.3 textshaping_0.3.6
## [40] tools_4.2.1 hms_1.1.2 gargle_1.2.1
## [43] lifecycle_1.0.3 munsell_0.5.0 reprex_2.0.2
## [46] compiler_4.2.1 jquerylib_0.1.4 systemfonts_1.0.4
## [49] rlang_1.0.6 grid_4.2.1 rstudioapi_0.14
## [52] labeling_0.4.2 rmarkdown_2.16 gtable_0.3.1
## [55] DBI_1.1.3 curl_4.3.3 R6_2.5.1
## [58] lubridate_1.8.0 knitr_1.40 fastmap_1.1.0
## [61] bit_4.0.4 utf8_1.2.2 ragg_1.2.3
## [64] stringi_1.7.8 parallel_4.2.1 vctrs_0.4.2
## [67] dbplyr_2.2.1 tidyselect_1.1.2 xfun_0.32
测试数据
配套数据下载:NFL Stats