QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3996|回复: 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学习
    ' p$ W0 m2 Y! t, F, Q所需加载包
    . S7 O1 \! X, H3 Q0 l1 r" ~0 m- }library(av)
    ' E+ n1 v  K# z/ ]& wlibrary(ggplot2)
    # p% ~7 n8 t/ Q7 }' Xlibrary(gganimate)1 b, u& w& p$ q" _! {# ?) [+ W/ A
    library(tidyverse)
    2 H$ E+ f: Y: Q. {- i2 ?library(lubridate)
    5 T" v: n/ @) `+ ~+ mlibrary(scales)5 i5 L: d$ `- b0 t6 `0 b& N
    library(ggrepel)6 K# ?( M9 X) s: ^4 q1 o+ A
    library(cowplot)5 d4 F0 ^: @. h# {! _0 P/ i
    数据
    8 f; C( K4 `2 E& h9 M  k6 o4 _; B5 j2 m8 |. B
    2 Y* [4 T$ c3 `
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +# ^0 G9 g% k8 E  E8 L
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    9 F  v$ `- t1 ?, j5 y, m" U* B4 Z    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    ( }+ ~$ \' G4 r/ E/ x    #theme_bw()+8 H- \) P$ R; L% A
       #theme(legend.position="none") +
      N) ~/ Y+ K2 @; F" e. b/ C5 ~    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    $ G  B6 `4 u# ~4 j/ r    theme(panel.background=element_rect(fill='transparent'))+
    : v8 n& F# B4 s4 g4 ^" n" W& `0 d* p4 w  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    . Y) N0 r+ k9 D& r( h! a; F  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    3 f8 l7 [6 y. r% n4 G" x" g8 z   theme(panel.grid =element_blank()) +   ## 删去网格线
    5 m. o+ k. F) i  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    % u& T/ N) E7 _; I% O. W6 c  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线) z( b& ?" E7 F4 {7 s7 B' J
    # Here comes the gganimate specific bits
      x- _$ p+ R7 V6 }1 M% L#labs(title = '日期:', x = '省份', y = '累计确诊病例') +' C: J. {( V  V5 a$ ?
    #annotate("text",x=0,y=40,label=C,parse=T)+5 W# }1 Y7 [/ }- F3 V% H$ M: I% ~! a4 q
    coord_flip()+/ N1 C! Z2 O$ p0 z0 [5 u
    transition_manual(frames=as.Date(Date)) +
    * }4 H* z: I- |7 g6 \1 [. ~( C2 o#ggdark::dark_theme_bw() + #设置黑色主题
    5 Q: m3 q8 i' V! T' Q; H6 llabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+& ^) Z# c8 R1 l, u6 z7 ?/ @% J
    ease_aes('linear')
    ! `) w1 }2 v9 n) b: J7 Cps
    : y0 C. e' O# T9 D, [6 Z结果展示2 Q6 M: S( e9 C# b, u

    - [) ^9 F* I% h' K
    , }4 F8 O& I* ]2 T( S6 Z; P  a视频格式转化,加载BGM$ \+ E* \' Y! k; }
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    ! A; ]& `2 w* B) a& c         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    : v& W0 S* I( s: P- G# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")" ~/ r, }7 G# a. N  J2 O$ r
    全国新冠状肺炎26天增长状况" Q, \% o3 F# X# q+ D1 n
    3 i$ @9 {& Z4 ^; ?1 E9 |  ^$ |
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+  j- B  T6 C' l6 A3 a) l* n# m
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +3 t) u2 q! W7 N3 F8 I* q
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    9 B- T. z4 o, W5 E/ v    theme(legend.position="none") +
    3 Q! G2 B& z. E+ F6 a, B2 [  theme(panel.background=element_rect(fill='transparent'))+
    ! K4 t$ q) l8 A$ u- a  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    , y7 V& Z; _: _; O( }* l  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
    # G# q7 e$ T6 o& Q* u: q: c   theme(panel.grid =element_blank()) +   ## 删去网格线
    ' H. C- ?. L. c5 d" j  [, H8 b2 a" n  theme(axis.text = element_blank()) +   ## 删去所有刻度标签  q) e$ p5 L7 K9 }9 S, F( J
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线$ \2 ^7 p' d7 T' ~6 e! B8 t
      #scale_x_continuous(limits = c(0,6))+
    6 W5 y8 h& C& w/ @/ {) i, Y # Here comes the gganimate specific bits
    : }0 K% Y: }3 D& k#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    % ?8 k0 D. V; J1 \7 q7 c#annotate("text",x=0,y=40,label=C,parse=T)+
    ) w# t2 d4 u- V4 `- K: J    # coord_flip()+- c7 q% V* U7 ]! r# ?
         transition_manual(frames=as.Date(Date)) +5 s& a  T& H5 I  e9 P
        # ggdark::dark_theme_bw() + #设置黑色主题) W9 B. C  \+ ^* c
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+& S  y# L9 Y1 r7 X
         ease_aes('linear')6 b! t& t7 p% G/ j! e
    pc
    % K" \  k! C) {: `1 w0 j. `* A7 m4 q# O' y
    9 R; Z9 h8 ^$ @$ q9 K' n" B1 }/ M
    动态图合并
    . ^* D- Q* k. u3 W) R* i  W7 b5 a9 Klibrary(magick)
    % Q! |2 P/ k4 r: ?ps_gif <- animate(ps,,width = 720, height = 480)  q2 i' P- x( |9 ^
    pc_gif <- animate(pc, width = 360, height = 480)  y  b* z( x$ Q' k
    ps_gifs <- image_read(ps_gif)
    , r+ T! g/ C0 c2 G) }5 ipc_gifs <- image_read(pc_gif)
    3 C, g! Z) J7 Q1 K' Unew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))% l1 I7 l' b$ s* ~+ Z9 o5 E6 k
    for(i in 2:length(pc_gifs)){
    : M, ?& [9 N- T% H& j& P8 D  combined <- image_append(c(pc_gifs, ps_gifs))# o0 t" O1 `# B' }: ^
      new_gif <- c(new_gif, combined)4 C8 v1 n0 y: r) d6 k- @6 T
    }& K; e/ Z! N; h6 }) M- L2 ^* O
    new_gif
    9 J2 D8 s. b; L  e6 s) M9 }+ a结果展示
    * ^# H+ }3 \" h- _% W  y. S
    & m; `/ P3 Y0 [4 m' K; A# _
    ( j9 N; H6 A  N( w# L
    # d. l% y. I4 d* B
    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-21 18:50 , Processed in 0.738931 second(s), 55 queries .

    回顶部