QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4008|回复: 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学习
      o$ H! S& E- N9 x7 ?, g- U所需加载包
    & N+ @  v7 x" R3 vlibrary(av)
      n2 G, L' t4 y# ~) ulibrary(ggplot2)
    7 z8 V; X8 x( [, Tlibrary(gganimate)
    ! |( G( S) \" ulibrary(tidyverse)9 m- }* I' \2 g
    library(lubridate)7 y! f* r( W2 I
    library(scales)! S! a" V8 h2 [
    library(ggrepel)
    8 w$ q9 H& V- klibrary(cowplot); t) {( o8 w: R$ p" [6 i! ?
    数据7 y9 }# j! w  J
    + X5 N7 D  }( q$ d8 m5 l

    * o& m- J8 t) b4 |- g. l$ f: sps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    - b# F, U2 G& _# e1 f     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +  [( y( I6 P1 R+ W" y
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+% m9 ?/ a: S9 j* ]: Z" o% z0 e
        #theme_bw()+, w5 L; g  A4 x+ n
       #theme(legend.position="none") +( |2 m+ V5 B. _5 ]7 Y3 ?9 n" B
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +- C* q& u  ]6 D) d1 J4 \" K$ {
        theme(panel.background=element_rect(fill='transparent'))+
    . e  u) R, |' i+ x0 o8 h. ^( t  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    & F1 ?6 z2 C1 A) d8 e0 b* P  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    ; o: j9 p# e- U   theme(panel.grid =element_blank()) +   ## 删去网格线
    # ]$ v9 H0 m9 L$ b' \  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    ) b  U  H' k: b  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线2 i1 m; C/ o3 Q1 I" [4 o
    # Here comes the gganimate specific bits
    & [7 Z6 K. j, }0 M0 u#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    : U, D6 c4 a. \( k4 B, L& ]1 [#annotate("text",x=0,y=40,label=C,parse=T)+
    5 p2 B' N2 L, N, L6 W) G7 O$ m! A( scoord_flip()++ ^0 p+ V7 e0 Q* R  s) }* d; `
    transition_manual(frames=as.Date(Date)) +
    7 @; E3 w9 f2 L+ Y% ~3 Y#ggdark::dark_theme_bw() + #设置黑色主题+ ?9 B: W3 o* O' p2 U. u4 J
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+8 V0 w! f* h( w' z) e, F
    ease_aes('linear')1 J: [! |1 F  d
    ps
    6 x9 N- g7 E5 [7 p3 K# L结果展示$ Q9 A1 [. W1 @0 F# |$ K2 s' i
    ! P" P; D9 [5 N
    $ ]2 R7 f/ v) O6 T
    视频格式转化,加载BGM
    3 l* i6 q7 o( l4 v1 G4 I4 X/ ]#df <- animate(ps, renderer = av_renderer('animation.mp4'), . ~. i( K" k2 y
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作# D+ ]1 ~: I- ]: m
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")2 ]9 J6 m& y1 J& ~/ m
    全国新冠状肺炎26天增长状况1 b( q# p# j* A
    % V5 J% L1 O. b2 O; C  }" h
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+! Q" ?1 ?4 x! q2 s1 ^
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    - D; e7 v6 E/ s4 k4 N, _   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+! H2 ~; j: q5 U; G
        theme(legend.position="none") +
    0 U% y4 L3 p5 y, _+ J% j  theme(panel.background=element_rect(fill='transparent'))+
    0 Y5 x) V* l/ Y' G6 V5 V; ]7 I  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+  ~$ G$ y) \' _1 N7 d
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+! V( ^8 ]4 S) J
       theme(panel.grid =element_blank()) +   ## 删去网格线3 u2 L8 L9 x( b% v
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    4 b, M0 ], [" r  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线3 K* x- X9 D( e( L. H! b6 ?
      #scale_x_continuous(limits = c(0,6))+3 J( a9 _0 A8 O2 h5 o2 e) M" r( P0 l
    # Here comes the gganimate specific bits
    ) `) m8 n+ L* D: d% z* |  W#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    7 m6 L2 u7 e3 f# ~% Y5 ?3 O3 P4 H#annotate("text",x=0,y=40,label=C,parse=T)+
    4 J0 d8 g' U  N2 x    # coord_flip()+
    5 B/ B1 e: {  q7 b4 ^     transition_manual(frames=as.Date(Date)) +
    0 o2 X3 m/ R! S6 C1 z    # ggdark::dark_theme_bw() + #设置黑色主题7 U, Y2 B& G0 ~0 C; {
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+. \. B. D6 A9 x7 y- R
         ease_aes('linear')$ n! I0 q5 {: |4 G$ }9 p1 I& V
    pc
    7 S9 C1 N- x9 q) C" @5 e9 x( O. K" M! ^! O
    % R& G' Z  G2 h5 C1 `9 l2 c7 o$ S
    动态图合并8 q* H, @: R, G
    library(magick), C7 ]% U+ B" h. S# y
    ps_gif <- animate(ps,,width = 720, height = 480)- Q6 V" R! e, i8 @9 o
    pc_gif <- animate(pc, width = 360, height = 480)* c* S0 U$ p: ~
    ps_gifs <- image_read(ps_gif)
    ( I. d( y. t9 G  o1 M5 u1 spc_gifs <- image_read(pc_gif)' a7 X/ Q( C% l8 X0 s/ |% s
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))1 X% r6 O% X/ n! w0 `
    for(i in 2:length(pc_gifs)){
    ( H  r* q. ?* X) O  combined <- image_append(c(pc_gifs, ps_gifs))
    ( M' T, x; S9 u8 B" T# E8 }  new_gif <- c(new_gif, combined), k2 y( D6 |9 H: H# H7 o) q
    }
    9 P' Q( I' Z7 a+ D( hnew_gif
    # Z$ r  R, {( ^! s# C结果展示
    & i5 n9 y2 T3 @- A+ A1 P# X9 p, v. A9 H/ F1 m3 A2 z/ l. \' L
    , K5 F0 q4 {1 R
    # Z$ M& v& y* X, ~" r+ l
    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-6-4 04:57 , Processed in 0.595449 second(s), 55 queries .

    回顶部