QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4137|回复: 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学习
    1 S4 U% W7 r3 f# X所需加载包) r+ s* _) G  X% M" D% w
    library(av)# J; G- `8 o+ j
    library(ggplot2)+ c6 o3 ]6 A! |; d
    library(gganimate)
    2 \0 i% @7 n' z$ ?* Klibrary(tidyverse)  g  ?) i# {( g+ ^4 d  ^% L
    library(lubridate)
    ! O$ b8 ~' G( B9 B. K9 Wlibrary(scales)
    ; Z/ M+ N  e) K  u% Rlibrary(ggrepel)! ?9 s# N( s+ Y" f
    library(cowplot); e( ^; V4 i; ^0 e5 H) t4 l
    数据
    % Q2 F4 s2 ^; Y+ f
    ( Z# a# ~  ?: B, Z3 Y& W
    - w+ j& u0 f% F" I" y& ?ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    " U" a9 s' ~6 w$ X  ~( r- F- }     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    $ G+ p$ p) l& M1 U4 r    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+' ~: X' [5 o7 D, C: e, y
        #theme_bw()++ F% S4 I( I8 H( g9 m
       #theme(legend.position="none") +; n& z. ~' p: x7 j2 i
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    . Z0 Y6 s  X4 q7 L- R  F6 s! D+ W    theme(panel.background=element_rect(fill='transparent'))+' b5 z2 R' ^+ q8 r& Z
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    ! c: N; J3 W1 R: v  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    ) s, |9 R4 u* h1 w' ?( I5 X* f- R   theme(panel.grid =element_blank()) +   ## 删去网格线6 ^) v; u. ?. C0 ^6 H* G; S
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签' W/ q( U% m$ C0 B* ~2 @* c
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    . ], S% U" o0 M! o$ d# Here comes the gganimate specific bits
    1 ]0 n% ]# a  Q3 Q# y#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    5 Y/ [- S& C% s* V#annotate("text",x=0,y=40,label=C,parse=T)+6 _  I8 {+ \% {+ [# T
    coord_flip()+0 }' |* ?. w5 ^, a+ A- O
    transition_manual(frames=as.Date(Date)) +
    ) ?* n; s. W/ I#ggdark::dark_theme_bw() + #设置黑色主题
    9 A4 \! T5 s3 ^6 Z1 Plabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+5 S4 D: F; y0 `$ L& ?8 x9 T
    ease_aes('linear')
    * s$ A1 ?2 _& O9 tps
    0 V) N$ h/ `- K+ e结果展示1 l; W2 S# R7 G+ z
    8 ~8 Y5 B4 ?) S) D$ D

    # k1 N; ~: ]' M( y& u1 {8 a, F视频格式转化,加载BGM
    : ~; y9 E" [& ?/ n: H$ {: ~; x#df <- animate(ps, renderer = av_renderer('animation.mp4'), : A+ G* \/ ~5 R
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作8 U* [5 P. g$ P2 I
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")3 i& S2 Y; N" Q. U( y$ s) k  j
    全国新冠状肺炎26天增长状况
    4 A1 t, v* d* L7 a% ]7 B; Z$ T0 V7 ]; l1 H; ~, W  P/ B5 N
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    % D, p" e* L( H( e   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +& r7 s3 ]( u6 l7 t8 }
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    0 ^( k4 O" G& @# T0 \3 f    theme(legend.position="none") +8 g5 @. R: M/ U0 @
      theme(panel.background=element_rect(fill='transparent'))+8 h2 o3 B2 c: N8 n3 b' ^; @, X
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    : |% k) Z  v! d7 c- L" }2 D  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+$ A5 Q) c6 F: E3 F
       theme(panel.grid =element_blank()) +   ## 删去网格线
    0 u6 j" n7 o0 t' ^2 k3 T8 x  theme(axis.text = element_blank()) +   ## 删去所有刻度标签5 e+ U- Z* c5 v# Y  V
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线0 _0 v' F6 X& r; h
      #scale_x_continuous(limits = c(0,6))+
    1 M- w% |: [% o; X) j # Here comes the gganimate specific bits
    6 \3 G& o, Z7 e9 c2 ^: B, ^#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    : }. t9 e% m2 e+ X2 `, l9 m# n9 l#annotate("text",x=0,y=40,label=C,parse=T)+
    3 |2 x( ~" F  A) ^8 q- y    # coord_flip()+/ H' g( H, h" s3 c+ n
         transition_manual(frames=as.Date(Date)) +
    " f5 l' u- l& C; {( p    # ggdark::dark_theme_bw() + #设置黑色主题
    & y, k! V  d) l3 w6 C$ B     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+$ \8 E. n; }9 C; ^# z
         ease_aes('linear')/ s; E( P: l: Y/ k9 B1 R
    pc
    / D! K+ o% Q/ K! |: U- U
    ! E2 p& K) E" r1 D/ x# Z2 j9 |3 D% c) n# U
    动态图合并
    1 w" X* c4 M0 Glibrary(magick)7 v' l  w+ F4 `  U3 x
    ps_gif <- animate(ps,,width = 720, height = 480)# k2 Y  p7 O( x" d7 t: T5 J& [# }6 M5 `
    pc_gif <- animate(pc, width = 360, height = 480)3 t4 c- l' _9 }+ G. W8 v4 {5 d
    ps_gifs <- image_read(ps_gif)+ v* j3 A* k% {; ]" ?! c5 q# l
    pc_gifs <- image_read(pc_gif): z8 E7 W( K- @5 X6 X" T, a
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
      X8 d( ]) v: I5 _$ J  P8 w5 Zfor(i in 2:length(pc_gifs)){
    ) Y# g2 t+ ^0 a2 ]+ ?+ ^" A  combined <- image_append(c(pc_gifs, ps_gifs))
    2 `& ~4 t  r. ?% h: |: O  new_gif <- c(new_gif, combined)
    # y3 f: ]9 E5 n$ r5 q0 G& n}: {; I% @2 e2 B. a8 c6 i
    new_gif
    $ V7 G. H, [0 y% K7 z( C结果展示
    / s; r5 b+ I: m$ G- m9 i0 H
    5 I' [; s$ k6 g* `2 R- L
    - G( b$ Q8 M; S
    3 ~/ u3 Y5 d( m6 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-9-6 20:39 , Processed in 0.426980 second(s), 56 queries .

    回顶部