QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3992|回复: 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学习
    5 Q) c! W* q$ r所需加载包6 U' R; F5 l, O
    library(av)% }/ g2 i: |0 W- }" F# I. P& U
    library(ggplot2)
    , R5 z4 i* s) k- J# ^' D5 qlibrary(gganimate)
    , t. l3 y# \5 R; l$ s  n- x0 l# ]library(tidyverse)7 T: X+ p. L; F' x% }; _
    library(lubridate)' @# j( A6 y% e) e6 A3 ~* B/ K6 B
    library(scales): Y( F) c2 ~$ A" P; h4 m
    library(ggrepel)+ X' d6 ]( J2 I" c! d
    library(cowplot)7 r! p. X8 e" |/ M) g6 g/ ]
    数据( n: q/ P+ E' ~1 _+ r: w2 R5 x+ Y" Z+ L

    ' m9 Q1 N6 C5 i, [, R% O, I
    6 N5 G+ \* f. @+ G: Jps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    $ z2 C6 ^( f1 V  B# U% c; N# v     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    0 z1 t- A- t7 D( H! _' w( s    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    - f1 s5 k$ [4 e* Y3 y7 K    #theme_bw()+; z* S6 h# P4 [5 y# x
       #theme(legend.position="none") +
    9 B( h2 K( P+ J- n    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +, a% E2 k! t+ _* t; y% u( u  X! W' ~
        theme(panel.background=element_rect(fill='transparent'))+4 q) w, k0 s( w7 p
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    2 U. X1 C9 r. v* [) v$ K  B  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    8 ], k; v# A" S" S   theme(panel.grid =element_blank()) +   ## 删去网格线
    & N/ ?+ H) ?0 V+ v- ~# ?* G" m  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    $ k" H3 _! o  ~0 v  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线$ z( _$ a& `7 q, N2 Q$ |* X
    # Here comes the gganimate specific bits
    - ~! Y* X; \1 o#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    ; A' c) x5 N; m2 v& ^: p1 ?2 n#annotate("text",x=0,y=40,label=C,parse=T)+
    5 p6 B9 b  K6 U, \! N* rcoord_flip()+9 H$ q; s, `  Z- U4 e$ K
    transition_manual(frames=as.Date(Date)) +
    + T0 y  C* n$ q8 }4 W#ggdark::dark_theme_bw() + #设置黑色主题
    2 c# Z6 T, ]% ~8 M6 R0 Wlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    3 V: k5 y7 X% O' Y' f6 L: e( ?0 xease_aes('linear')4 g7 Z% j7 ^9 k' [4 B- v
    ps' [! m- _: B9 x
    结果展示
    " o0 k4 X1 K( s* V  k
    # C) F3 ?4 [7 ~/ D+ B% A/ n6 f- W% d9 M; J9 S2 ~4 q* L. B0 n+ c
    视频格式转化,加载BGM6 x" s8 `  q! S& D4 q+ ~! a" w- J
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    + ]2 z# t8 ^9 X3 _% H         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作5 n1 V* k# L" u- _: v7 {
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    6 ~9 d' y+ }3 l: L- v+ N' O# R全国新冠状肺炎26天增长状况
    ) U/ [$ A) K# ^1 _5 _, O0 X* Q& A
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+% [1 n5 H0 \! C: q9 g& X
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +8 r8 O& x% R/ ^' K+ t" s7 j) @
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    . u: ^& S( m1 f* W    theme(legend.position="none") +
    7 t& _. p6 n! n# Q6 R; G( Z  theme(panel.background=element_rect(fill='transparent'))+9 p, F7 U- ]. f# K) y, B
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    ) F8 M: }7 w+ m  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+" {# }  u4 @  T3 M. C; K3 Q
       theme(panel.grid =element_blank()) +   ## 删去网格线
    * I$ o! w1 m1 F' H" ]  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    , e- N# K3 c1 K; J; ]$ |  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线2 ?2 X# C8 A! j% r1 H$ z
      #scale_x_continuous(limits = c(0,6))+
    ' P+ n8 ]) a6 l  _ # Here comes the gganimate specific bits
    7 T8 H- _5 T9 s6 x2 m#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    , I  J( p2 _2 h( l3 Z#annotate("text",x=0,y=40,label=C,parse=T)+
    8 x3 o3 \- k* @8 z' U# \( q9 q    # coord_flip()+
    * C! v* ~& G# X% J+ a     transition_manual(frames=as.Date(Date)) +. p2 h& O( W& p: ?) r
        # ggdark::dark_theme_bw() + #设置黑色主题9 X  _2 B  D" i# u- s
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+: J3 M: W- `6 L0 c
         ease_aes('linear')( e- R9 W6 U# v  \* Z5 F+ X
    pc! ?' Y  d7 T1 r2 A; S7 [

    3 A# X; D9 O- j: t- X7 W: u) w% N: @, I
    动态图合并- b( Q: c- t4 |% g' \
    library(magick)4 A# P% F" `9 I5 Q, d# q
    ps_gif <- animate(ps,,width = 720, height = 480)
    ' P  f# c# y6 L. r8 k. r$ fpc_gif <- animate(pc, width = 360, height = 480)0 v6 w5 W* q* e/ D. @
    ps_gifs <- image_read(ps_gif)
    8 L) \: _  a, d4 T+ V9 R+ q' Xpc_gifs <- image_read(pc_gif)
    6 Y: E' [. d4 }5 a: C/ L/ Dnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    . A+ P! w. Z6 p& q6 ifor(i in 2:length(pc_gifs)){
    , B, k- R& w" k6 h  combined <- image_append(c(pc_gifs, ps_gifs))
    ; K, x2 Q2 V9 d& I6 Q% w) c. G. H4 E  new_gif <- c(new_gif, combined)5 W+ f2 _7 [( s/ b# h
    }
    + Y" u3 y# K7 {2 U' B  wnew_gif
    % ^' K- p) L% s# |结果展示! b/ Q  L# [: F% w

    " w4 m$ {& `/ Y  e* ~6 e: q: C
      W8 u/ |, H# Z' q' _* L' B) r- ?  P1 r& G. l6 z
    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-4-20 21:15 , Processed in 0.428876 second(s), 56 queries .

    回顶部