QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3986|回复: 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 d: }: h# B6 \! J5 Q) h. T$ o所需加载包
      y9 v* f$ G1 G8 N  l. glibrary(av)
    1 L- \# U3 f/ P* ]+ F% H7 F7 Wlibrary(ggplot2)
    3 a/ ~# t, a) L5 M$ ~% n/ ^5 R$ ]library(gganimate)
    / d  {! D$ i5 N0 x1 u# h8 Z4 w3 Ulibrary(tidyverse)
    1 J) O% U2 p9 dlibrary(lubridate)
    + E4 y9 t0 ?3 `$ a: Z, Slibrary(scales)
    + Y( {6 L9 ]; P. N3 Klibrary(ggrepel)
    0 `: ~% G: Q* W. flibrary(cowplot)5 e* q  b: o, [+ P9 e8 r
    数据: Z% N- h- s3 x" }$ _5 ~8 y

    6 ~8 }. N4 C4 {/ `+ D( e
    $ q9 y& I) ^7 R8 Y# }ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +  Y% D* a+ L/ h9 S" @
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +( `4 R  d* M1 S6 ^: f) h  ^! m
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    0 j7 L9 W' x# q2 }8 z0 d    #theme_bw()+6 j$ h# s( m# N8 N2 q
       #theme(legend.position="none") +$ q9 a/ B4 R% |6 k! n
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +% l) D3 R4 F# z( E. M% Y6 ^# ~: P
        theme(panel.background=element_rect(fill='transparent'))+
    " a; l$ g; s$ ]" p' _# [2 Q5 I  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+2 G5 G& \1 f2 B5 q
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    1 ?& _& x8 i) r5 `   theme(panel.grid =element_blank()) +   ## 删去网格线
    : B2 W2 O- \( c& q$ {2 V  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
      D" B2 f" f  p( c5 o  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线; b3 Z, t- y4 S4 y" T
    # Here comes the gganimate specific bits5 z7 O+ C+ A4 y3 s3 ~% m! x
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    ( w0 b' I3 k% ^" _; c9 E#annotate("text",x=0,y=40,label=C,parse=T)+
    5 R# z- V! S/ X! ~coord_flip()+! J( g( k) u8 m  l# H9 F9 m" U. C
    transition_manual(frames=as.Date(Date)) +8 D% g# W" G) S4 b5 l4 z
    #ggdark::dark_theme_bw() + #设置黑色主题5 x$ g* P- o5 p
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+& K1 \' n, i1 d6 Z1 W
    ease_aes('linear')  C; W8 ^6 ?$ W+ t4 J& j( ]  j
    ps6 K+ u7 u5 ]( L0 O9 k% @
    结果展示2 U% u1 z. d' s% @/ r- J! b7 {( d+ Y  B

    ( G# z" n4 u9 A( \" Z- c. C: P
    视频格式转化,加载BGM+ V0 `- `! U' S+ m7 M
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), & A6 D$ ^$ K, i7 n/ T2 O
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    3 i( |; r9 r6 m! |4 C# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3"), @- F9 y: Y# t: K& Z
    全国新冠状肺炎26天增长状况
    6 @6 B7 `; ^/ D; ]8 c! _( r' d" S8 e+ t' X
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    5 w" @7 t1 z$ F. o) D. ~; D   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +# ^5 L, L2 A' u* O7 {6 ~5 j% r
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    ) g/ l5 k1 T% \( \0 \) ]9 |    theme(legend.position="none") +
    , P! C) g5 o9 G. R( B  theme(panel.background=element_rect(fill='transparent'))+: i) D# `! L+ Q
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    , n0 H% o  P9 F4 |6 T) w  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+, `" e) s5 s" f0 S8 l5 _9 F2 z
       theme(panel.grid =element_blank()) +   ## 删去网格线
    - E4 v/ i9 y0 U- i. N) y  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    9 G* g& A: h2 i0 P  S! y  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    # P( M& F; u# d3 Q) z/ p4 g  #scale_x_continuous(limits = c(0,6))+4 X+ m; r% y; P+ n3 o, a4 [4 I
    # Here comes the gganimate specific bits
    . c: ~9 P7 M1 r#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    ' ?4 ?0 i$ z0 C1 L( @7 y#annotate("text",x=0,y=40,label=C,parse=T)+
    / F! ~' l+ n7 R3 z& u9 j- \    # coord_flip()+
    5 o  g% G. w: o- c& L/ i' O     transition_manual(frames=as.Date(Date)) +1 c. K9 P3 @8 q
        # ggdark::dark_theme_bw() + #设置黑色主题: [) a1 r* j# d) i5 r7 F
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+9 i5 w. L. q" W0 t
         ease_aes('linear')
    6 C9 e9 ^5 h; u" p2 cpc, |' C0 v4 k1 b0 s8 P3 W( z1 @0 T

    ( w& U, D4 w0 f  F' o* R/ ~3 K1 ~
    & k; Y9 F+ ]& \7 B8 l& ]* q动态图合并
    , }5 e4 y& _5 V- \4 vlibrary(magick)
    % D: l# e2 m1 f# r1 U. V* ~ps_gif <- animate(ps,,width = 720, height = 480)0 j1 x+ ]1 J. @  J+ W6 ^4 D/ t; d
    pc_gif <- animate(pc, width = 360, height = 480)$ f; W/ \9 _  v# _* r% s+ z. I
    ps_gifs <- image_read(ps_gif)
    " v, ~: @1 A% C2 X$ @pc_gifs <- image_read(pc_gif)
    4 u7 z+ H( o( e# L2 bnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))1 q# C2 _8 V+ A/ p2 @9 ^
    for(i in 2:length(pc_gifs)){
    : }* n7 Z( V. v  Z, x  combined <- image_append(c(pc_gifs, ps_gifs)). x5 K! d/ s7 H' n2 ]- j  u% z
      new_gif <- c(new_gif, combined)
    6 r4 Z4 E  {9 {6 n5 i9 @}
      T* |( Q( B/ e: L3 Wnew_gif" w9 ]% }# d4 g$ z5 k1 S: b
    结果展示
    3 c4 R+ X% p3 K+ C% E: L) n8 c" h0 V% H) f" q% F

    , W" h1 W' `: `! u- ^8 X6 t2 n! l6 |1 t8 V
    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 01:13 , Processed in 0.421742 second(s), 56 queries .

    回顶部