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

2018 年合集传送门: 2018

Global Mortality

What do people die from?

在过去的几个世纪里,世界发生了很大的变化–这就是《我们的世界》的数据所显示的。然而,有一件事在这种转变中一直保持不变:我们都必须在某个时候死亡。然而,随着生活水平的提高、医疗保健的进步和生活方式的改变,死亡的原因正在发生变化。

在这篇博客中,我们试图回答 “人们死于什么?”,首先看一下全球死因的数据,然后选择国家层面的例子。
世界各地的主要死因仍有很大差异,因此,也可以选择了一些国家,以突出这种异质性。

本次示例通过一些可视化方式来展示这些信息。

plotly包是一个基于浏览器的交互式图表库,建立在开源的 JavaScript 图表库 plotly.js 上。

1. 一些环境设置

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

2. 设置工作路径

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

3. 加载 R 包

library(scales)
library(tidyverse)
library(glue)
library(plotly)
library(ggpubr)
library(showtext)
# 在 Ubuntu 系统上测试的, 不加这个我画出来的汉字会乱码 ~
showtext_auto()

4. 加载数据

df_input <- readxl::read_excel("../data/global_mortality.xlsx")# 简要查看数据内容
glimpse(df_input)
## Rows: 6,156
## Columns: 35
## $ country                                    <chr> "Afghanistan", "Afghanistan…
## $ country_code                               <chr> "AFG", "AFG", "AFG", "AFG",…
## $ year                                       <dbl> 1990, 1991, 1992, 1993, 199…
## $ `Cardiovascular diseases (%)`              <dbl> 17.61040, 17.80181, 18.3868…
## $ `Cancers (%)`                              <dbl> 4.025975, 4.054145, 4.17395…
## $ `Respiratory diseases (%)`                 <dbl> 2.106626, 2.134176, 2.20829…
## $ `Diabetes (%)`                             <dbl> 3.832555, 3.822228, 3.90012…
## $ `Dementia (%)`                             <dbl> 0.5314287, 0.5324973, 0.540…
## $ `Lower respiratory infections (%)`         <dbl> 10.886362, 10.356968, 10.09…
## $ `Neonatal deaths (%)`                      <dbl> 9.184653, 8.938897, 8.84138…
## $ `Diarrheal diseases (%)`                   <dbl> 2.497141, 2.572228, 2.70774…
## $ `Road accidents (%)`                       <dbl> 3.715944, 3.729142, 3.81635…
## $ `Liver disease (%)`                        <dbl> 0.8369093, 0.8455159, 0.874…
## $ `Tuberculosis (%)`                         <dbl> 5.877075, 5.891704, 6.03466…
## $ `Kidney disease (%)`                       <dbl> 1.680611, 1.671115, 1.70098…
## $ `Digestive diseases (%)`                   <dbl> 1.058771, 1.049322, 1.06288…
## $ `HIV/AIDS (%)`                             <dbl> 0.01301948, 0.01451458, 0.0…
## $ `Suicide (%)`                              <dbl> 0.4366105, 0.4422802, 0.456…
## $ `Malaria (%)`                              <dbl> 0.4488863, 0.4550191, 0.460…
## $ `Homicide (%)`                             <dbl> 1.287020, 1.290991, 1.32616…
## $ `Nutritional deficialbert (%)`             <dbl> 0.3505045, 0.3432123, 0.345…
## $ `Meningitis (%)`                           <dbl> 3.037603, 2.903202, 2.84064…
## $ `Protein-energy malnutrition (%)`          <dbl> 0.3297599, 0.3221711, 0.323…
## $ `Drowning (%)`                             <dbl> 0.9838624, 0.9545860, 0.951…
## $ `Maternal deaths (%)`                      <dbl> 1.769213, 1.749264, 1.76424…
## $ `Parkinson disease (%)`                    <dbl> 0.02515859, 0.02545063, 0.0…
## $ `Alcohol disorders (%)`                    <dbl> 0.02899828, 0.02917152, 0.0…
## $ `Intestinal albertious diseases (%)`       <dbl> 0.1833303, 0.1781074, 0.176…
## $ `Drug disorders (%)`                       <dbl> 0.04120540, 0.04203340, 0.0…
## $ `Hepatitis (%)`                            <dbl> 0.1387378, 0.1350081, 0.134…
## $ `Fire (%)`                                 <dbl> 0.1741567, 0.1706712, 0.171…
## $ `Heat-related (hot and cold exposure) (%)` <dbl> 0.1378229, 0.1348266, 0.139…
## $ `Natural disasters (%)`                    <dbl> 0.00000000, 0.79760256, 0.3…
## $ `Conflict (%)`                             <dbl> 0.932, 2.044, 2.408, NA, 4.…
## $ `Terrorism (%)`                            <dbl> 0.007, 0.040, 0.027, NA, 0.…
# 检查数据的列名
colnames(df_input)
##  [1] "country"
##  [2] "country_code"
##  [3] "year"
##  [4] "Cardiovascular diseases (%)"
##  [5] "Cancers (%)"
##  [6] "Respiratory diseases (%)"
##  [7] "Diabetes (%)"
##  [8] "Dementia (%)"
##  [9] "Lower respiratory infections (%)"
## [10] "Neonatal deaths (%)"
## [11] "Diarrheal diseases (%)"
## [12] "Road accidents (%)"
## [13] "Liver disease (%)"
## [14] "Tuberculosis (%)"
## [15] "Kidney disease (%)"
## [16] "Digestive diseases (%)"
## [17] "HIV/AIDS (%)"
## [18] "Suicide (%)"
## [19] "Malaria (%)"
## [20] "Homicide (%)"
## [21] "Nutritional deficiencies (%)"
## [22] "Meningitis (%)"
## [23] "Protein-energy malalberton (%)"
## [24] "Drowning (%)"
## [25] "Maternal deaths (%)"
## [26] "Parkinson disease (%)"
## [27] "Alcohol disorders (%)"
## [28] "Intestinal infectious diseases (%)"
## [29] "Drug disorders (%)"
## [30] "Hepatitis (%)"
## [31] "Fire (%)"
## [32] "Heat-related (hot and cold exposure) (%)"
## [33] "Natural disasters (%)"
## [34] "Conflict (%)"
## [35] "Terrorism (%)"

5. 数据预处理

df_tidy = df_input %>%# pivot_longer() 从宽数据透视到长数据转换pivot_longer(names_to = "Cause", cols = contains("%"), values_to = "Percent") %>%# 建议使用 dplyr::mutate 形式调用函数, 不然容易与 plyr 中的函数冲突 (因为我自己就报错了...)dplyr::mutate(Percent = Percent/100,Cause = str_trim(str_to_title(sub(" [(]%[)]", "", Cause)))) # 删除缺失值的观测
df_tidy <- na.omit(df_tidy)# 简要查看数据内容
glimpse(df_tidy)
## Rows: 167,808
## Columns: 5
## $ country      <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan…
## $ country_code <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "…
## $ year         <dbl> 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 199…
## $ Cause        <chr> "Cardiovascular Diseases", "Cancers", "Respiratory Albert…
## $ Percent      <dbl> 0.1761039712, 0.0402597540, 0.0210662613, 0.0383255475, 0…

6. 利用 ggplot2 绘图

# 以中国地区的数据为例画图
# 从中挑选一些需要关注的点
Country = "China"
Diseases = c('Cardiovascular Diseases', 'Cancers', 'Diabetes', 'Suicide', 'Homicide', 'Neonatal Deaths', 'Tuberculosis')
line.plot = df_tidy %>% filter(Cause %in% Diseases, country == Country)
gg <- line.plot %>% ggplot(aes(year, Percent, col = Cause,text = paste("年份:", year, "\n致死因素: ", Cause, "\n百分比:", round(Percent, 4)*100, "%")))
# geom_line() 添加折线图
gg <- gg + geom_line(aes(lty = Cause, group = 1), lwd = 0.8)
gg <- gg + scale_x_continuous(breaks = seq(1990, 2018, by = 5), limits = c(1989, 2019))
gg <- gg + scale_y_continuous(labels = percent_format())
gg <- gg + labs(title = glue("死亡因素致死变化情况 中国 {Country}"),subtitle = "历年死亡变化情况",x ='年份',y = '百分比 (%)')
# theme_classic2() 经典白色背景, L坐标系主体
gg <- gg + theme_classic2()
# theme() 实现对非数据元素的调整, 对结果进行进一步渲染, 使之更加美观
gg <- gg + theme(# panel.grid.major 主网格线, 这一步表示删除主要网格线panel.grid.major = element_blank(),# panel.grid.minor 次网格线, 这一步表示删除次要网格线panel.grid.minor = element_blank(),# plot.title 主标题plot.title = element_text(hjust = 0.1, color = "black", size = 20, face = "bold"),# axis.text 坐标轴刻度文本axis.text = element_text(hjust = 0.1, size = 12),# plot.caption 说明文字plot.caption =  element_text(hjust = -0.85),# legend.direction 设置图例的方向legend.direction = "vertical", # legend.position 设置图例位置legend.position = "right")

7. 保存图片到 HTML

ggplotly(gg, tooltip = c("text"))


原图:20180416-E-01.html

filename = '20180416-E-01'
htmlwidgets::saveWidget(ggplotly(gg, tooltip = c("text")), paste0(filename, ".html"))

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  ggpubr_0.4.0
##  [5] gridExtra_2.3   plotly_4.10.0   maps_3.4.0      glue_1.6.2
##  [9] extrafont_0.18  tidytext_0.3.4  ggthemes_4.2.4  forcats_0.5.2
## [13] stringr_1.4.1   dplyr_1.0.10    purrr_0.3.4     readr_2.1.2
## [17] tidyr_1.2.1     tibble_3.1.8    ggplot2_3.3.6   tidyverse_1.3.2
## [21] ggbump_0.1.0    scales_1.2.1
##
## loaded via a namespace (and not attached):
##  [1] fs_1.5.2            lubridate_1.8.0     httr_1.4.4
##  [4] SnowballC_0.7.0     tools_4.2.1         backports_1.4.1
##  [7] bslib_0.4.0         utf8_1.2.2          R6_2.5.1
## [10] DBI_1.1.3           lazyeval_0.2.2      colorspace_2.0-3
## [13] withr_2.5.0         tidyselect_1.1.2    compiler_4.2.1
## [16] extrafontdb_1.0     cli_3.3.0           rvest_1.0.3
## [19] xml2_1.3.3          labeling_0.4.2      sass_0.4.2
## [22] digest_0.6.29       rmarkdown_2.16      pkgconfig_2.0.3
## [25] htmltools_0.5.3     dbplyr_2.2.1        fastmap_1.1.0
## [28] htmlwidgets_1.5.4   rlang_1.0.5         readxl_1.4.1
## [31] rstudioapi_0.14     farver_2.1.1        jquerylib_0.1.4
## [34] generics_0.1.3      jsonlite_1.8.0      crosstalk_1.2.0
## [37] tokenizers_0.2.3    car_3.1-0           googlesheets4_1.0.1
## [40] magrittr_2.0.3      Matrix_1.5-1        Rcpp_1.0.9
## [43] munsell_0.5.0       fansi_1.0.3         abind_1.4-5
## [46] lifecycle_1.0.1     stringi_1.7.8       yaml_2.3.5
## [49] carData_3.0-5       grid_4.2.1          crayon_1.5.1
## [52] lattice_0.20-45     haven_2.5.1         hms_1.1.2
## [55] knitr_1.40          pillar_1.8.1        ggsignif_0.6.3
## [58] reprex_2.0.2        evaluate_0.16       data.table_1.14.2
## [61] modelr_0.1.9        vctrs_0.4.1         tzdb_0.3.0
## [64] Rttf2pt1_1.3.10     cellranger_1.1.0    gtable_0.3.1
## [67] assertthat_0.2.1    cachem_1.0.6        xfun_0.32
## [70] broom_1.0.1         rstatix_0.7.0       janeaustenr_1.0.0
## [73] googledrive_2.0.0   viridisLite_0.4.1   gargle_1.2.1
## [76] ellipsis_0.3.2

测试数据

配套数据下载:global_mortality.xlsx

20180416-E · Global Mortality · ggplot2 plotly 动态折线图 · R 语言数据可视化 案例 源码相关推荐

  1. 20180416-A · Global Mortality · ggplot2 散点图 ggbump 凹凸图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 Global Mortality What do people die from? Tidy Tuesday 在 G ...

  2. 20180416-D · Global Mortality · gt 包用于生成表 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 Global Mortality What do people die from? 在过去的几个世纪里,世界发生了很 ...

  3. 20180402-A · US Tuition Costs · ggplot2, 折线图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 US Tuition Costs Average Tuition and Educational Attainmen ...

  4. 20180416-C · Global Mortality · ggplot2 马赛克图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 Global Mortality What do people die from? 在过去的几个世纪里,世界发生了很 ...

  5. 20180416-G · Global Mortality · ggplot2 maptools 地图 热力图 组合图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 Global Mortality What do people die from? 在过去的几个世纪里,世界发生了很 ...

  6. 20180416-B · Global Mortality · ggplot2 条形图 百分比图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 Global Mortality What do people die from? 在过去的几个世纪里,世界发生了很 ...

  7. 20180409-C · NFL Positional Salaries · ggplot2, 折线图 散点图 分面图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 NFL Positional Salaries NFL Positional Salaries Tidy Tuesd ...

  8. 20180430-A · ACS Census Data (2015) · ggplot2 maps rcstatebin 地图 热图 蜂窝地图 动态图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 ACS Census Data (2015) 欢迎来到`ggplot2`的世界! ggplot2是一个用来绘制统计图 ...

  9. 20180409-B · NFL Positional Salaries · ggplot2, 峦峰图, gganimate 动态图 · R 语言数据可视化 案例 源码

    所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 NFL Positional Salaries NFL Positional Salaries Tidy Tuesd ...

最新文章

  1. opencv补全边缘_为什么OpenCV中绘制的轮廓不能填充图像边缘的轮廓?
  2. @所有城市:想建AI智算中心的看这里!国家认可的那种
  3. java基本类型是类吗_Java基本数据类型和引用类型(一)
  4. It Smells!不要让两个状态纠缠的类同时成为某个事件的观察者(一切都只是视图)...
  5. Shiro集成Web时的Shiro JSP标签
  6. 数据解析,重中之重!
  7. 消息队列中点对点与发布订阅区别
  8. java vo转map_Jython:在 Java 程序里运行 Python 代码 4.5
  9. IISExpress Log 文件路径
  10. [设计模式]单例模式(懒汉式,饿汉式)
  11. 程序员面试金典 - 面试题 04.03. 特定深度节点链表(BFS)
  12. 【nodejs原理源码赏析(3)】欣赏手术级的原型链加工艺术
  13. “乘风破浪”的中国SaaS :风浪越大,机会越大?
  14. 关于修改android studio的gradle目录
  15. Python零基础学习系列之三--Python编辑器选择
  16. Java判断拼音的工具类
  17. 深度解析|积分墙防作弊,一直在发展
  18. P5900 无标号无根树计数 题解
  19. Little Keng
  20. SQLite3 模糊查询

热门文章

  1. Python--ndarray 常用属性详解
  2. pdf文件加水印怎么加,5个方法快速易学
  3. 计算机系张馨文,2020届魅力毕业生 | 【信息学部计算机与信息学院】张馨文——努力且热爱,做一朵乐观而坚韧的向日葵...
  4. 外汇占款激增究竟是谁惹的祸
  5. 【ASP.NET】ASP.NET框架概述
  6. 反悔贪心题-炼金模拟器
  7. 汇编指令sub也改变标志位
  8. 对集合、复杂度以及泛型的认识
  9. 几何向量:ScreenToViewportPoint/ScreenToWorldPoint函数解析
  10. 解决全局安装cli相关问题