700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 0402-E · US Tuition Costs · ggplot2 地图 热力图 gganimate 动图 · R 语言数据可视化 案例 源码

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

时间:2024-01-17 07:51:50

相关推荐

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

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

年合集传送门:

US Tuition Costs

Average Tuition and Educational Attainment in the United States。

Tidy Tuesday 在 GitHub 上的传送地址:

Thomas Mock (). Tidy Tuesday: A weekly data project aimed at the R ecosystem./rfordatascience/tidytuesday

1. 一些环境设置

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

2. 设置工作路径

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

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

3. 加载 R 包

# fiftystater: /wmurphyrd/fiftystater# install.packages("devtools")# devtools::install_github("wmurphyrd/fiftystater")# gganimate: /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…## $ `-05` <dbl> 5682.838, 4328.281, 5138.495, 5772.302, 5285.921, 4703.777, …## $ `-06` <dbl> 5840.550, 4632.623, 5415.516, 6082.379, 5527.881, 5406.967, …## $ `-07` <dbl> 5753.496, 4918.501, 5481.419, 6231.977, 5334.826, 5596.348, …## $ `-08` <dbl> 6008.169, 5069.822, 5681.638, 6414.900, 5672.472, 6227.002, …## $ `-09` <dbl> 6475.092, 5075.482, 6058.464, 6416.503, 5897.888, 6284.137, …## $ `-10` <dbl> 7188.954, 5454.607, 7263.204, 6627.092, 7258.771, 6948.473, …## $ `-11` <dbl> 8071.134, 5759.153, 8839.605, 6900.912, 8193.739, 7748.201, …## $ `-12` <dbl> 8451.902, 5762.421, 9966.716, 7028.991, 9436.426, 8315.632, …## $ `-13` <dbl> 9098.069, 6026.143, 10133.503, 7286.580, 9360.574, 8792.856,…## $ `-14` <dbl> 9358.929, 6012.445, 10296.200, 7408.495, 9274.193, 9292.954,…## $ `-15` <dbl> 9496.084, 6148.808, 10413.844, 7606.410, 9186.824, 9298.599,…## $ `-16` <dbl> 9751.101, 6571.340, 10646.278, 7867.297, 9269.844, 9748.188,…

# 检查数据的列名colnames(df_input)

## [1] "State" "-05" "-06" "-07" "-08" "-09" "-10"## [8] "-11" "-12" "-13" "-14" "-15" "-16"

5. 数据预处理

# 调整年份为 YYYY 格式, 方便绘图展示df_tidy <- df_input %>%dplyr::rename("" = "-05","" = "-06","" = "-07","" = "-08","" = "-09","" = "-10","" = "-11","" = "-12","" = "-13","" = "-14","" = "-15","" = "-16")# 第一张图片数据预处理df_tidy.1 <- df_tidy %>%# 建议使用 dplyr::mutate 形式调用函数, 有可能与 plyr 中的函数冲突 (因为我自己就报错了...)dplyr::mutate(id = tolower(State)) %>%dplyr::mutate(cagr = ((``/``)**(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> "", "", "", "", "", "", "", "", "…## $ 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 = "从到美国各种学费的年均变化",x = NULL,y = NULL,caption = "资料来源: <i></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 = '0402-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 = "资料来源: · 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 = '0402-E-02'anim_save(filename = paste0(filename, ".gif"))

8. session-info

sessionInfo()

## R version 4.2.1 (-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=CLC_TELEPHONE=C ## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C ## ## attached base packages:## [1] statsgraphics grDevices utilsdatasets 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.1dplyr_1.0.10purrr_0.3.4 readr_2.1.2## [9] tidyr_1.2.1 tibble_3.1.8ggplot2_3.3.6tidyverse_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.2httr_1.4.4tools_4.2.1 ## [7] backports_1.4.1bslib_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.2compiler_4.2.1## [19] textshaping_0.3.6 cli_3.3.0 rvest_1.0.3 ## [22] xml2_1.3.3labeling_0.4.2sass_0.4.2 ## [25] scales_1.2.1 classInt_0.4-8proxy_0.4-27 ## [28] systemfonts_1.0.4 digest_0.6.29 rmarkdown_2.16## [31] pkgconfig_2.0.3htmltools_0.5.3highr_0.9## [34] dbplyr_2.2.1 fastmap_1.1.0 maps_3.4.0 ## [37] rlang_1.0.5 ggthemes_4.2.4readxl_1.4.1 ## [40] rstudioapi_0.14sysfonts_0.8.8jquerylib_0.1.4 ## [43] farver_2.1.1 generics_0.1.3jsonlite_1.8.0## [46] googlesheets4_1.0.1 magrittr_2.0.3Rcpp_1.0.9 ## [49] munsell_0.5.0 fansi_1.0.3 lifecycle_1.0.1 ## [52] stringi_1.7.8 yaml_2.3.5grid_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.2evaluate_0.16## [67] gifski_1.6.6-1modelr_0.1.9 vctrs_0.4.1 ## [70] tzdb_0.3.0tweenr_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.3class_7.3-20 googledrive_2.0.0 ## [82] gargle_1.2.1 units_0.8-0 ellipsis_0.3.2

测试数据

配套数据下载:us_avg_tuition.xlsx

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。