QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4077|回复: 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学习6 k& H: P" U! p8 s) r; R2 C8 f6 a
    所需加载包- _; g3 W, |4 k9 d; ^6 q$ g% Y8 y
    library(av)4 L1 {1 k/ U: }  s! y' A/ s
    library(ggplot2)
    4 L5 s" V8 R1 O# J$ C: f1 n6 e9 ^library(gganimate)
    4 t" s9 I' e& K/ E$ v  |library(tidyverse)" g( ]" w8 d* s$ e* A
    library(lubridate). L( O5 k2 u9 n# s! X! [
    library(scales)
    6 g6 ~" M& P9 O! Q6 G9 y4 w5 y( ]library(ggrepel)
    * P+ F, Y: p9 _) M0 i" Hlibrary(cowplot)
    7 @* G1 H  ]9 z) `" u; w数据9 f! H! L  P/ i" P* n: P/ I! r

    , q( l9 F( f* ?, Q( j) a' {6 k+ I  e6 R5 n  H! a& B) f
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    $ b; a+ ^) v8 v& m* z" U     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +. e: V4 M* G5 O. A) l/ P2 ~
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    4 C& m; h; N+ p$ H5 `7 e    #theme_bw()+
    2 {. k$ U1 `2 L7 @5 ]( u   #theme(legend.position="none") +7 z+ ?# n0 C$ s, Z) x
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") ++ E8 b+ R0 w' }4 o8 O
        theme(panel.background=element_rect(fill='transparent'))+: @4 l5 a- _2 \
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+7 I2 }. s3 i% v- \) J6 b: n
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    , o; w9 A! r0 d/ V2 x# w   theme(panel.grid =element_blank()) +   ## 删去网格线
    , E, f3 r7 p3 b) N9 P  theme(axis.text = element_blank()) +   ## 删去所有刻度标签6 a% S0 ?' z3 y$ q
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    8 _, d: @# f7 I5 j# Here comes the gganimate specific bits- c3 I" ^$ ]$ F" x6 }
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    ; Z% f$ [5 C! G, {  ?; ~# I#annotate("text",x=0,y=40,label=C,parse=T)+3 b' Q6 k& _' l. w
    coord_flip()+
    ; {5 N0 e- D- j( i+ h  mtransition_manual(frames=as.Date(Date)) ++ C. e) v7 H+ m9 |" t0 l" X
    #ggdark::dark_theme_bw() + #设置黑色主题% ~6 P5 l" R4 h. s! u; x5 b
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    $ v1 @" W( N/ z  J0 o4 ]9 {ease_aes('linear')0 d. d# v# U5 Z2 D  g3 o
    ps
    + X4 u; @2 A3 n, @2 ^结果展示
    + u1 i, d. V. t. |
    ' p7 {9 Q/ h  s$ q# m  Z; P$ A4 X6 S, Q# O
    视频格式转化,加载BGM* C+ o) S3 y. v0 J5 p- Q/ \3 n
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), & N; L. _$ p" x6 r  U& ^9 s: o
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    / G8 b8 i  a. C% }. |2 a  u) y+ b# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    : c% u  @7 N; q, o8 M; ?全国新冠状肺炎26天增长状况4 G6 N9 C6 v# x" B; B1 M% I
    * F. s8 \3 |) V4 p! M
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+. i2 P1 o% T! @8 O
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +& n, t' r: v% L8 @& w
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+: x# |  g5 Y1 _6 q; O) r, j( V
        theme(legend.position="none") +" c* k% Q, `1 ^/ O! r
      theme(panel.background=element_rect(fill='transparent'))+
    + e" P. u# {/ B. Z  u* m+ l  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    / _( b+ r+ b4 e: F0 @! ^  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+' m" m+ n$ d( `* V; J# E( Y
       theme(panel.grid =element_blank()) +   ## 删去网格线
    7 C; J' y) n7 ]- f7 g  theme(axis.text = element_blank()) +   ## 删去所有刻度标签1 E. n4 D# H* z* m; e
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线* L) D* F4 }& k1 P; j  i. Q4 l
      #scale_x_continuous(limits = c(0,6))+- U: |# V/ ?2 J, b, Y* J4 T
    # Here comes the gganimate specific bits
    2 e! t: |3 q. M8 L+ s6 S#labs(title = '日期:', x = '省份', y = '累计确诊病例') +- X8 _: R8 f, [& q( Z4 X" d
    #annotate("text",x=0,y=40,label=C,parse=T)+
    * v  Y. h* @) d3 T$ f. B    # coord_flip()+4 `3 L3 |  B8 {' i% D6 D+ M
         transition_manual(frames=as.Date(Date)) +
    ' q4 R- h' C0 g% n% k    # ggdark::dark_theme_bw() + #设置黑色主题, |6 t7 f5 G) m' |( N; b# A  Z, T* }
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    ( e! z% I2 d5 z1 r     ease_aes('linear')2 c" ^3 D! V' Y$ `) H1 p
    pc' P" L/ n0 J+ Q$ }) U
    4 _* o% l( Z0 W* M5 H7 _9 H
    3 a+ [% H7 y! D6 v1 x
    动态图合并, s3 D) F- [' w6 o' C% U
    library(magick)1 |2 O) e+ F% K% {. t) }: Q4 L
    ps_gif <- animate(ps,,width = 720, height = 480)* n8 q; V% v- P) M$ N
    pc_gif <- animate(pc, width = 360, height = 480)
    7 i. O$ e& F' Mps_gifs <- image_read(ps_gif)
    3 q7 f( M2 {9 `+ w" g# P2 a. ^- `pc_gifs <- image_read(pc_gif)
    / X% \9 i2 E* Y1 S- {' [new_gif <- image_append(c(pc_gifs[1], ps_gifs[1])); W+ F5 n2 }9 t3 t5 G
    for(i in 2:length(pc_gifs)){( x* e0 n7 e2 g9 f, n% @7 i5 D! d2 P6 c
      combined <- image_append(c(pc_gifs, ps_gifs))
    : t% H, p! v# @  new_gif <- c(new_gif, combined)/ N: g$ _, |! Q% F+ D
    }
    , d( T/ s; {& t# j- W: B5 s8 \2 \new_gif
    ; B. q9 z9 M1 k/ c结果展示$ b' d. y4 J! d6 Z# G: f7 ~' y" T9 L
    ; J' h" W& e) c( e7 v. @: J
    : Q/ N" I2 g) w$ x5 A7 y0 }
    / z9 B- I" n) O$ g) M+ T  l1 B
    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 05:56 , Processed in 0.345762 second(s), 56 queries .

    回顶部