QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3988|回复: 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  V* i2 z* |1 _所需加载包
    ! R2 {# s: x4 X" O1 F# c( Dlibrary(av)
    4 ^' o) y1 P6 \# J! |+ \; X) ilibrary(ggplot2)/ \" B- V& K( }0 F# Q- x) u  N% R
    library(gganimate)6 @; h: `# Q& h& n- Y8 w
    library(tidyverse)- @$ H) ?. C" \( w$ g$ \) U3 m
    library(lubridate)0 V9 [" J4 y1 u1 i
    library(scales)  m+ V0 W& k0 f( {" X
    library(ggrepel)$ y/ V8 s; P# I7 [# Y' c
    library(cowplot)
    2 \# V" Y) ]+ _0 [数据# @( Q! D# Y% c) L, @
    $ n) }: n9 V0 f+ m' r7 u
    " H. d% N6 b; G2 @7 ]. h- @- q/ U
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    # J6 Z0 ^# v3 @, T. d8 j5 N% W: `     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    # x0 Y* S. q, I8 \, Z0 l8 t    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    ; o. n" q6 g% k1 t' K) _    #theme_bw()+
    , l% H/ w+ d7 \   #theme(legend.position="none") +" h! V3 u* f8 Z5 i+ v" U) X- |
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +$ S3 _  h2 J% V
        theme(panel.background=element_rect(fill='transparent'))+8 H3 M6 G& e$ s" A% d' I+ B% B
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    ( A* [' h4 ^/ @, e0 D) h) Q  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    - T: R5 L' P- E6 n- a# [   theme(panel.grid =element_blank()) +   ## 删去网格线/ X: _2 a  h- v  [
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    + c+ z  ?8 M6 y' h7 ?  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线0 T% H' |5 v& O( s7 r1 s
    # Here comes the gganimate specific bits
    * F- }$ x- p9 a$ [9 R+ m* J#labs(title = '日期:', x = '省份', y = '累计确诊病例') +% c5 ?" r3 R% w. }& e' a
    #annotate("text",x=0,y=40,label=C,parse=T)+
    1 N& Q# z- ?  ycoord_flip()+
    9 a0 d( n/ d& B- v) X8 Rtransition_manual(frames=as.Date(Date)) +
    7 h6 F9 j+ ]8 p: a$ ~! u  t#ggdark::dark_theme_bw() + #设置黑色主题; l$ K! _8 n" P
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    5 U# H) o7 X" Z$ r* y4 mease_aes('linear')
    : o7 ~0 ^# M  D" m2 H* Lps
    ) ^0 h4 ?# J2 S7 m" f& M* s结果展示
    - {: l. \  R1 [' t$ g/ W3 N! s
    * J8 `, u! U" `" t& ~5 O% a
    视频格式转化,加载BGM, B" R2 o% B. W, l0 q: G
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    0 q8 O2 t( K* u' j, b. Q0 i         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作: Q2 L$ d4 {$ _& K3 N2 X2 H; [
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3"): i* I9 h% T. |& R: g; l7 Z
    全国新冠状肺炎26天增长状况6 L+ O$ p; n8 M" d& v5 D
    . @7 r) u" e6 P. m, P, W
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    % c) q. s4 |* z4 w  E. j   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +0 N! P+ G, w, R. F3 v. b
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+! k; B! J; ~2 S: t3 O6 W
        theme(legend.position="none") +; r3 B* h: H  F+ r5 g
      theme(panel.background=element_rect(fill='transparent'))+
    5 R( t% G$ a: E* y7 z0 u5 x/ R: ]  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+$ {% I3 ]% C6 R8 f
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+, O! h" x! l  w; D
       theme(panel.grid =element_blank()) +   ## 删去网格线. ^* [( a$ Y7 r7 t9 D1 ^3 y
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签- X4 H+ B' P/ {2 r: B: x/ S
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    0 X. r. f' |8 ]8 q/ S  #scale_x_continuous(limits = c(0,6))+, F9 }9 k5 X& j$ x7 o3 w
    # Here comes the gganimate specific bits4 s- B. P6 G  F" w/ q
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +8 U$ J; E% N4 G) }
    #annotate("text",x=0,y=40,label=C,parse=T)+6 q$ P. R+ K1 d  O/ s! B! ?
        # coord_flip()+
    . z2 F: A* }" x/ F. i     transition_manual(frames=as.Date(Date)) +! K5 r/ L2 d' Q- W
        # ggdark::dark_theme_bw() + #设置黑色主题: f5 c2 e" P% I" {) J# n* Q
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    5 L7 H) e% w/ X# r2 q* D) l     ease_aes('linear')8 h; q3 b7 X$ g( F5 b5 ^# g. f
    pc
    & w/ x" f1 O% Q$ p6 @8 F# y, @% r- U1 [
    + H9 a# l# g! @' j
    动态图合并' |# o6 Q' C+ P1 \8 W
    library(magick)
    ; o+ U! I2 e. t' O; u( v- c( eps_gif <- animate(ps,,width = 720, height = 480)( J4 I- d! A/ F) M# n0 W
    pc_gif <- animate(pc, width = 360, height = 480)8 _) G/ n* h1 ?; n% d& b8 @, s0 o1 y
    ps_gifs <- image_read(ps_gif); j. W9 {7 W. ?- |4 a
    pc_gifs <- image_read(pc_gif)
    1 H  p7 u3 i0 v* N- ?# nnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))2 Y8 n5 |4 ]' O* b2 u5 F
    for(i in 2:length(pc_gifs)){. t- V9 K# J* N2 b2 w/ e5 b9 I1 g
      combined <- image_append(c(pc_gifs, ps_gifs))# [1 F* K' R: G; ~* y
      new_gif <- c(new_gif, combined)
    8 g( A* L  t7 r# R/ z}
    1 k  K6 k0 {" W; Y2 q' h: i5 vnew_gif$ c! i+ Z- a+ f
    结果展示# h* w# x: b# H6 m1 q. {

    9 t0 Q1 d. p: b
    4 L- _6 j. B. d1 w
    9 r, T; r! i+ d
    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-4-20 11:55 , Processed in 0.414403 second(s), 55 queries .

    回顶部