QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4079|回复: 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学习% ~7 w8 C+ q0 z
    所需加载包4 M: w& d7 ^" ?9 k  d
    library(av)
    6 Z3 I+ m( `% K6 H, ^% llibrary(ggplot2)
    : E9 S: |6 H) K0 D' f/ D$ |library(gganimate)
    ( a* U& x# k# d% a* blibrary(tidyverse)5 _; X' X% u5 J# ^0 H4 I
    library(lubridate). B/ M& }9 B! V! ~" o
    library(scales)* }( W, S# O; c! u/ g
    library(ggrepel)
    2 O# Z, l# M1 y6 A. I) slibrary(cowplot)( c' m% w2 S! x6 c
    数据, R+ y& E4 O* @% c/ M/ ]8 N
    . t: l; m' T# ~' n5 y+ A1 \6 q8 R  j
    4 D- F) v  q& s+ L' Z1 y( x; G
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    5 M( H& v, @- d( \" v7 `# n     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    , z9 N# p  G, X6 B+ b    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+7 K0 f1 w. ?9 w4 S4 N3 t  [0 T
        #theme_bw()+8 F' Z! X# u, U0 t
       #theme(legend.position="none") +
    ) g; u( y& `0 ~' k; Q2 P! q- q7 f    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +& |/ m" {6 i4 |8 Y- b; H+ F
        theme(panel.background=element_rect(fill='transparent'))+" X% w8 K$ c& H" b% ?# R
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+$ K" W! _! q3 i
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+" ^4 v* E5 d7 V8 a& T
       theme(panel.grid =element_blank()) +   ## 删去网格线7 Y' E) v/ y+ ]  C
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签* |9 @( ~: Y4 R$ V% L( r
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    9 @" w' }% ~) u! L" n+ t* i  Q# Here comes the gganimate specific bits
    + M0 }. B& B: m. z  b1 T8 U# i& I7 t#labs(title = '日期:', x = '省份', y = '累计确诊病例') ++ `, ]. o+ l6 r) Z! ~6 ^9 E
    #annotate("text",x=0,y=40,label=C,parse=T)+1 o& L# ^% F7 p& \1 ]
    coord_flip()++ G& x/ W; O, L% k
    transition_manual(frames=as.Date(Date)) +
    1 \. G/ U1 W, d#ggdark::dark_theme_bw() + #设置黑色主题( I+ ]7 W7 }" X0 l/ u4 b
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    5 l+ ^  Q5 D4 V" n* A$ Dease_aes('linear')- Z. p- P9 m: e# ]+ U- [/ |: b
    ps
    + v4 j2 [+ n9 g结果展示' o7 B' h' ~9 G& p! }
    2 t* W  h; V4 b& E/ b

    / \& }6 @# z/ v" L9 I视频格式转化,加载BGM6 _' I8 Z) r  d/ c1 A
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    $ _* f8 M# M9 n# Y. L" j         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    6 j# s7 F! A( O# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    2 I" |9 v/ ?& t3 U全国新冠状肺炎26天增长状况
    3 K0 O' K  s' b( y9 \+ q& V0 T3 O: `: ]
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+# v. E; w4 R0 K
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +3 R5 \* w4 X" _# F' ^' G2 {
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    5 b' p- D9 S( o& S    theme(legend.position="none") +
    + G1 W% z, V* e5 U" D  theme(panel.background=element_rect(fill='transparent'))+; T+ V6 [7 b7 x: V
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    ) L% Z; u; S% i) a6 q  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+" A5 z" Q* N, R, x$ O
       theme(panel.grid =element_blank()) +   ## 删去网格线0 H7 T3 U% E, G2 _- C* M
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签7 t3 `. {  s8 d" o- d
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    , h- G( `4 C9 w6 z  #scale_x_continuous(limits = c(0,6))+
    % d6 K) g" ]# A# s. S, \ # Here comes the gganimate specific bits
    # b' p: ]- I# N1 }; N2 w#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    1 y3 i( [# j9 x. k, a#annotate("text",x=0,y=40,label=C,parse=T)+
    - H/ {6 ]+ g% D3 v    # coord_flip()+
    4 k0 }& x0 E' M" I0 D     transition_manual(frames=as.Date(Date)) +. W9 O9 p$ X) |# \6 S
        # ggdark::dark_theme_bw() + #设置黑色主题1 n6 _' G+ }" ~- [3 x( Y6 _5 l
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    $ h; H5 f0 m) B) G. {* _6 e     ease_aes('linear')
    * K% L7 M: `* Y0 S2 t" \pc+ i9 a- o  @, m. {* v7 ]
    5 {+ J, l9 Q, ]# k1 x

    & L# k1 [' o$ K) _: G动态图合并
    ' Z' T/ R2 y( x/ plibrary(magick)7 F7 K& `4 ^  M. m/ _
    ps_gif <- animate(ps,,width = 720, height = 480): m0 d5 K+ ]5 `( D5 j/ ]0 j
    pc_gif <- animate(pc, width = 360, height = 480)5 E" I2 M1 ?5 U2 M  a& V3 n
    ps_gifs <- image_read(ps_gif)2 n! T4 B9 n( x) r
    pc_gifs <- image_read(pc_gif)3 Y2 O& {* ]8 J- K
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1])): c$ H9 |  Y" F' m2 u
    for(i in 2:length(pc_gifs)){
    " H/ j) P. g& p) r) }1 o  combined <- image_append(c(pc_gifs, ps_gifs))$ C2 h, `( j! p
      new_gif <- c(new_gif, combined)
    & H; `4 Z0 z' p9 b- s# f  G# q}
    + H2 C. a1 n6 r. d  }- J/ `1 K0 k7 hnew_gif
    8 h' F$ S, w# B( y3 P6 o结果展示
    2 {) X( C/ d! H% Y/ m, ]. U. Z1 O8 b& R, |
    * P5 f. `$ H) A$ k$ ]. Y1 d
    6 N8 d8 i% q7 `
    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-7-20 09:30 , Processed in 1.147427 second(s), 55 queries .

    回顶部