QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4136|回复: 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学习, P: N. l. }! B8 k- c4 s% E
    所需加载包
    # B4 ^* G# V$ W5 U: K% Xlibrary(av); C1 q1 U* j. [& C9 K. U1 d
    library(ggplot2)
    ' a8 ~4 L7 V+ H2 I6 ~library(gganimate)8 k1 r* y: d! |! J6 J$ s1 k7 Z
    library(tidyverse)5 ?0 M$ `- J; a& s
    library(lubridate)
    . a1 l: h7 N4 v1 I" G4 m' E- j9 ^library(scales)' c$ u/ f3 q# J0 Z! V1 z% m! E
    library(ggrepel)) ^! A: ^0 c3 L0 V9 _- c3 \
    library(cowplot)
    & \3 o- e# Y  Z* P6 M( f3 H: P4 N数据: X: s( M8 F) @
    8 D( w" K0 l& O! x! R. G

    # ?  i( C5 s, Z$ _9 d* ~: sps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +: z8 C! p+ \1 I0 a- u7 h# g" |# k
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
    ' n. w' v" m+ a  y7 B5 |    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+0 u9 Q+ n8 U$ U2 q
        #theme_bw()+
    - G* t. j) J+ M  z   #theme(legend.position="none") +
    / W; U3 {0 v! |) \2 R# v' ~7 C    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +) w! `- n, ~9 R( J& T* }: L) C
        theme(panel.background=element_rect(fill='transparent'))+$ z; D: m2 C5 S: S' q  h) x
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+2 `! {, n8 T( o1 S2 G) i
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+" g+ ?6 R& H$ m# t9 u' B7 U0 q& N
       theme(panel.grid =element_blank()) +   ## 删去网格线( v  |* U- o- T
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    , O6 B# t9 M1 B! Q& z  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    + a% v1 q1 K5 P7 {* o& A( q# Here comes the gganimate specific bits
    4 D3 Y: X8 r. J. s#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    # U* l: Z1 R2 e#annotate("text",x=0,y=40,label=C,parse=T)+
      q1 ^  V: d* u2 p4 C: o2 r  Zcoord_flip()+3 ~- @! k8 V* Z' X
    transition_manual(frames=as.Date(Date)) +
    + T! r+ d, c8 e$ q#ggdark::dark_theme_bw() + #设置黑色主题
    3 m+ T- J. t, l% @# qlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+( y, U, s' [0 q8 \$ F" t: n6 Z
    ease_aes('linear')
    : v$ W. E8 p' K, D3 aps! X/ c3 x0 p+ z% W, p8 O
    结果展示
    ! O+ S* q: E, B2 v+ t7 ~: i' k3 s2 r, K; b, }
    # X. ^: L0 K& Q
    视频格式转化,加载BGM( K% Q$ ~7 f  |! K! Z
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), - t  z  P3 O3 x9 ?9 f) P$ A4 U1 J& O
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    ) W  t) r5 @4 K* o  I7 o# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
    + p2 u3 Z  W: `- I% k全国新冠状肺炎26天增长状况) t. a5 H+ o3 g7 k, L, t
    $ V3 p( C' t* N4 a0 o( X/ o
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    ! G* x4 @0 f# C5 U8 Z   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    - e% N7 s9 C& B$ B/ z   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+0 K' P2 |7 m( H
        theme(legend.position="none") +" Z" L. y) Z0 a/ Z
      theme(panel.background=element_rect(fill='transparent'))+
    6 b$ y3 d% `" ^# P* C  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    7 J+ n! d$ q1 n9 s4 D" e$ O4 f4 A  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
    ( @+ Z1 N6 j( _+ |4 U   theme(panel.grid =element_blank()) +   ## 删去网格线: T# z( k5 B- a
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    + S# b- C# U/ l" R4 i! j/ K) c  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线* V/ ~$ N1 D& J9 f
      #scale_x_continuous(limits = c(0,6))+7 M* V/ i; W7 U* b
    # Here comes the gganimate specific bits8 F4 n& g( y; K7 f& W0 C1 ]
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +4 V5 b/ x. ]+ D- [; m3 k: e% j, v- n
    #annotate("text",x=0,y=40,label=C,parse=T)+8 e+ E: K5 Q1 |" ?: d, `) f
        # coord_flip()+' H+ I5 k9 b6 s. u$ |0 f2 |1 Z$ V
         transition_manual(frames=as.Date(Date)) +6 q5 S3 {3 g7 g( |/ A
        # ggdark::dark_theme_bw() + #设置黑色主题
    7 {; V/ D: O4 W9 V% S     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+5 ?2 L" P  v7 b1 y, s$ J6 d! f) j
         ease_aes('linear')) a0 b" g3 h9 [& P4 ?
    pc; x/ u( m: ?0 r, P9 w, e* n/ n
    ; Y0 X. {& Q4 X7 z
    ; K5 A7 l- O9 O0 s% x+ l1 W
    动态图合并
    , J* |- \& `# K# ^1 j& [library(magick)8 e. `* U4 Z5 m5 V
    ps_gif <- animate(ps,,width = 720, height = 480)$ F/ \& p* A; o: R7 @* q/ r. x
    pc_gif <- animate(pc, width = 360, height = 480)
    % k, @2 J) N2 `7 L; d- D# x3 ]ps_gifs <- image_read(ps_gif)
    4 S/ I+ D" _0 L' z. L! Kpc_gifs <- image_read(pc_gif)! M% A9 ~/ o9 d. j& l% W
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))' b+ ~% y5 J4 i6 }- i; q5 T
    for(i in 2:length(pc_gifs)){
    % N4 J+ h  ~* |: k$ t1 U  combined <- image_append(c(pc_gifs, ps_gifs))
    : H1 l) O: G! o& w  new_gif <- c(new_gif, combined)
      Y" a7 K3 o7 A1 Y. z4 s. [}
    - J$ w& [. J9 C, M" Knew_gif
    / A6 O9 x  n. f5 q0 T. w, G结果展示2 v; U4 j# |# B+ P9 O6 p
    / N- ?) s: ^2 j$ n) h
    - p3 [4 T$ V- W; p' X8 U+ c7 E

    3 A) E4 r: [; X- l8 C+ m
    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-9-6 20:38 , Processed in 0.426303 second(s), 55 queries .

    回顶部