QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3775|回复: 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学习
    4 E( V4 C% m( n1 ?所需加载包
    # @' z! G. K  W9 S: r; R2 Llibrary(av)
    5 `! ^8 R2 N+ D% ~: Clibrary(ggplot2)/ L) S6 W1 ^4 E: @
    library(gganimate)5 y, k' r/ X* M+ ]. z) ?) \1 S
    library(tidyverse)9 e# q: U: Y6 s/ `& R! m" H: {
    library(lubridate)& q. h% m- w: R3 n9 Y
    library(scales)0 c$ Z. n3 H% \  ?! Q7 z/ m
    library(ggrepel)
    & z+ h# ?4 u; a& k. Nlibrary(cowplot)
    2 o9 A& S/ ~  A! i9 T数据: x, _- J/ ]: d1 E1 {, d
    2 C8 R  N# o2 c
    3 w6 c2 w! U" |; M: R
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    4 x. k# b" W, `     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    ) l  v+ i' K9 N' n  n% ]2 R( }+ e% U    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    : m6 N* E, w9 B7 Z    #theme_bw()++ ?! K6 h* m2 Q  }
       #theme(legend.position="none") +
    2 `9 F0 C/ ~3 `0 a! R, Z1 R    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    4 S$ a' L8 ?3 V4 [# q( r    theme(panel.background=element_rect(fill='transparent'))+
    4 W/ H: x" ?# A3 r9 C# @# s  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
      R  H6 _/ \" u% a, f# y  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    : M# s) ]/ K5 V6 K  B  c   theme(panel.grid =element_blank()) +   ## 删去网格线: D/ S4 r+ d0 u* ~' N* ~5 t
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    . h, s/ G2 K5 c& r  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线) k' ]' ]. \& I$ N! ~
    # Here comes the gganimate specific bits% S' D* |5 {; ~$ \  a
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +2 x8 y- s: W/ E1 X, l" Y
    #annotate("text",x=0,y=40,label=C,parse=T)+) @! B: Q7 _2 K5 B
    coord_flip()+
    + |2 D" o+ Y. L* b6 @/ {# ktransition_manual(frames=as.Date(Date)) +
    * J7 V" d& ~) Y$ C7 Y7 `#ggdark::dark_theme_bw() + #设置黑色主题
    $ p; E) Y* Y1 ~2 |4 U7 g, {labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    * t+ P, Y2 E" g+ Q% I6 R0 V7 ~; u. qease_aes('linear')% `8 ^. b3 n& o; ]
    ps% d: ?  [: O: U! u
    结果展示
    " Z: z0 F7 q9 ~' x0 F
    . g8 Z: S  X1 G2 w  ~2 n+ o, M$ b' z( N7 v
    视频格式转化,加载BGM' ?% n& e# G6 `8 }
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    * g8 r& f1 Q. f( Z4 X- R4 f) [; W         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作! N! `& x5 V0 R, l9 _) O
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    " D4 O1 q- ^6 d6 f- {& {全国新冠状肺炎26天增长状况2 g# s9 e" v$ L( {# }* I% y

    3 F9 H; |; z( Q" U  @0 G/ Hpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    ( ]" H% M# d  f' d6 ~   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    8 G4 p, d7 ^: N, _1 [: f   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    ' ~4 K7 R1 ]' d& B! f    theme(legend.position="none") +
    3 k, V8 \; l6 v  theme(panel.background=element_rect(fill='transparent'))+/ p$ G# O5 @) k0 [1 C
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+& l6 J; c& u2 H$ t& j6 p! D1 z
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+* {4 V2 @+ F- O. V; c
       theme(panel.grid =element_blank()) +   ## 删去网格线
    ; L$ N# n( [+ {  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    ) ~9 M; \) l# S/ B$ p  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    ( [1 \2 g9 A$ p( r  #scale_x_continuous(limits = c(0,6))+
    4 q" |7 U6 R) Q! }' f8 l # Here comes the gganimate specific bits
    % z& @0 f6 H2 G. C; G9 k3 t* J#labs(title = '日期:', x = '省份', y = '累计确诊病例') +" q( q6 _, N7 y" _2 _
    #annotate("text",x=0,y=40,label=C,parse=T)+( T) Z; D* ?( l5 @* t/ Q# b
        # coord_flip()+
    # r1 Y3 \+ M0 F/ p- B+ @) V" T$ c     transition_manual(frames=as.Date(Date)) +
    ; |$ e) H. b, i7 c    # ggdark::dark_theme_bw() + #设置黑色主题" S0 Z* _* H, W5 r: @5 ?
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    & \7 P0 t2 `$ \9 E. S2 s: y0 ^     ease_aes('linear')* ]5 v( F9 p! ^/ `/ o3 {
    pc
    9 w! b! i* h( ~: N0 u9 m5 Q( l; }3 g8 h* [; y

    ; D5 t8 J& j" G! n/ w% d动态图合并& b5 z; d- T  H2 Q. `
    library(magick)
    6 h9 J1 s; P) Q' A& [ps_gif <- animate(ps,,width = 720, height = 480)
    , i! ~& _' O3 L, I1 H( ?$ \. N8 npc_gif <- animate(pc, width = 360, height = 480)+ O& {' u4 k9 B$ F5 e3 k
    ps_gifs <- image_read(ps_gif)
    9 m; r( \( p3 a; q, [1 \* Opc_gifs <- image_read(pc_gif)) }( k( d7 A( A: J% I
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))' A: j0 T" n7 g8 W
    for(i in 2:length(pc_gifs)){2 G' _: s1 h1 T2 }
      combined <- image_append(c(pc_gifs, ps_gifs))$ Q! J9 G4 H0 w/ ?$ f! }$ c8 O
      new_gif <- c(new_gif, combined)4 W; A0 g! Y) z8 E3 T
    }* G, L; ^& f1 x% J: @, D
    new_gif
    4 ?# x1 y3 j. f3 d  k结果展示7 E4 P: c0 v' D3 M$ h+ d+ j) m1 Q" L
    6 K8 d3 p' E# M( S9 ?3 Z

    / U) f; h' ?9 c. v* ]8 Z  q' B; K# F) {  _) H  p, z; D- 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, 2025-9-29 06:28 , Processed in 0.472954 second(s), 56 queries .

    回顶部