QQ登录

只需要一步,快速开始

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

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

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

1158

主题

15

听众

1万

积分

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

    [LV.7]常住居民III

    自我介绍
    数学中国浅夏
    跳转到指定楼层
    1#
    发表于 2021-10-28 19:38 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    R语言gganimate学习& T: [2 G+ C/ t
    所需加载包4 L/ n3 N5 V6 d# }: ?
    library(av)7 e9 u" ?' v% `& d: ^
    library(ggplot2); j0 H6 W4 y3 k1 w6 T' j
    library(gganimate)
    ( q" V! A6 F& olibrary(tidyverse)+ i! b* u/ I7 H0 t1 [6 P! ^) s
    library(lubridate)
    8 S5 W: u, B* N5 V* hlibrary(scales)
    ; F$ M/ Y/ d2 \& klibrary(ggrepel)' L& ~4 A* S8 w- l; P5 \  b! ~  l
    library(cowplot)
    # ?, S: A8 j1 i# F数据
    7 o" d3 y8 A+ u! l2 j
    4 l! K# v6 x+ l1 x3 c4 Q9 E' J; ?/ C0 z/ z
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    3 `( Q2 p4 x4 j1 H6 H& V     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    " M% I1 B: z9 }/ S0 j# L4 n5 C    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    ' }0 x! @0 x) Q; Z9 T9 I    #theme_bw()+; q: `( u& B6 b" I8 w9 E
       #theme(legend.position="none") +% u1 J: _9 i4 u* h3 e" d9 C1 O
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +; Q: l/ D- O0 V. K
        theme(panel.background=element_rect(fill='transparent'))+
    & E# i) t( N. P, L& L  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+8 n+ o6 e0 f- U8 c; M6 q0 x
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+2 Y# P" H5 W( _* V+ i& l
       theme(panel.grid =element_blank()) +   ## 删去网格线: X5 E! \6 Z% n5 e: [" ]
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
      C$ i. ^& w' D8 t  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线! N4 b, n1 T+ Q9 c$ n
    # Here comes the gganimate specific bits& y( I5 j! h0 O& D3 M3 j* X8 i. t7 T
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +, y, L* A4 y. w, f! Z! d
    #annotate("text",x=0,y=40,label=C,parse=T)+
    8 P1 N. s$ c, R, S7 d/ `coord_flip()+
    % D2 P+ M1 Y4 k9 P4 x! Mtransition_manual(frames=as.Date(Date)) +
    9 q+ F3 p- n8 b#ggdark::dark_theme_bw() + #设置黑色主题
    * b" Z, I$ D0 ]  y* n$ h) tlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+1 Q4 x7 c% e# P# i1 B
    ease_aes('linear')
    / s8 o' o# s9 x$ N$ Dps6 z1 S! ^8 l9 _- ~% Y2 [4 f& |5 o2 d: x
    结果展示
    $ z& {1 ~$ l* g& }: O; N) s8 _/ P6 M( R) F9 t: r0 S
    8 N6 v' ?' H) n
    视频格式转化,加载BGM+ p3 x4 W1 }' R* e" H: v( ?
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), ! ^% m) F. U6 `+ {. p, ?, v) c
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    4 s8 W' M0 O, K" ?2 J8 H' M# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")  F/ B( {, U( v& N: o
    全国新冠状肺炎26天增长状况
    ; h) v% r! h; F* M/ C; ^/ ^
    , \' u* D- F& m4 @' q, Cpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    / }* `; u% U* H1 e$ M: y$ @   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    8 L* j9 v* }8 b5 p' }: p5 [   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+% |3 M9 B% o) q6 u, l$ J
        theme(legend.position="none") +4 n) C4 z  U1 J- y& l2 ?
      theme(panel.background=element_rect(fill='transparent'))+$ X+ P5 o5 p% [+ F. W
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+9 o% w/ Q% Q6 C5 E" q& O
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+& e  l1 z0 q8 q% e' N
       theme(panel.grid =element_blank()) +   ## 删去网格线
    : E. u  P7 H- ]) H3 x/ l  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    2 l: i3 k0 W  w) f  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    " x7 ]0 q- A. E" ]5 x2 o# y7 A6 r/ b1 i  #scale_x_continuous(limits = c(0,6))+9 U# t" E& N) C/ _
    # Here comes the gganimate specific bits/ B. ?7 `, x5 J  v# H) m
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    # y8 H; j  Z! t, ~' }5 X8 U( I#annotate("text",x=0,y=40,label=C,parse=T)+
    & U5 g+ r( {. E5 G    # coord_flip()+5 K! f/ v% ]  R
         transition_manual(frames=as.Date(Date)) +& I2 }6 T% p; P
        # ggdark::dark_theme_bw() + #设置黑色主题
    . s, d4 X4 ]0 L' g4 l     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    & y3 `! ?: a, @; v+ o     ease_aes('linear')9 p% ^6 i+ Y' z3 b1 C+ @9 f5 f
    pc( ^- e. B& J/ C' `

    , }) K& w1 d! ^3 N: F* S' S6 z0 K+ @7 z
    动态图合并
    & k+ ~/ \" K  M4 Ulibrary(magick)' O+ C' t* O( S% \
    ps_gif <- animate(ps,,width = 720, height = 480), `; A' [- S! {/ K. S3 d  ]2 _
    pc_gif <- animate(pc, width = 360, height = 480)" w  {0 H7 T- S( L
    ps_gifs <- image_read(ps_gif)
    : s) R/ l. G$ s! G: M2 ]9 ypc_gifs <- image_read(pc_gif)
    ( c- R2 L9 B2 Qnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))! r! H' r/ Y+ S. U+ i6 g0 ]! b
    for(i in 2:length(pc_gifs)){& U5 e2 ?- K7 i1 d6 g% ?2 M- F
      combined <- image_append(c(pc_gifs, ps_gifs))* l$ {! x% |* c* }
      new_gif <- c(new_gif, combined)
    ; S, F# Q( D7 a1 [% E" @}0 g+ s* d4 x  Q1 \9 U* g  D+ e" m
    new_gif4 T9 t4 p6 C; j  ^3 N  w
    结果展示
    & f% K, |1 R/ G" z: ^; i
    2 u5 A  [( h/ j0 h" o7 L8 m0 A
    # Y, ^) h/ A4 {/ U/ q9 c& b+ [6 f* q5 x2 k1 M0 C; |
    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, 2024-4-27 07:54 , Processed in 0.397084 second(s), 55 queries .

    回顶部