QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4262|回复: 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学习
    ; e' x) c; m% D1 J% N& h. v( X3 k8 p所需加载包) K  m' Q7 z$ D* \! i
    library(av)
    % i0 w9 [: O/ }) c: p; Blibrary(ggplot2)1 A, T7 {' P& k$ |( t
    library(gganimate)' M+ R: J, w6 k4 ~( ^: X7 @" w
    library(tidyverse)
    / ~$ t0 K$ L, X2 \library(lubridate)
    1 l. |- n0 H! ?library(scales), N- H  G4 o5 V9 C2 c+ |
    library(ggrepel)
    ' j! r; S, ~% V6 zlibrary(cowplot)
    % P7 i9 ]" c( }数据$ F6 {: s  G3 V9 a; q' z

    * }4 ]% ~' K3 ~8 b; f0 O
    % q5 h) o" U% o/ wps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    7 a# P6 F5 u* @* z) S9 l% _  Z& O     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    ' G1 |+ q( H; V; i2 m& ~( C    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)++ q* H" L% G$ w; x. h
        #theme_bw()+$ K/ P  Y6 A4 e! R4 B3 Q
       #theme(legend.position="none") +; n+ ]0 {5 m. A, a0 G, {9 H
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    : v* Q5 o* i# a  ]# @    theme(panel.background=element_rect(fill='transparent'))+# o) v7 m! z, b- l: I
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    9 R# ~: d. M9 f' t% o/ B  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+2 D! v( a' x. b" s* k* K, w0 @
       theme(panel.grid =element_blank()) +   ## 删去网格线7 [5 Q/ D( u- |6 }% t1 ~, N+ O3 ~$ X
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签- V9 l. T/ X' f$ C
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线* A8 p- O* G9 A( C1 T$ D# W8 u$ v
    # Here comes the gganimate specific bits, b. S' L, e# K0 M4 S" E7 C
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +8 D8 o( D- c" a$ n# F# ?! F
    #annotate("text",x=0,y=40,label=C,parse=T)+2 b" [4 P! l6 A8 Y! j0 H8 B
    coord_flip()+
    $ p$ k# j, n/ w/ L  W3 u4 @4 w3 Ktransition_manual(frames=as.Date(Date)) +" E1 }& V" N" w0 K3 c
    #ggdark::dark_theme_bw() + #设置黑色主题) N3 P2 E( S, y- q( ^- C/ Z! z; N
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+# `# A6 U3 Y! y, c. F# U% z
    ease_aes('linear')- S% s4 T: V1 ^0 ]( I
    ps
    7 Q) w, b2 q9 R$ u, Q* i结果展示
    + z! w* `# e6 O# A# v; p' M: D2 H& c$ P
    : d* n; u' J' j- t8 L7 D
    视频格式转化,加载BGM, G5 S8 t8 l  ~; h* z
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    ; L  `6 A6 [- ?% l6 ^' n5 P         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    / b2 m* I1 |2 p6 e2 D# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")# ^3 I) D2 i* c( ?1 Y' Q/ P
    全国新冠状肺炎26天增长状况& G# D& j: B2 I" ~& S6 U
    4 V7 o6 O# T5 N- w- m
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    % a* B/ J/ x7 i# n5 i   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +- b2 r7 r3 C  x8 x. _6 A( l
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    . X. t0 K' o6 l) @$ B" N1 _* g    theme(legend.position="none") +
    6 p+ Z. Y" ?0 {" \0 O# Z! C' z  theme(panel.background=element_rect(fill='transparent'))+
    5 |9 Z) @7 O6 R; ?  W  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+# m. E" K9 H5 ?  N
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+" X" A" P7 k  }4 d* ?8 G4 ~: n
       theme(panel.grid =element_blank()) +   ## 删去网格线
    ' @  s3 k) c. T( u5 u: `  theme(axis.text = element_blank()) +   ## 删去所有刻度标签- P$ D8 a' U! H2 U8 ^; a
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线2 O% X$ `: F+ V) ?0 u+ j" F, s
      #scale_x_continuous(limits = c(0,6))+
    ! s7 L5 y0 k4 A0 L4 E # Here comes the gganimate specific bits7 j9 |$ |7 T- a
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +9 O, I: k7 Z; f* }2 d/ y" Y
    #annotate("text",x=0,y=40,label=C,parse=T)+7 d' o) F! C" H/ A! U8 H: W# o
        # coord_flip()+6 f  b2 u7 J" z/ ^
         transition_manual(frames=as.Date(Date)) +7 P( o) O# }* q
        # ggdark::dark_theme_bw() + #设置黑色主题
    - B% t0 T6 M" Z     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    , b8 w. o3 _- ?4 R3 W2 J1 o7 N     ease_aes('linear')
    / M; Q- N9 k0 l8 X# Hpc
    0 b" m3 L9 u* f: U& b
    ; g3 ?4 }" A) h) h; o$ m
    & A  _* w/ V  ]+ g7 I; ^动态图合并
    0 f+ m1 f, Y5 Rlibrary(magick)+ L) c& l7 i! i/ P
    ps_gif <- animate(ps,,width = 720, height = 480)
      l! F" b3 T- q& d8 }$ Kpc_gif <- animate(pc, width = 360, height = 480)
      Z' K5 g) J. O* j3 X# h9 B# c) {ps_gifs <- image_read(ps_gif)
    7 q& ~1 X3 w) w% ?, ]7 r5 D* Y. Opc_gifs <- image_read(pc_gif)& D# t  p, W' B+ e. f. V
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    2 M) E" M" h/ N& m; jfor(i in 2:length(pc_gifs)){
    ' [  }8 r) ]# h! w8 _7 o  combined <- image_append(c(pc_gifs, ps_gifs))) M  t3 N( T3 j
      new_gif <- c(new_gif, combined)3 C# S. Q, J- c3 y, h3 G& Q
    }8 ]; z& I; \7 Y1 V5 _
    new_gif. ~) D9 E, ?. M8 {
    结果展示
    - A/ j# ]* t4 z2 p- a8 W: S8 w/ L; f0 U, e3 Y9 N5 Q# k. M
    - Y& V  Y/ n5 X9 V5 C) L
    / N8 a2 M; A( C) T
    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-10-23 09:13 , Processed in 0.366023 second(s), 57 queries .

    回顶部