QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4287|回复: 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学习3 W! n( c- c. ^7 z# |/ w% r: d
    所需加载包
    + u0 S- [6 n) O" G% i3 w! elibrary(av)2 r( Y/ U5 y) W
    library(ggplot2)
    , J. k: T) j5 _1 [+ F( V6 p- Ylibrary(gganimate)) \* n* b- f  O: L, S
    library(tidyverse)9 F' K& d5 [+ N; k0 h; a
    library(lubridate)5 v( f  H3 |/ h( g% V/ l
    library(scales)
    & s; t4 f0 j7 {; N/ E: Slibrary(ggrepel); _! x4 {3 R2 N/ e+ l& |
    library(cowplot)
    5 d- g- K; {( t* Q2 }8 w  C数据, k0 e; Y1 \$ _" q) \* r  H  R

    - ?5 b$ C: O5 D% Y( o7 O" L$ h1 k7 Q  \4 R3 o9 H- P
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +! s* ~$ c0 }: G
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    2 [" e/ {3 d: B9 b. k" O. x    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    5 R! x4 A+ H/ p    #theme_bw()+
    8 A; q6 i. }* x2 c   #theme(legend.position="none") +
    ) s8 r5 E8 l3 ]: C% [    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +2 b; Y8 z% a- b  ~
        theme(panel.background=element_rect(fill='transparent'))+
    + r% A: B3 m' M4 h* o+ \8 O- \  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    1 T8 p* j* k: F: @  g  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    - }" }* q% o/ j2 Z/ F1 e3 `   theme(panel.grid =element_blank()) +   ## 删去网格线
    & I! G' h# T4 f: P7 @- ]" e! B  theme(axis.text = element_blank()) +   ## 删去所有刻度标签, A" A  G: y$ ~& d6 i& X+ `
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    4 T; v- _; G7 }& T) F6 m# Here comes the gganimate specific bits
    # s6 ~  G7 t; q" `- D#labs(title = '日期:', x = '省份', y = '累计确诊病例') +7 j. ^8 M$ C1 h+ M# A. e, I
    #annotate("text",x=0,y=40,label=C,parse=T)+
    ( G  Q. X9 f( h1 Scoord_flip()+
    ! N. p6 _9 Q. T' w8 m2 P, h/ [transition_manual(frames=as.Date(Date)) +
      K' c6 ?/ L1 ^#ggdark::dark_theme_bw() + #设置黑色主题
    , Q1 Y$ A; J" e+ i# e  mlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    , t0 K2 }+ N9 S/ _  p% C3 b6 ~ease_aes('linear')% F9 }' p$ N, s; F  E# M- Y9 m# j
    ps0 }$ F1 n, r* \6 W0 a( _* S& u
    结果展示
    0 F5 i! W8 b; ]% n
    + d# q1 X4 I, ~! Q8 b/ v% M
    8 _7 ^# ?4 S7 ?( n视频格式转化,加载BGM
    5 l* d$ B/ C5 Y* `- Q, ?% y4 ]#df <- animate(ps, renderer = av_renderer('animation.mp4'),
    6 t( [) c, ]) J& M, j! v, J         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作6 Q% n  X6 O! J- A
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    9 G: w& ]3 S! X0 N全国新冠状肺炎26天增长状况; b/ ]3 j+ B# q/ k+ q) \% y

      e) @9 h) C' Npc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+9 _; g9 v7 F* }
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +- Q7 U- Q! N9 h: X) G" Q
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    4 m7 \3 V) l5 q    theme(legend.position="none") +
    , W  i1 l" G- I0 o/ U. [  theme(panel.background=element_rect(fill='transparent'))+4 @7 }7 u! ?- |; f0 v6 @) K& b
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    ; \- P9 c( |6 M1 f  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+( u4 |& s  @5 K- q
       theme(panel.grid =element_blank()) +   ## 删去网格线& O5 U% S1 x6 A
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    8 t. v% f% s, [: G. u  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    ( _/ c. K% U( d! x3 F5 ~! F  #scale_x_continuous(limits = c(0,6))+8 d. d$ z* A  C$ t; a* j
    # Here comes the gganimate specific bits3 |- S  w& s! l; l
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +. ^: V8 B+ I. a/ {. I
    #annotate("text",x=0,y=40,label=C,parse=T)+$ g; p! m8 x8 z+ W
        # coord_flip()+
    + \0 s4 X( V! G3 [3 ?+ `     transition_manual(frames=as.Date(Date)) +- K, ~" w8 Y( [8 f" ^0 ~+ y7 _
        # ggdark::dark_theme_bw() + #设置黑色主题, @. o8 g: ^. F. m) Z
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    , W3 Y* I8 l- \1 Z     ease_aes('linear')
    4 A# o/ U3 E3 }0 s# Q7 ~3 ]pc
    * ]/ X6 }0 N% `
    6 `' K# K" V& O0 f% D) I4 a6 S0 V! r$ l( k6 O* p' M
    动态图合并6 A% i( m2 x/ P) A
    library(magick)  J; u. A3 c  `6 v6 o
    ps_gif <- animate(ps,,width = 720, height = 480); ?* Z$ U- {* u/ Z4 |: F3 Y
    pc_gif <- animate(pc, width = 360, height = 480)
    5 ^" K/ {! G2 h$ E  A+ P* Nps_gifs <- image_read(ps_gif)9 S: G) P0 T1 ]. {5 x
    pc_gifs <- image_read(pc_gif)
    7 H9 Y8 E7 f  e  l) g8 w$ N5 p8 D5 knew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    9 E: U+ T0 l) W6 O2 o+ a: ^) Q9 ifor(i in 2:length(pc_gifs)){
    0 ?/ C% c7 J4 u  combined <- image_append(c(pc_gifs, ps_gifs))3 D$ T" H- k2 T& Y+ c3 v# T/ p" j
      new_gif <- c(new_gif, combined), G7 W& G- n1 _$ C$ D4 M/ k
    }
    * [- |, B* |4 k3 ?new_gif! n$ b1 @$ D8 U8 Q
    结果展示4 [5 \8 A1 d- m* i$ S0 J1 B
    . @$ v* S$ K8 X/ a" h0 f

    , L# j) b+ g- z' @' Y8 R$ f
    ' b& G; x: I# m' w) H7 X3 \6 W
    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-10-30 07:34 , Processed in 0.517498 second(s), 55 queries .

    回顶部