QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4082|回复: 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学习
    # x# ?4 C. o8 Y! s: t. J/ n0 u所需加载包
    ' V9 J, R( s& k0 a: K. Alibrary(av)* N3 H, S6 q$ O
    library(ggplot2)
    & n' `( t" D% k, R& ]library(gganimate)
    " Z" J, m1 R  @2 c3 Q* a8 Q& xlibrary(tidyverse)) j" F# R) }1 L$ c
    library(lubridate)( K) Y: o% F) y% O; q7 ^
    library(scales)7 i- N2 s# A4 i7 p
    library(ggrepel)
    $ M0 ^$ J" ?+ |0 ~& I. T8 e! ?library(cowplot)) F. B7 c+ Z$ O0 o: p9 o2 H
    数据/ j/ `2 i' O1 e  V! R( e
    8 W* i% }7 B; I8 j0 s1 _3 M

    ) q( |( h0 e; [- a- A+ w8 Sps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    ; l, Z. k6 }. k' d) y/ [) V     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +- t2 L2 j' _8 |$ o% Q
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+) G1 b- t9 E6 d9 Z
        #theme_bw()+2 H. v4 ]% Q9 m+ `# M
       #theme(legend.position="none") +
    9 y8 s' D. b+ v3 m/ ?" C0 R1 }) o    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +* h9 _3 _" y! B$ l) A" q& _) _
        theme(panel.background=element_rect(fill='transparent'))+
    " k7 h/ c8 `; p6 F2 c  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    1 _* V" E( w% h  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+' \; _: g" _) i* Z- [
       theme(panel.grid =element_blank()) +   ## 删去网格线7 N: s" @- A1 ^3 Y& q$ `
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签4 z, o* z  V) y2 h% [1 x: e
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    7 `8 ?& U) y1 R, q/ A* i4 U" n# Here comes the gganimate specific bits2 p! [$ m" F; r4 m% `3 ?3 j
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    $ @+ M( n7 ~8 P& @1 H5 I3 I& `#annotate("text",x=0,y=40,label=C,parse=T)+
    4 S0 t; v$ ~3 |! `* a; `' r2 @coord_flip()+
    # x+ f3 R* x) \* j8 Mtransition_manual(frames=as.Date(Date)) +
    $ C2 @+ B" b9 L5 N#ggdark::dark_theme_bw() + #设置黑色主题
    4 O- c6 C5 \& I3 blabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    3 i- |: U0 ^9 {. F/ Q% oease_aes('linear'): g6 e$ O* c3 W4 g5 Q
    ps
    3 o( [! H: M/ W' f2 B* Y9 I! ]. Q结果展示
    0 Z' r( o3 p  W5 ~7 z6 c0 \- X2 `2 U; v; D8 l% n; ^

    8 y& d- [7 N! `; @视频格式转化,加载BGM
    7 @% H5 Z  l' N! J#df <- animate(ps, renderer = av_renderer('animation.mp4'), ; X) b" D% `0 k: |. P! p: z
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作- \& {. G$ r. V/ b  \, l- u% i: d
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    $ z  l& h" d4 d. d7 i全国新冠状肺炎26天增长状况
    1 v9 J, v( |. m, H6 j3 |2 x1 Q; I' [1 h  h/ P5 j8 A% W
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    . y- w+ q7 q) D   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    . @3 v: y" n# a) e! ?0 x7 j( I* l   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    8 }, D6 S( H2 Z6 v: k4 q    theme(legend.position="none") +
    1 Q1 l. A: r; c5 D% P  theme(panel.background=element_rect(fill='transparent'))+
    ' h# Y6 A' w3 I- @; Y" D4 z2 [  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+; l$ y* k* u& q, T9 Y' T
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+! Q4 n1 M! u0 ~3 g  q: i' b3 d
       theme(panel.grid =element_blank()) +   ## 删去网格线# p) f* G  v, V, y  s" y/ P  v
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    " D; c& C9 n" t7 A1 Y  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    " m3 v; T9 }2 C& V6 K* y  #scale_x_continuous(limits = c(0,6))+2 k* L( I5 T$ Y2 Y
    # Here comes the gganimate specific bits: X& F( t: ~' D; i
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +8 S+ ]: V# S6 C: X* ?
    #annotate("text",x=0,y=40,label=C,parse=T)+
      P" c. L! X0 f$ C    # coord_flip()+
    " p" O/ A% x) J: e* z7 G     transition_manual(frames=as.Date(Date)) +
    & z) y7 W# {% B    # ggdark::dark_theme_bw() + #设置黑色主题
    # @6 g1 h3 j# W# {- c! }     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+' f5 }1 c  X7 K/ J: @5 c
         ease_aes('linear')! \2 s+ ~! S- Q3 }& S- l  o
    pc
    ' I8 S4 E; u9 m4 r2 R! X9 G0 B: w& K( K0 Z# M5 N
    0 K0 o: H: |# B" g7 a* r1 M. }3 R
    动态图合并
    . x8 ~- @* Z) g+ r+ s2 b1 U) Flibrary(magick)  d8 p& A. S6 s2 l
    ps_gif <- animate(ps,,width = 720, height = 480)3 g) ]3 I) x4 A8 D' ^) R" [
    pc_gif <- animate(pc, width = 360, height = 480)& O# A. b1 U. [% L2 D1 o- ~
    ps_gifs <- image_read(ps_gif)
    ! T3 S; ~9 c. R- T9 Epc_gifs <- image_read(pc_gif)
    1 J7 z6 t  y; ]; Y3 i- \7 J: E( L6 qnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    5 X: e* z! W  p0 }: Pfor(i in 2:length(pc_gifs)){, N0 k6 M4 ]5 G- }; [' j5 v$ h/ W
      combined <- image_append(c(pc_gifs, ps_gifs))
    6 O4 n: M" m  _# L  new_gif <- c(new_gif, combined)
    8 |, d5 [5 Y* q}) x! \! Z$ J7 a3 \4 r; I+ a6 @
    new_gif, b) _* ?* w2 r* c0 w
    结果展示
    - i% C( p' @; v% Z7 O6 R) t; h6 w- N, k+ S% e

    ' N$ _. P- _  g& ~1 l# z
    2 i. ~( Z* N- n$ m
    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-7-21 11:16 , Processed in 0.752584 second(s), 55 queries .

    回顶部