QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3987|回复: 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学习& M0 K. A. z# z5 w8 ?3 b4 C+ Q2 ~
    所需加载包
    - h, G  z( k% O5 h9 ylibrary(av)
    ) Y) l  O+ S, F0 l( G( y3 V; `library(ggplot2), G( B4 s- a' h# K* Z6 Z/ ^& L) y
    library(gganimate)
    + }3 p4 N* d0 n6 `5 qlibrary(tidyverse)( K) g1 s  t, m! N
    library(lubridate)
    . q- w, p0 @/ g6 _3 Clibrary(scales)- U0 u- o; y( z3 A" s) z+ h% K
    library(ggrepel)5 y7 e( y9 l2 g: s4 j  m- r
    library(cowplot)+ A5 f$ P" N9 ]# V3 I
    数据
    6 c( t, }& x( ], _6 n0 j! ?/ n$ N1 b  ~1 s
    9 j0 N4 T2 F& w! F% Z0 Q
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    ) P* o3 l/ \/ n" V6 d9 r     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    + a4 `! g1 Z! H5 C1 v    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    . \& R7 `# N0 t2 E    #theme_bw()+
    + E  `3 N% n9 ?7 S   #theme(legend.position="none") +& s( N9 I8 L( G6 |5 R& R
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +& }7 }" W$ r) U9 H/ a2 Y) e
        theme(panel.background=element_rect(fill='transparent'))+
    7 y7 d) S. T! F8 {2 h8 H3 c: o  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+) u0 ]4 @/ |0 [
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+- F1 l5 ?! e0 z
       theme(panel.grid =element_blank()) +   ## 删去网格线5 S* X( J5 q  D
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    4 m" y) A' y, S3 U4 `  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    ( u( M% ]& m9 p/ I# O4 G. i# Here comes the gganimate specific bits) q* n  `8 C2 z1 a7 x4 j
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +: F8 t+ _# ]* h1 Z8 W
    #annotate("text",x=0,y=40,label=C,parse=T)+" O0 m0 g/ n2 i+ ?& o8 D1 w
    coord_flip()+8 u$ Q/ w" `/ k& \
    transition_manual(frames=as.Date(Date)) +
    & j. g* @3 `- J: \; @/ h" w1 z#ggdark::dark_theme_bw() + #设置黑色主题
    , `8 g( I0 y2 F% s. C( [6 H! flabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+7 E: P' T$ b3 u8 M
    ease_aes('linear')1 U% w! W9 ]+ t! U3 }
    ps
    : s1 S" \. d; \0 w5 o# F9 O/ ^结果展示
    * s7 v  \+ R( p/ g: z* E  E! T# F* `9 D8 r3 ~

    5 ~# B' G" r1 {; f$ E4 }. L视频格式转化,加载BGM
    3 Z1 `7 b+ }% w2 l( D#df <- animate(ps, renderer = av_renderer('animation.mp4'), 4 d7 e! l( F. u' A; B. x
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    2 s; A% N. N& |& h% ]1 |# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    4 N$ w9 T& s) ]1 A9 F全国新冠状肺炎26天增长状况
    9 i7 s% d( {3 W9 g. H& l
    - ?: N5 s/ d7 x; [) k* u2 s* J; _* fpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    . ?! |( i6 m& K8 c2 X+ B   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +2 J: u% U$ b! h- k* b' j) b
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
    * q/ `5 |9 l/ ^" f0 l- f4 v    theme(legend.position="none") +5 d4 J& j6 q. v2 W" ]; ]# u
      theme(panel.background=element_rect(fill='transparent'))+
    0 ]1 ?4 F7 \9 ^  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+7 Y# F1 c3 ~& n  Z! X* M
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+# n& [9 z4 E6 I
       theme(panel.grid =element_blank()) +   ## 删去网格线, t3 x& g" ^# H
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签8 s* @: Q# ]/ K+ \
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    . r4 U" A  r" E  #scale_x_continuous(limits = c(0,6))+
    ; i- E" i" a4 C* p5 i  W- B # Here comes the gganimate specific bits
    7 x8 {8 f1 P0 d0 l#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    5 Q7 j+ J; B! p- V0 d) K#annotate("text",x=0,y=40,label=C,parse=T)++ g8 ~' ?3 z* {! w0 r$ ?0 u/ K
        # coord_flip()+; R6 @& j+ y. j$ Q; z0 L
         transition_manual(frames=as.Date(Date)) +
    8 k4 H0 X7 h  u* J    # ggdark::dark_theme_bw() + #设置黑色主题1 r, v1 j4 @# O6 `/ K- i
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    4 @$ r0 ], q  y6 O/ f     ease_aes('linear')( y" g& T4 S7 l8 p& a& U% W
    pc: A& y9 [2 N( s9 D3 D3 l& }

    * o1 ~. A1 M5 [
    ) j$ q7 i) {% Q% O6 A动态图合并# p$ t$ X9 [0 o  T- y: ~
    library(magick)
    9 J( |( v* u1 [ps_gif <- animate(ps,,width = 720, height = 480)1 d& o0 S4 g( T2 l: ]5 p7 U* ?* E
    pc_gif <- animate(pc, width = 360, height = 480), o7 e: i7 Y; C( R# J. s# _% ?
    ps_gifs <- image_read(ps_gif)
    9 g3 a4 F* ]6 t. X5 r+ |) q2 _' Opc_gifs <- image_read(pc_gif)3 K1 H5 Q: _# k9 n- r) t: [2 f
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    $ l) l& T7 I$ a/ j- dfor(i in 2:length(pc_gifs)){2 f' }" X1 {+ k9 `' [
      combined <- image_append(c(pc_gifs, ps_gifs))
    ! L7 l) |) j' x  new_gif <- c(new_gif, combined)
    $ A5 T! R% I* T% {/ _}+ W* X8 E7 ?* v& L) G: v, a
    new_gif
    9 j) [. C# h0 {  e1 Z, E结果展示& j( k7 k4 v4 Q" v6 y
    4 Z  }/ F0 b  z. f' W8 f

    ! z) B" s( N% \# [- w8 \4 t9 i; ^$ Y9 W6 `; A$ s
    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 08:24 , Processed in 0.397368 second(s), 57 queries .

    回顶部