20180402-E · US Tuition Costs · ggplot2, 地图 热力图 gganimate 动图 · R 语言数据可视化 案例 源码

所有作品合集传送门: Tidy Tuesday

2018 年合集传送门: 2018

US Tuition Costs

Average Tuition and Educational Attainment in the United States


Tidy Tuesday 在 GitHub 上的传送地址:
Thomas Mock (2022). Tidy Tuesday: A weekly data project aimed at the R ecosystem. https://github.com/rfordatascience/tidytuesday


在这里插入图片描述

在这里插入图片描述



1. 一些环境设置

# 设置为国内镜像, 方便快速安装模块
options("repos" = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))

2. 设置工作路径

wkdir <- '/home/user/R_workdir/TidyTuesday/2018/2018-04-02_US_Tuition_Costs/src-e'
setwd(wkdir)

一些关于字体的设置可以参考这篇文章 R/ggplot2保存图片中文字体至PDF——showtext包一文清除所有障碍 。需要更多的相关字体设置知识可以自行学习帮助文档或检索其他文章,这里不多加累述了。

3. 加载 R 包

# fiftystater: https://github.com/wmurphyrd/fiftystater
# install.packages("devtools")
# devtools::install_github("wmurphyrd/fiftystater")

# gganimate: https://github.com/thomasp85/gganimate
# devtools::install_github('thomasp85/gganimate')

library(ggtext)
library(tidyverse)
library(gganimate)
library(transformr)
library(fiftystater)
# 需要能访问 Google, 也可以注释掉下面这行, 影响不大
sysfonts::font_add_google("Gochi Hand", "gochi")

4. 加载数据

df_input <- readxl::read_excel("../data/us_avg_tuition.xlsx")

# 简要查看数据内容
glimpse(df_input)
## Rows: 50
## Columns: 13
## $ State     <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "Caalbert", "C…
## $ `2004-05` <dbl> 5682.838, 4328.281, 5138.495, 5772.302, 5285.921, 4703.777, …
## $ `2005-06` <dbl> 5840.550, 4632.623, 5415.516, 6082.379, 5527.881, 5406.967, …
## $ `2006-07` <dbl> 5753.496, 4918.501, 5481.419, 6231.977, 5334.826, 5596.348, …
## $ `2007-08` <dbl> 6008.169, 5069.822, 5681.638, 6414.900, 5672.472, 6227.002, …
## $ `2008-09` <dbl> 6475.092, 5075.482, 6058.464, 6416.503, 5897.888, 6284.137, …
## $ `2009-10` <dbl> 7188.954, 5454.607, 7263.204, 6627.092, 7258.771, 6948.473, …
## $ `2010-11` <dbl> 8071.134, 5759.153, 8839.605, 6900.912, 8193.739, 7748.201, …
## $ `2011-12` <dbl> 8451.902, 5762.421, 9966.716, 7028.991, 9436.426, 8315.632, …
## $ `2012-13` <dbl> 9098.069, 6026.143, 10133.503, 7286.580, 9360.574, 8792.856,…
## $ `2013-14` <dbl> 9358.929, 6012.445, 10296.200, 7408.495, 9274.193, 9292.954,…
## $ `2014-15` <dbl> 9496.084, 6148.808, 10413.844, 7606.410, 9186.824, 9298.599,…
## $ `2015-16` <dbl> 9751.101, 6571.340, 10646.278, 7867.297, 9269.844, 9748.188,…
# 检查数据的列名
colnames(df_input)
##  [1] "State"   "2004-05" "2005-06" "2006-07" "2007-08" "2008-09" "2009-10"
##  [8] "2010-11" "2011-12" "2012-13" "2013-14" "2014-15" "2015-16"

5. 数据预处理

# 调整年份为 YYYY 格式, 方便绘图展示
df_tidy <- df_input %>%
  dplyr::rename(
    "2004" = "2004-05",
    "2005" = "2005-06",
    "2006" = "2006-07",
    "2007" = "2007-08",
    "2008" = "2008-09",
    "2009" = "2009-10",
    "2010" = "2010-11",
    "2011" = "2011-12",
    "2012" = "2012-13",
    "2013" = "2013-14",
    "2014" = "2014-15",
    "2015" = "2015-16")

# 第一张图片数据预处理
df_tidy.1 <- df_tidy %>%
  # 建议使用 dplyr::mutate 形式调用函数, 有可能与 plyr 中的函数冲突 (因为我自己就报错了...)
  dplyr::mutate(id = tolower(State)) %>%
  dplyr::mutate(cagr = ((`2015`/`2004`)**(1/11)-1)*100) %>%
  select(id, cagr)

# 第二张图片数据预处理
df_tidy.2 <- df_tidy %>%
  dplyr::mutate(id = tolower(State)) %>%
  select(-State) %>%
  gather(key = year, value = cost, -id)


# 简要查看数据内容
glimpse(df_tidy.1)
## Rows: 50
## Columns: 2
## $ id   <chr> "alabama", "alaska", "arizona", "arkansas", "california", "colora…
## $ cagr <dbl> 5.0309094, 3.8688482, 6.8464675, 2.8549355, 5.2391738, 6.8490463,…
glimpse(df_tidy.2)
## Rows: 600
## Columns: 3
## $ id   <chr> "alabama", "alberta", "arizona", "arkansas", "california", "colora…
## $ year <chr> "2004", "2004", "2004", "2004", "2004", "2004", "2004", "2004", "…
## $ cost <dbl> 5682.838, 4328.281, 5138.495, 5772.302, 5285.921, 4703.777, 7983.…

6. 第一张图

6.1 用 ggplot2 开始绘图

# PS: 方便讲解, 我这里进行了拆解, 具体使用时可以组合在一起
gg <- ggplot(df_tidy.1, aes(map_id = id))
# geom_map() 绘制地图
gg <- gg + geom_map(aes(fill = cagr), color = "azure4", map = fifty_states)
# expand_limits() 控制坐标轴的范围
gg <- gg + expand_limits(x = fifty_states$long, y = fifty_states$lat)
# coord_map() 将地图变成球状展示
gg <- gg + coord_map()
# scale_x_continuous() 对连续变量设置坐标轴显示范围
gg <- gg + scale_x_continuous(breaks = NULL)
# scale_y_continuous() 对连续变量设置坐标轴显示范围
gg <- gg + scale_y_continuous(breaks = NULL)
# geom_text() 添加文本信息
gg <- gg + geom_text(x = -68.5, y = 36.5, label = "CAGR (%)", size = 4) 
gg <- gg + ggthemes::theme_fivethirtyeight()
# scale_fill_continuous() 针对连续型数据, 自定义图像的色彩梯度
gg <- gg +scale_fill_continuous(name = "", low = "#00FF00", high = "#FF0000")
# labs() 对图形添加注释和标签(包含标题、子标题、坐标轴和引用等注释)
gg <- gg + labs(title = "从2004年到2016年美国各种学费的年均变化",
                x = NULL,
                y = NULL,
                caption = "资料来源: <i>onlinembapage.com</i> · graph by 萤火之森")
# theme_minimal() 去坐标轴边框的最小化主题
gg <- gg + theme_minimal()
# theme() 实现对非数据元素的调整, 对结果进行进一步渲染, 使之更加美观
gg <- gg + theme(
  # plot.margin 调整图像边距, 上-右-下-左
  plot.margin = grid::unit(c(9, 16, 9, 16), "mm"),
  # aspect.ratio 固定图像的纵横比
  aspect.ratio = 9/16,
  # panel.background 面板背景 数据下面
  panel.background = element_blank(),
  # plot.title 主标题
  plot.title = element_text(hjust = 0.5, size = 32),
  # plot.caption 说明文字
  plot.caption = element_markdown(colour = "dodgerblue4", size = 12),
  # legend.text 设置图例文本格式
  legend.text = element_text(size = 12),
  # legend.title 设置图例标题
  legend.title = element_text(size = 12),
  # legend.position 设置图例位置, 这里用坐标来指定图例的位置
  legend.position = c('.88', '.2'),
  # legend.key.size 调整图例的大小
  legend.key.size = unit(.75, 'cm'),
  # plot.background 图片背景
  plot.background = element_rect(fill = "white"))

6.2 保存图片到 PDF 和 PNG

gg

在这里插入图片描述

filename = '20180402-E-01'
ggsave(filename = paste0(filename, ".pdf"), width = 12.2, height = 7.6, device = cairo_pdf)
ggsave(filename = paste0(filename, ".png"), width = 12.2, height = 7.6, dpi = 100, device = "png")

7. 第二张图

6.1 用 ggplot2 开始绘图

# PS: 方便讲解, 我这里进行了拆解, 具体使用时可以组合在一起
gg <- ggplot(df_tidy.2, aes(frame = year, map_id = id, group = interaction(year, id)))
# geom_map() 绘制地图
gg <- gg + geom_map(aes(fill = cost), color = "azure4", map = fifty_states)
# expand_limits() 控制坐标轴的范围
gg <- gg + expand_limits(x = fifty_states$long, y = fifty_states$lat)
# coord_map() 将地图变成球状展示
gg <- gg + coord_map()
# scale_x_continuous() 对连续变量设置坐标轴显示范围
gg <- gg + scale_x_continuous(breaks = NULL)
# scale_y_continuous() 对连续变量设置坐标轴显示范围
gg <- gg + scale_y_continuous(breaks = NULL, labels = function(x) format(x, big.mark = ".", scientific = FALSE))
# transition_states() 在动画中数据的几个不同阶段之间的转换
gg <- gg + transition_states(year, transition_length = 1, state_length = 4)
# guides() 设置图例信息
gg <- gg + guides(fill = guide_legend(title = "学费及相关的费用"))
# scale_fill_gradientn() 将颜色比例转换为概率转换颜色分布
gg <- gg + scale_fill_gradient(low = "#00FF7F", high = "#DC143C")
gg <- gg + ggthemes::theme_fivethirtyeight() 
# labs() 对图形添加注释和标签(包含标题、子标题、坐标轴和引用等注释)
gg <- gg + labs(title = "美国各州的平均学费:{closest_state} 年",
                subtitle = '',
                x = NULL,
                y = NULL,
                caption = "资料来源: onlinembapage.com · graph by 萤火之森")
# theme_minimal() 去坐标轴边框的最小化主题
gg <- gg + theme_minimal()
# theme() 实现对非数据元素的调整, 对结果进行进一步渲染, 使之更加美观
gg <- gg + theme(
  # panel.grid.major 主网格线, 这一步表示删除主要网格线
  panel.grid.major = element_line("grey", size = 0.2),
  # panel.grid.minor 次网格线, 这一步表示删除次要网格线
  panel.grid.minor = element_blank(),
  # plot.margin 调整图像边距, 上-右-下-左
  plot.margin = grid::unit(c(9, 16, 9, 16), "mm"),
  # aspect.ratio 固定图像的纵横比
  aspect.ratio = 9/16,
  # panel.background 面板背景 数据下面
  panel.background = element_blank(),
  # plot.title 主标题
  plot.title = element_text(family = 'gochi',hjust = 0.5, size = 40),
  # plot.subtitle 次要标题
  plot.subtitle = element_markdown(size = 20),
  # plot.caption 说明文字
  plot.caption = element_markdown(colour = "red", size = 24),
  # legend.text 设置图例文本格式
  legend.text = element_text(family = 'gochi',size = 20),
  # legend.title 设置图例标题
  legend.title = element_text(family = 'gochi',size = 20),
  # legend.position 设置图例位置, 这里用坐标来指定图例的位置
  legend.position = c('.79', '.92'),
  # legend.key.size 调整图例的大小
  legend.key.size = unit(.75, 'cm'),
  # legend.box.background 设置图例背景
  legend.box.background = element_blank(),
  # plot.background 图片背景
  plot.background = element_blank())

7.2 保存图片到 PDF 和 PNG

# 通过调整 duration 参数设置动画长度
animate(gg, renderer = gifski_renderer(), width = 1580, height = 1000, duration = length(unique(df_tidy.2$year)))

在这里插入图片描述

filename = '20180402-E-02'
anim_save(filename = paste0(filename, ".gif"))

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: albert
## 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] fiftystater_1.0.1 transformr_0.1.4  gganimate_1.0.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] ggtext_0.1.2     
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.2            sf_1.0-8            lubridate_1.8.0    
##  [4] progress_1.2.2      httr_1.4.4          tools_4.2.1        
##  [7] backports_1.4.1     bslib_0.4.0         utf8_1.2.2         
## [10] R6_2.5.1            KernSmooth_2.23-20  DBI_1.1.3          
## [13] colorspace_2.0-3    withr_2.5.0         tidyselect_1.1.2   
## [16] prettyunits_1.1.1   curl_4.3.2          compiler_4.2.1     
## [19] textshaping_0.3.6   cli_3.3.0           rvest_1.0.3        
## [22] xml2_1.3.3          labeling_0.4.2      sass_0.4.2         
## [25] scales_1.2.1        classInt_0.4-8      proxy_0.4-27       
## [28] systemfonts_1.0.4   digest_0.6.29       rmarkdown_2.16     
## [31] pkgconfig_2.0.3     htmltools_0.5.3     highr_0.9          
## [34] dbplyr_2.2.1        fastmap_1.1.0       maps_3.4.0         
## [37] rlang_1.0.5         ggthemes_4.2.4      readxl_1.4.1       
## [40] rstudioapi_0.14     sysfonts_0.8.8      jquerylib_0.1.4    
## [43] farver_2.1.1        generics_0.1.3      jsonlite_1.8.0     
## [46] googlesheets4_1.0.1 magrittr_2.0.3      Rcpp_1.0.9         
## [49] munsell_0.5.0       fansi_1.0.3         lifecycle_1.0.1    
## [52] stringi_1.7.8       yaml_2.3.5          grid_4.2.1         
## [55] crayon_1.5.1        haven_2.5.1         gridtext_0.1.5     
## [58] mapproj_1.2.8       hms_1.1.2           knitr_1.40         
## [61] pillar_1.8.1        markdown_1.1        lpSolve_5.6.16     
## [64] reprex_2.0.2        glue_1.6.2          evaluate_0.16      
## [67] gifski_1.6.6-1      modelr_0.1.9        vctrs_0.4.1        
## [70] tzdb_0.3.0          tweenr_2.0.2        cellranger_1.1.0   
## [73] gtable_0.3.1        assertthat_0.2.1    cachem_1.0.6       
## [76] xfun_0.32           broom_1.0.1         e1071_1.7-11       
## [79] ragg_1.2.3          class_7.3-20        googledrive_2.0.0  
## [82] gargle_1.2.1        units_0.8-0         ellipsis_0.3.2

测试数据

配套数据下载:us_avg_tuition.xlsx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值