QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3937|回复: 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学习* C) d( q- E$ ^' M0 f
    所需加载包, o6 o$ B/ d7 B" W2 B
    library(av)$ B, S* s5 O: g9 @
    library(ggplot2)
    / h0 d2 b" r0 o/ Plibrary(gganimate)
    . v/ M- M5 a% T2 B& Dlibrary(tidyverse)
    ; i' _* [; r1 Ilibrary(lubridate)
    5 Q5 A" G  R6 z/ R+ olibrary(scales)- M+ E9 e7 R% ?( Y& c6 d
    library(ggrepel)
    ( U; a2 N, o; d# l* A' I. U1 rlibrary(cowplot)1 D' k, i- r8 v5 z# C* H
    数据" n1 w" D' {* o: v2 e
    / P; z3 i- @! z) F. P
    / Y( A( D$ f5 |8 P: U4 f8 _, j
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    # g* V) U4 q+ b6 L( I     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +0 H! `+ m( ~  m9 `. a) T9 E# j
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+3 n' n' V- u) W$ P, d  X
        #theme_bw()+
    6 I1 e3 h1 S! d3 B0 V* O   #theme(legend.position="none") +
    0 T' ~9 M3 b2 |5 y% a" C7 z# |! I3 L    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +# m" B9 e$ }3 Y8 Y' `" t% ^4 |
        theme(panel.background=element_rect(fill='transparent'))+
    - L9 y9 j, ^# N. C- [  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    7 @1 ]0 {- w* o$ o/ j  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    & I5 j# f& w( B0 A  s+ \# Y   theme(panel.grid =element_blank()) +   ## 删去网格线
    ) [. q0 B: K& v  u, h$ O9 l  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    6 N- S0 F- `' w# {& m1 O  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    - r0 r* w5 t6 ]: t# Here comes the gganimate specific bits
    ' J) {. ^9 F" z( ]#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    9 O6 R9 N) f7 S#annotate("text",x=0,y=40,label=C,parse=T)+& v8 C" B+ y0 o' i& u% O. X
    coord_flip()+
    3 k2 v4 i# l6 y4 I, J% [transition_manual(frames=as.Date(Date)) +
    : V0 D( D) N. Z& a#ggdark::dark_theme_bw() + #设置黑色主题' A4 O6 [& s6 W' ?- t7 k# g
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+9 K( @! @* i& j
    ease_aes('linear')2 M( p4 j! h' M9 ~) D7 _  _
    ps
    ) q4 m* F2 Z. J4 R结果展示
    0 @* |% n6 \  y3 `$ O, {! W3 M5 {7 X& i  N! m

    , w, [/ `5 P( k8 P! p5 O' B4 J视频格式转化,加载BGM. }/ O/ C! L6 g0 ^8 l& g
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), & ^1 v( F8 b9 B7 n# y- O+ q
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作' H3 ?& l# _) {2 Z3 f) n
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")/ I, R# Y4 j! r1 ?6 `" J3 J
    全国新冠状肺炎26天增长状况3 A4 ?0 |. W6 P+ v1 }
    2 i. ~( z$ R( ]# X& W0 B
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    - {8 d$ ~( F4 \- r+ f3 D( k   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +% k4 ~( Z# ]+ r, k6 k7 [
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+/ j. B0 o+ L5 w7 R& ~6 e( p
        theme(legend.position="none") +- s$ ^7 [- z8 ^" J, _( P
      theme(panel.background=element_rect(fill='transparent'))+! O; o, x# Z4 P; Q( n% S9 o
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+. D  z, P& @( L
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+, ?* a+ s! _4 X
       theme(panel.grid =element_blank()) +   ## 删去网格线
    ' c) @# D0 `5 x1 g9 x6 K! G  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    0 d: I( z$ n: f# k* }5 E4 Y  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线" R9 A: P4 I, y, A( T" O* o
      #scale_x_continuous(limits = c(0,6))+( o2 G+ I9 n; N7 W6 N1 ~
    # Here comes the gganimate specific bits
    8 w& `8 s' |2 L' l" L: h) a#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    ! X* z  d! T6 r% E3 W: D5 l#annotate("text",x=0,y=40,label=C,parse=T)+. X: }; y3 q4 Q! k$ }
        # coord_flip()+
    6 c6 R5 e4 E4 M2 f) t+ D5 z) y     transition_manual(frames=as.Date(Date)) +2 Z: K! m8 L) `4 ]/ y1 o
        # ggdark::dark_theme_bw() + #设置黑色主题# N2 \- ?  L) {5 o! t1 q% @& s
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    8 R* Q) v& Q& l* L8 G     ease_aes('linear')( X3 `. P% S9 o: D  S( h7 P
    pc
    - A) M% o4 h% }* ^. C- J- ?0 B+ z/ M7 z0 Q0 q9 T6 ]- N

    * J8 A5 K* |, t# X! @; C) P# }动态图合并
    ) U" V" @* @% p& H, xlibrary(magick)8 r4 U2 m, P& g( {0 c) n
    ps_gif <- animate(ps,,width = 720, height = 480)
    ; E9 B$ |% Q( q( Q) xpc_gif <- animate(pc, width = 360, height = 480)3 v8 c4 m$ y6 q3 K( }# h" S# w) u
    ps_gifs <- image_read(ps_gif)5 |- O$ Q( B( j; _) Y
    pc_gifs <- image_read(pc_gif): @& A9 q4 F: {9 W5 |, u
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    6 S3 O) U4 X3 ~/ N4 f) x6 lfor(i in 2:length(pc_gifs)){4 |6 \+ f0 t2 @# z
      combined <- image_append(c(pc_gifs, ps_gifs))
    9 ?- b: r8 z6 l/ B  new_gif <- c(new_gif, combined)
    3 P- ^  Y, d3 s( |2 l/ b0 f}0 `' K- F! T* F  F1 t- q
    new_gif
    9 C0 }# ~1 ^" X  D结果展示
    6 L! U" c+ m& l
      d* k- |9 _9 O: `6 ~( d7 j
    6 q9 B: _7 W6 \. U# M3 c& s: M0 }- @) `0 i
    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-12-30 22:49 , Processed in 1.791140 second(s), 56 queries .

    回顶部