QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4269|回复: 1
打印 上一主题 下一主题

R语言gganimate疫情数据动态可视化

[复制链接]
字体大小: 正常 放大

1178

主题

15

听众

1万

积分

  • TA的每日心情
    开心
    2023-7-31 10:17
  • 签到天数: 198 天

    [LV.7]常住居民III

    自我介绍
    数学中国浅夏
    跳转到指定楼层
    1#
    发表于 2021-10-28 19:38 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    R语言gganimate学习8 {% t) O8 a  J5 J/ s
    所需加载包# x, o0 j7 L% m0 ^, f1 P9 o
    library(av)
    ! k7 F6 x8 R8 V" e4 dlibrary(ggplot2), w+ ]' d$ R) X0 [; `0 l* C
    library(gganimate)2 B* V! D) p# t. Y3 I3 G, Y
    library(tidyverse)
    9 x3 S4 i3 p$ B7 z4 ?' I8 {library(lubridate)2 \  z  {0 `# |! M& i# `
    library(scales)
    & @6 F7 o4 B% glibrary(ggrepel)
      c9 T! I; B5 ^  ]/ |6 m3 a9 dlibrary(cowplot); b' r0 E. B+ S' Q# h& [
    数据. e$ l: P5 C- R0 A+ C$ b1 S
    * r6 }4 m2 W  o' ]

    # {$ f% b# ?+ n: nps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +7 |# `( S; U0 z' _
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    7 `' b- X$ h5 _0 g% y/ k    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    ) o" c( u% y0 J    #theme_bw()+% S  e( d4 y; ?3 N
       #theme(legend.position="none") +2 H1 ?. l6 F8 h& l0 S) B  k
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +$ F# `% I' x6 [, j! {# Y" @
        theme(panel.background=element_rect(fill='transparent'))+
    ; p% V# D/ G6 |- b& l+ U7 R  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+4 ?" a% V( K% F9 k/ C, D% E4 V
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+; m8 t7 I- V; C+ t0 y* u5 Q2 k
       theme(panel.grid =element_blank()) +   ## 删去网格线
    ; u$ q) x# u, L3 b# D5 ?& `% Q, a  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    * C8 H( X  _% j: M8 M5 X1 L  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    5 B# b6 q( g. L& k3 \% ^# Here comes the gganimate specific bits
    # Y4 z8 ~# [3 F; T3 S" b#labs(title = '日期:', x = '省份', y = '累计确诊病例') +6 A  t8 N+ _0 }7 I) E* d/ [
    #annotate("text",x=0,y=40,label=C,parse=T)+
      ^2 ^# r8 |9 T+ O4 [) Xcoord_flip()+% W( B4 g$ V  E) w
    transition_manual(frames=as.Date(Date)) +2 {3 _$ v+ I! E; f
    #ggdark::dark_theme_bw() + #设置黑色主题# P' K3 Z% v6 a/ X4 [
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    # R& H8 \9 i. B1 aease_aes('linear')
    5 ^! R3 V9 G( [2 k5 m1 g. X, h4 `- _ps# E/ O7 \! m% m( d
    结果展示
    6 h* W! H7 i+ m6 d) f( R
    4 S0 [$ U( T" b. P0 y; {7 I2 _2 s7 n' a* O! P8 V) ^7 @
    视频格式转化,加载BGM; i& b( U$ K; |' ~5 h
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
      }* t  `& [" ~5 c  X0 m1 v         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作, V+ [1 f5 ^  t% Y# V
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    4 {& A8 y8 W" p2 v( J# H9 j/ ~全国新冠状肺炎26天增长状况
    - {: m( h8 S* ~0 @6 }& V
    ) g4 k1 }. |" O) P# E7 U, O* E& ipc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+. m6 W* a/ ~/ W9 m( l
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    . X& i3 @$ b  W' P% e* S8 S1 b   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    / C" I8 n- W5 o6 Q0 g    theme(legend.position="none") +% V, p. _  K, l/ b' ?. h
      theme(panel.background=element_rect(fill='transparent'))+8 @. Q% `1 o* K3 y* z, {4 ~
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+7 I) c% ]0 F/ H
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
    * D' s0 g8 K) O' ~/ _# V7 W   theme(panel.grid =element_blank()) +   ## 删去网格线2 {" \( d9 L$ o
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签+ J$ u( ?1 U: O) K- }$ q$ _$ Z
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    4 U- f# T: a2 R; i: Y3 L8 @  #scale_x_continuous(limits = c(0,6))+" ^- b  F" ]+ u' m* _  O
    # Here comes the gganimate specific bits
    8 p# r, D# U( ^/ V: w: y- e) ]- M#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
      E" {# N3 E, L; k# V#annotate("text",x=0,y=40,label=C,parse=T)+
    6 r* F- e) J& c8 H7 G+ V5 K9 t    # coord_flip()+: p- o$ U$ s& N' g4 u& h
         transition_manual(frames=as.Date(Date)) +7 |5 S; _3 l5 U' Q9 ]; b- y' `  x
        # ggdark::dark_theme_bw() + #设置黑色主题% C; A' T. u* a: T9 B; y  Y
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    # O' f6 x5 H# T, I# y     ease_aes('linear'); \$ j1 n+ e: x, D4 Q
    pc2 w3 J  m- S( H4 S5 `" p

    : a# q: t+ _: _+ r3 w3 o7 R$ K$ b' y/ }3 I9 b
    动态图合并
    . C8 ~+ [) |* J, F  Ilibrary(magick), B7 x+ f4 a# z2 [5 ?) J
    ps_gif <- animate(ps,,width = 720, height = 480)' `- x9 n. }; V' a" ^% l
    pc_gif <- animate(pc, width = 360, height = 480)3 n* G9 Z& P) [3 |5 ?
    ps_gifs <- image_read(ps_gif)5 ~: V( F6 V& a8 ~& `
    pc_gifs <- image_read(pc_gif)
    - K. F4 a, m+ Y8 D* F7 Onew_gif <- image_append(c(pc_gifs[1], ps_gifs[1])); i2 j9 y3 m! h  t/ p" _
    for(i in 2:length(pc_gifs)){$ d. J2 N) t8 g2 n, b4 M6 S
      combined <- image_append(c(pc_gifs, ps_gifs))2 n/ d2 a& u/ A  F
      new_gif <- c(new_gif, combined)
    8 q- l. B' W, A+ a}1 m" }. m2 s/ S) s2 @' H
    new_gif, I3 w& j$ x* K
    结果展示
    . ?4 k1 Z) _0 \1 Q: B6 ?4 H
    : v  `, v  T, m8 O; g6 o) P( K5 L- H& ~

    4 A5 O- f' g, x# ?$ w
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    sjlxdn        

    1

    主题

    2

    听众

    155

    积分

    升级  27.5%

  • TA的每日心情
    无聊
    2022-2-19 17:40
  • 签到天数: 30 天

    [LV.5]常住居民I

    国际赛参赛者

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-10-25 04:09 , Processed in 0.352319 second(s), 56 queries .

    回顶部