QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3993|回复: 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 B+ @3 w) U" {% R; e  U; c1 Y
    所需加载包5 j* R2 \' ]5 v$ N5 Y; s& i
    library(av)
    - D* E7 C9 f4 s+ Xlibrary(ggplot2)
    % N2 C3 J4 L. zlibrary(gganimate)5 K1 g1 s! M: u2 j
    library(tidyverse)' S- h2 P! c' C, i5 g
    library(lubridate)
    ) }# g$ ?& `: o  Flibrary(scales)
    % q/ `9 @0 q; O# |3 alibrary(ggrepel)
    8 h  M* q: J" g) S# G( [" Q* mlibrary(cowplot)
    ( {3 f5 N" n3 {( ]数据
    ( R( f" X$ m) x& `3 C/ H: D- A1 o

    / L9 P3 l' h8 r* N3 ~ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    5 d# X7 M# h; D$ p# _9 u     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    6 }* Q, Q9 M1 @6 D    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+4 g2 ]; W$ e% l) i8 ^. r, e
        #theme_bw()+
    " H/ |: D) i  g8 ^4 p. K   #theme(legend.position="none") +
    ! H+ d+ u- t' p6 R    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    . F/ M" E6 f0 x" f9 f    theme(panel.background=element_rect(fill='transparent'))+* b6 q4 X% k+ W
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    # U1 v8 a- V8 K# R  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    / {0 G% }' h9 {5 L7 o( x3 k2 P   theme(panel.grid =element_blank()) +   ## 删去网格线
    $ C$ q7 X* V- ]* z' G  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    # E9 N2 D; C- [& Y  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    ( x: T; E* Y: s* K% ]# Here comes the gganimate specific bits# V7 }6 _* j3 ?  \. I/ _- o
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    2 Z1 T* q7 G/ s& b. B# O4 f& S#annotate("text",x=0,y=40,label=C,parse=T)+
    & c( C+ k( z4 F" x4 Ccoord_flip()+
    # q% O$ c" M) `6 ~transition_manual(frames=as.Date(Date)) +
    3 w/ ]* Z! W- m0 e" B# h6 Q#ggdark::dark_theme_bw() + #设置黑色主题$ }* i; e& i- I: l. b7 {; t
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+% [. }$ \. Q' @& e# d
    ease_aes('linear')
    / I( q7 [) ^( T: J1 X9 S1 Ops
    - G0 h) Y7 M# A9 x3 P6 b结果展示2 Q3 B* X5 k( ?# i

    0 H( ]1 I" F" a8 F- O: ]4 L6 s3 h9 K& q+ P" q/ ?
    视频格式转化,加载BGM" b3 @$ e# u3 w
    #df <- animate(ps, renderer = av_renderer('animation.mp4'),
    5 p: \; c# J: W         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作% |( ^  D! W, @1 P4 ?7 N
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")0 ?% o3 e: h% h
    全国新冠状肺炎26天增长状况
    + l1 g: ?! Q* h& n' X% Q
    ' c" ]. c, p# I* R" m! f2 }pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    * ?) b9 i, m5 o3 J   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +. Z( B" y# v. j* I$ ~
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+3 Z9 Z& u+ U5 E( e
        theme(legend.position="none") +5 J5 j/ @" B, a- x
      theme(panel.background=element_rect(fill='transparent'))+
    9 E3 K2 f' i. g/ v5 e/ _# U  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    1 W& U. K; y' g" J/ x" G  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
    ' `6 A7 H. X4 \3 M   theme(panel.grid =element_blank()) +   ## 删去网格线  {1 a: q. B8 ^8 k. F5 I0 W
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    2 f$ X5 T  B' q* k; L/ x+ X8 L  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    ( X! p- @! d1 d% ^! T  #scale_x_continuous(limits = c(0,6))+
    8 z7 X5 R" D/ S# H% f # Here comes the gganimate specific bits6 y( l5 d, l* z" K5 V
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +( |" ?$ L$ J$ A0 f
    #annotate("text",x=0,y=40,label=C,parse=T)+; s1 C% L5 E2 ]8 z! V
        # coord_flip()+6 ]9 E4 }/ W% S6 o9 z) P& a! i
         transition_manual(frames=as.Date(Date)) +
    " \" m$ g# ?; B' f. w0 b5 K" x    # ggdark::dark_theme_bw() + #设置黑色主题2 j2 {! W/ d+ [& f( S# W$ b
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+( y# n- i& S, H
         ease_aes('linear')* n2 p7 @6 H( Z8 y: [9 a
    pc3 ]$ B0 I; C$ x$ v2 s9 W, s  X

      o' [& p8 f2 ~, m7 p' T& b% _3 i- c9 h% R. {
    动态图合并' o: P1 g0 ~7 A' o4 O. U
    library(magick)
    ' m" V2 ^& b6 t: S9 m5 ?' Fps_gif <- animate(ps,,width = 720, height = 480)- @+ \+ j+ V8 O+ Q4 G! A$ G
    pc_gif <- animate(pc, width = 360, height = 480)
    ) @. {5 D4 h  C: A8 H6 Wps_gifs <- image_read(ps_gif)
    ' Y4 N8 I  f+ Gpc_gifs <- image_read(pc_gif)
    3 f3 ?# }0 v3 ]. R3 [new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    5 G! M+ L/ }( D0 ], r) B9 cfor(i in 2:length(pc_gifs)){9 r" D7 o' x" ^9 X4 q
      combined <- image_append(c(pc_gifs, ps_gifs))! v  X3 l1 z" p( a) p: X$ {7 a7 g
      new_gif <- c(new_gif, combined); s# m. ~4 T) k( [( z1 i
    }
    4 ~5 y9 ~8 L8 W  f' gnew_gif# H4 f$ _3 n0 S+ F, g& i) M8 n* `7 h
    结果展示* R) j: v5 Z- e  y, ?' m2 ?/ Y

    & }8 n8 ]- f/ H
    2 j" C9 F' A6 Y5 P1 t, X, [1 Q( D- A, i3 L
    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-21 05:06 , Processed in 0.422776 second(s), 56 queries .

    回顶部