• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      R语言

      • 首页
      • 博客
      • R语言
      • 数据发现:中国爱情故事

      数据发现:中国爱情故事

      • 发布者 weinfoadmin
      • 分类 R语言
      • 日期 2019年7月18日
      测试开头

      开篇

      他们离了。

      数据发现:中国爱情故事

      他们也离了。

      数据发现:中国爱情故事


      在当今这个物欲横流的社会里是否还存在爱情?

      本文通过统计局2009年到2017年各省份的结婚,离婚,人均年收入数据来看中国爱情故事的发展。

      注意:文中的离婚比率不等于离婚率。离婚比率高有可能是离婚的人多了也有可能是结婚的人少了。


      package

      library(tidyverse)
      library(dplyr)
      library(scales)
      library(ggplot2)
      library(readr)
      library(janitor)
      library(ggthemes)
      library(readxl)
      library(rayshader)
      library(sqldf)
      theme_set(theme_light())

      导入数据

      数据源: 中国统计局

      http://data.stats.gov.cn/
      https://www.kaggle.com/tduan007/china-marriage-and-divorce-data

      marriage001=read_csv('marriage.csv')
      divorce001=read_csv('divorce.csv')
      income001=read_excel('income.xls')

      数据处理

      使用tidyr中的gather将’宽数据’转换成’长数据’

      marriage002=marriage001%>%gather(year,marriage_couple,'2017':'2009')
      divorce002=divorce001%>%gather(year,divorce_couple,'2017':'2009')
      income002=income001%>%gather(year,annual_income,'2017':'2009')

      使用sqldf,将结婚,离婚,人均年收入数据结合起来

      data001=sqldf('select t1.*,t2.divorce_couple,t3.annual_income 
                    from marriage002 t1 
                    left join divorce002 t2 
                    on t1.area=t2.area and t1.year=t2.year
                    left join income002 t3 
                    on t1.area=t3.area and t1.year=t3.year')
      data001$year=as.integer(data001$year)
      data001$divorce_marriage_ratio=round(data001$divorce_couple/data001$marriage_couple,2)

      作图

      1. 2009年到2017年各省离婚比率与人均年收入的变化

      各省人均年收入每年快速上涨。人均年收入高的省份离婚比率也相对较高。

      西藏是个例外。人均年收入高且离婚比率低。或许真爱在西藏?

      gg_plot <- ggplot(data001, aes(x = year, y = annual_income,col=divorce_marriage_ratio)) +
        theme_minimal() +
        geom_point() +
        labs(y="人均年收入",
             x='',
             colour = "离婚比率",
             title = "2009到2017年各省离婚比率",
             subtitle = "与各省人均年收入的关系",
             caption ="统计时间:2017|数据源:中国统计局
             @Tony Duan"
      )+scale_x_continuous(breaks= pretty_breaks())+scale_y_continuous(labels = scales::comma,breaks= pretty_breaks())+
             scale_color_gradient(low="green", high="red")
      plot_gg(gg_plot, height=4, width=5,multicore=TRUE,windowsize = c(1200, 960),zoom = 1, phi = 35, theta = 30, sunangle = 225, soliddepth = -150)
      render_movie('abc.mp4')

      2. 2009年到2017年各省结婚人数和离婚人数的变化

      河南省,山东省,江苏省每年结婚人数最多。但到2013年后开始有逐年减少的趋势。

      四川省每年离婚人数最多。从2009年以来各省的离婚人数都在增加。

      library(ggplot2)
      library(gganimate)
      gg_plot <- ggplot(data001, aes(x = marriage_couple, y = divorce_couple)) +
        theme_minimal() +
        geom_point() +
        geom_text(aes(label = area), size=4,vjust = 1, hjust = 1, check_overlap = TRUE)+
          geom_text(aes(label = paste0(as.character(divorce_marriage_ratio*100),'%')), size=5,vjust = -1, hjust = 1, check_overlap = TRUE)+
         labs(x = "结婚人数(万对)",
             y = "离婚人数(万对)",
             title = "各省每年结婚人数与离婚人数",
             subtitle = "离婚比率=当年离婚人数除以结婚人数",
             caption ="统计时间:2009-2017|数据源:中国统计局
             @Tony Duan"
      )+ggtitle("{frame_time}") + transition_time(year) 
      animate(gg_plot)

      数据发现:中国爱情故事



      3. 2009年到2017年 各省份离婚比变化

      各省的离婚比率基本都在逐年上升。

      library(ggplot2)
      library(gganimate)
      data001=data001 %>% arrange()
      gg_plot <- ggplot(data001, aes(x = area, y = divorce_marriage_ratio,fill=area)) +
        theme_minimal() +
        geom_bar(stat="identity") +
        geom_text(aes(label = area), size=2.5,vjust = 0, hjust = 1)+
        geom_text(aes(label = paste0(as.character(divorce_marriage_ratio*100),'%')), size=3,vjust = 0, hjust = -1)+
        theme(legend.position="none",axis.title.y=element_blank(),axis.text.y=element_blank())+
        labs(y="离婚比率",
             title = "2017年各省离婚比率",
             subtitle = "2009到2017年各省离婚比率",
             caption ="统计时间:2017|数据源:中国统计局
             @Tony Duan"
      )+ coord_flip()+
        ggtitle("{frame_time}") +transition_time(year) 

      gg_plot

      4. 2009年到2017年各省离婚比率的变化

      2017年离婚比率最高的3个省份与最低的3个省份的历史离婚比率趋势。

      library(ggplot2)
      library(gganimate)
      data002=data001 %>% filter(year==2017) %>% arrange(divorce_marriage_ratio)
      data003=rbind(head(data002,3),tail(data002,3))
      data004=data001 %>% filter(area %in% data003$area)
      data004=data004 %>% arrange(year,area)
      gg_plot =data004 %>% ggplot(aes(x = year, divorce_marriage_ratio ,col=area)) +
        theme_minimal() +
        geom_line(size=3)+
        labs(y="离婚比率",
            colour = "省份",
             title = "最高和最低省份离婚比率",
             subtitle = "离婚比率=当年离婚人数除以结婚人数",
             caption ="统计时间:2009-2017|数据源:中国统计局
             @Tony Duan"
      )+ scale_x_continuous(breaks=seq(2009,2017,1))
      gg_plot+ geom_point(aes(group = seq_along(year)))+transition_reveal(year)
      数据发现:中国爱情故事

      5.结论

      尽管各省份的离婚人数越来越多。结婚人数越来越少。但我们仍然要相信爱情。

      做到马列心中留,资本穿耳过。诱惑终不移,真爱恒不变。

      如果您喜欢本文。欢迎转发。作者微信ID:veryfly888

      Reference

      https://www.kaggle.com/tduan007/china-marriage-and-divorce-data

      https://github.com/tylermorganwall/rayshader

      https://tduan.netlify.com/post/china-divorce-ratio/

      公众号推荐:

      1 ChiefdataLab

      珠海市岭南大数据研究院(现在诚聘各类数据和AI人才,有意向者,请添加我的微信:luqin360,大家一起来做些有意思的项目和产品)是一家以高端人才引进培养、前沿技术研究及产业孵化为宗旨的新型研究机构。研究院更多了解,请识别下面二维码。

      数据发现:中国爱情故事

      2 数据人才

      数据人才助手,一个帮助数据人才找工作,帮助数据公司招人才的公众号,也分享数据人学习和生活的有趣事情。

      数据发现:中国爱情故事

      测试结尾

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      在火山图上标记基因(方法一)
      2019年7月18日

      下一篇文章

      小白学数据可视化:基于R生态的数据可视化工作环境构建
      2019年7月19日

      你可能也喜欢

      3-1665801675
      R语言学习:重读《R数据科学(中文版)》书籍
      28 9月, 2022
      6-1652833487
      经典铁死亡,再出新思路
      16 5月, 2022
      1-1651501980
      R语言学习:阅读《R For Everyone 》(第二版)
      1 5月, 2022

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?