QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4078|回复: 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学习
    ! T9 N- ?1 o$ Z! \3 G% B6 W9 n" o所需加载包
    ! C# L! W+ L' I# tlibrary(av)
    / I! I. m4 b( F' L+ A+ ]# }+ p) r# Glibrary(ggplot2)
    . d! G+ [6 T) wlibrary(gganimate)2 Y6 V  h/ G6 |. R
    library(tidyverse)8 Y% F& t# z. l
    library(lubridate)) s# y+ q) K) `5 f7 a1 N
    library(scales)1 k  {' j  h$ J" F0 f& W
    library(ggrepel)- Y3 d, b3 U: K% y1 ~; h$ e( U
    library(cowplot)
    : @$ I5 I) q1 v8 X- }5 ?6 N% F数据
    : p7 C; d; Q3 Q
    " W; R# l, J7 y6 @% E$ c; K5 W9 r, F4 }, l1 ]
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    , p5 ~$ J- v1 ~# z/ L) h6 D  z     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +' v7 o% x% B3 @) {4 P6 V7 y$ Y' Q2 l
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+6 K$ q. V3 ^4 }  S
        #theme_bw()+
    " X2 S1 o( T4 M6 K% S   #theme(legend.position="none") +
    9 O& |* s% t1 O; T. A3 ~/ b  o" J' f& q    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    ' a1 J# x- z' T    theme(panel.background=element_rect(fill='transparent'))+/ t/ S1 V2 v1 e: B
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    4 R  Q! I5 G' P8 t  Z6 r/ D. D. x  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    ' U( H) a! W" S; W8 k" Z   theme(panel.grid =element_blank()) +   ## 删去网格线; E( B  ?3 x8 t' K, ]9 q1 Z- q! {" k
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签& S) i" E5 e" w" X. P2 I/ e7 v
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线* l! }- U6 {' o* y; b
    # Here comes the gganimate specific bits
    8 u' T) C, d+ l7 ^  c8 \#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    0 t- |; o& J: B" U8 q#annotate("text",x=0,y=40,label=C,parse=T)+
    5 a. P) }9 R' Fcoord_flip()+
    2 A- {: F: k8 v; Wtransition_manual(frames=as.Date(Date)) +
    " @0 S; s$ L  [8 n  F#ggdark::dark_theme_bw() + #设置黑色主题
    $ E' \2 P, ?* t( J' [labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+- D8 f+ t* k& o
    ease_aes('linear')
    , E2 S! C- H. b* b3 yps
    4 _9 H5 B/ g* H. `结果展示0 m$ u: f9 y5 A- k

    ) B6 J( X7 R* ?' z8 E0 @. @2 b$ P) O+ G$ X
    视频格式转化,加载BGM
    $ T  u* _0 v0 @: ~#df <- animate(ps, renderer = av_renderer('animation.mp4'), % G: |+ g8 }+ P# F3 w: t
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    6 L6 K4 b8 p' n2 W8 Q7 ?# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3"): H0 w- A  u4 B; G% }2 r
    全国新冠状肺炎26天增长状况8 S' ]. W' L! x6 t

    2 A2 F, L  S/ L1 ppc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+  T0 _- X: h. n7 D2 L6 e$ t
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    ; A, W$ L: y" }' t8 x   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+$ K) J, |2 N+ F. a
        theme(legend.position="none") +
      K1 Z3 Q4 P, A2 p  theme(panel.background=element_rect(fill='transparent'))+( L# L1 g& h4 B
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    1 m7 r9 t/ U5 b  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+8 h2 A5 ]0 ~# t4 {
       theme(panel.grid =element_blank()) +   ## 删去网格线
    $ |* `; H- N5 x7 B  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    + t) Y! [& d$ ~" M  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线; ]' [4 r, a4 L) d# w6 b. T
      #scale_x_continuous(limits = c(0,6))+
    $ O8 o* @- g! s! |" m3 e; P* S& Q # Here comes the gganimate specific bits# n( I5 f* n" x
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    + m8 l; j/ X9 b" V#annotate("text",x=0,y=40,label=C,parse=T)+- j9 J8 I3 |2 t& b+ Y" x  _
        # coord_flip()+0 @* T; w, x3 b! [- i# |" o. _) Y
         transition_manual(frames=as.Date(Date)) +* p5 n: q3 d& s$ q
        # ggdark::dark_theme_bw() + #设置黑色主题
    3 L/ M: |) R, Y1 `! }2 u, r4 ]     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    9 _9 n7 Z: v' I5 r1 G     ease_aes('linear')
    & Q" k9 c% k4 [6 U% W$ V9 zpc9 L$ w$ d) F$ C

    ( \2 u! [" H- g( [: \
    ' b/ w! i) g$ J7 n动态图合并
    ; J- Y8 |4 A- z/ clibrary(magick)
    + |1 a/ M8 }0 Q3 T8 X; ips_gif <- animate(ps,,width = 720, height = 480)
    & r3 s& \4 C) \pc_gif <- animate(pc, width = 360, height = 480)/ a5 o6 S* Q8 ~. d: H7 B3 a
    ps_gifs <- image_read(ps_gif)
    , R* _  x  P" Q9 Spc_gifs <- image_read(pc_gif)
    2 F, r$ o7 [) M" ^0 C5 ~* g# bnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    ( j# [" _: Z8 {, V! i' n; |for(i in 2:length(pc_gifs)){; Z5 N* ^2 ~1 r+ B/ y: o+ N
      combined <- image_append(c(pc_gifs, ps_gifs))
    1 `+ z$ L# E+ O2 a/ m' a8 D  new_gif <- c(new_gif, combined)
    + \5 d) _9 {/ p9 F1 X# [* a2 x}
    7 U, _  D5 t& @new_gif
    & S8 @' `, E) B# `结果展示0 g+ k3 c- D5 {% p" O' u. h% k4 m# D
    ( M+ e0 A' X8 q. \
    5 S5 m2 Y& f4 L) S) z+ w: j
    - r; W( S" F. W! ^" }9 o
    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 08:17 , Processed in 0.438698 second(s), 56 queries .

    回顶部