QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3774|回复: 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学习
    8 p6 u, ^" y5 F( k# \所需加载包
    9 `% k. u2 U3 u! o0 \' X$ Plibrary(av)  v: v! n# T" Y8 d. a( F
    library(ggplot2)& r# f! o" ~1 g0 ^8 u* ]7 f
    library(gganimate)1 I' ^# n* a+ b5 f
    library(tidyverse)
    8 u) K  m0 Y0 [- J% Ylibrary(lubridate)
    . v" l' |2 d8 i/ F4 @7 m7 X9 Xlibrary(scales)
    9 F4 k9 f1 L4 glibrary(ggrepel)
    2 U- t# G  j& M( h6 ?; Ilibrary(cowplot)4 B  U* I) O! Q
    数据) D7 b! Q0 {7 f" ?3 u! x

    7 m) {' _2 i" f$ L: U5 S- u
    9 ~8 Z* D  S, P7 T3 {  O5 @ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +4 Y4 V9 E: h2 ^6 q" D
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +4 A0 I8 E- y9 {- ?1 z- i- N
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    5 n8 a1 v! @7 H6 I$ I8 E9 A    #theme_bw()+
    - A4 Q2 Y$ I4 g. F2 p   #theme(legend.position="none") +( k( [: g/ W8 e" [
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +) V7 h% |) ?% ]8 A% I$ Q
        theme(panel.background=element_rect(fill='transparent'))+
    - E' _4 T/ @4 Q) S  L4 R  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+1 I: C/ e( Y- q
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    6 L8 Y, D6 z9 L6 L   theme(panel.grid =element_blank()) +   ## 删去网格线8 L6 H9 R" B( u0 y. ?/ @3 [
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    " j- k( I" p8 p4 W  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线$ F! |6 }% ^% M6 w* d. z
    # Here comes the gganimate specific bits: n$ l5 f! n- C0 W% Y
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    9 q9 ~3 H4 z3 y2 M#annotate("text",x=0,y=40,label=C,parse=T)+. ~' O0 c5 s4 p
    coord_flip()+
    ) u5 @- f# g2 y* G" @4 P) k3 ptransition_manual(frames=as.Date(Date)) +
    % s- m8 d3 y, d$ q1 _1 l#ggdark::dark_theme_bw() + #设置黑色主题" j3 P3 B- s0 X
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
      D4 M; O" x! k5 x- aease_aes('linear')& x# _, ]- G7 U. E9 ~! c
    ps! Y$ g% ^( [0 u% ?  }! A! t; w( e
    结果展示
    8 o5 O# B; W! I# L6 M, _
    6 i. U/ ?- h/ Z  g
    9 j/ J4 Y: G& m: X% j( n视频格式转化,加载BGM. q) H* @* m# W1 m
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), - K" `) l# m$ T$ {& w! }0 C
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    / N6 n; W/ N, w* A: d. E8 x# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3"): v9 k6 ^+ p3 ~, I9 K% y) [4 F: C
    全国新冠状肺炎26天增长状况! ]' j9 J/ Z, a" e  w. Q

    # Y0 B) r$ z$ }2 P. s& F. L  Ypc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+( w+ D! ^  ?4 d: e! V7 r
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +! g/ @% Y' h: Z6 }0 J
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    , h4 g" ?2 G% W$ D: E$ Q    theme(legend.position="none") +
      l( X/ S1 ]. @& H  theme(panel.background=element_rect(fill='transparent'))+
    9 t7 b3 t6 g: ?3 J) X0 ~  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+6 C8 s2 x7 q, K4 P( J* U$ |
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
    6 h, L, b* F3 _( J7 d7 w   theme(panel.grid =element_blank()) +   ## 删去网格线
    . q- q. |# a6 u! X0 c& V" V  theme(axis.text = element_blank()) +   ## 删去所有刻度标签; e$ s( E1 `- S1 x
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线- w; i# ~- ~' ^
      #scale_x_continuous(limits = c(0,6))+' L3 l" w2 _/ t% T% J
    # Here comes the gganimate specific bits
    % V0 t& {7 ?3 T/ w1 M% Z% M' X/ r# Y, g#labs(title = '日期:', x = '省份', y = '累计确诊病例') +" \+ y* e( b9 P0 x5 \3 A% P" T
    #annotate("text",x=0,y=40,label=C,parse=T)+
    ' `( c. h7 R# ^& v& Q    # coord_flip()+: g" @' n0 l  \6 X) a* _+ V  I
         transition_manual(frames=as.Date(Date)) +
    % i9 N2 o% w6 `" |1 C9 |    # ggdark::dark_theme_bw() + #设置黑色主题
    & D/ U, s9 K2 E! L     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+; F! E# I$ _# M  u" |" B5 c
         ease_aes('linear'). G* h, O7 U# {/ k; ]) h
    pc; H% ~! ?9 i8 F2 {% K

    / @" h" w8 _) e; T; e  X$ O
    ; p1 G2 l  L8 ]: P动态图合并
    ( R0 |( n# |# y' P5 zlibrary(magick)
    8 S' b+ K! U0 ~$ w" I6 X$ ~1 T+ ips_gif <- animate(ps,,width = 720, height = 480)8 V2 z7 p: i6 v& j9 J5 ?" c5 F
    pc_gif <- animate(pc, width = 360, height = 480)2 j( f* `1 X! e
    ps_gifs <- image_read(ps_gif)
    ; J* S  ]. p$ b% T, _2 ?, q7 fpc_gifs <- image_read(pc_gif)
    / f3 u# ~2 ?$ T7 N& z% [: ?new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    ! m, }- q, l3 qfor(i in 2:length(pc_gifs)){
    + U0 v6 F7 {) f1 t  combined <- image_append(c(pc_gifs, ps_gifs))8 R! f- E7 [: v
      new_gif <- c(new_gif, combined)9 p. Y% ^  V6 W7 ~1 o8 ?
    }1 n; W0 L0 J( v  w9 v* ~" s! `* A  b
    new_gif
    + }3 b, Q! a- D: ~& M/ w" \- B结果展示1 T  }1 [+ Q% v+ s

    1 u! o" V8 B* E6 q! l. k" ^/ I* J- ?
    ! R( y0 ~" I9 g* F# {* X$ u
    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 04:31 , Processed in 1.903563 second(s), 56 queries .

    回顶部