QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4260|回复: 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学习5 i( y" l; o! V  H+ o
    所需加载包
    2 s; O& R) O$ ~1 V5 E) O2 g' ]library(av)/ w) V. X3 |$ z% F& a
    library(ggplot2)
    4 G) F" Z4 T0 Z6 Llibrary(gganimate)3 V" u2 M' {6 f6 J+ l; t
    library(tidyverse)7 g& j3 `5 z1 c* z
    library(lubridate)) y7 k2 ~* S- h3 S
    library(scales)1 t# c) v: s7 i0 H9 ?8 f/ |
    library(ggrepel)7 d0 L" c- i2 T: p7 n0 J
    library(cowplot)" F0 X" h" h- p2 V0 y" d) k
    数据
    % R" _' j: Z7 @2 z* x/ p! k# _- l6 j: \1 M. @3 ]9 v3 A
    + w) Q9 q4 x' P7 G6 X2 C2 h
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    & X- W% Y7 G& |1 x  l     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +) @4 u8 W) `3 L4 J
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    : O( G8 H0 Z' ]& V; W    #theme_bw()+
    6 _& ^' V, p9 ~3 I; b   #theme(legend.position="none") +
    ) |8 t2 Y; a0 J6 C/ @- {& w: G    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    / I6 d, q$ n" m+ Q9 l8 s3 R    theme(panel.background=element_rect(fill='transparent'))+% s" ], Y( j# N
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
    " Z3 ^4 }! k% d2 X3 _2 X. D# t  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
    ' P9 ]. l# n$ Z# \2 a! ]   theme(panel.grid =element_blank()) +   ## 删去网格线
    ; e! g1 b7 b, Z" `$ t3 u+ M3 d$ a( ~  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    $ m6 u& u# \* {" P  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    " i; K+ W8 {- X/ g& y# Here comes the gganimate specific bits
    1 C& z& D+ d% \8 @0 N9 Q#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    $ |, @! X+ f3 ~& A#annotate("text",x=0,y=40,label=C,parse=T)+
    $ e) d% A/ u& ^coord_flip()+) f2 X" U4 A+ h  v' Y
    transition_manual(frames=as.Date(Date)) +) a/ F' V9 b0 ]/ }, b( R& S
    #ggdark::dark_theme_bw() + #设置黑色主题
    % o  }; s5 a8 D( r5 Xlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+5 Z, U; V- c: t) x& i
    ease_aes('linear'). V$ o* }' a7 R
    ps
    1 o5 O0 X9 m" E结果展示1 O. l  v2 e8 h

    2 w/ G. a: U. G0 @) |
    ' n6 `; o+ b9 [$ O7 b, @( I视频格式转化,加载BGM8 A% t7 \; d7 o( G( @
    #df <- animate(ps, renderer = av_renderer('animation.mp4'), # \- V; A2 q  p% p
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    0 O% z$ n6 B- A7 ]/ R" n) Q# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")/ L! G! [7 O9 P
    全国新冠状肺炎26天增长状况/ d, J, e+ n. u$ }
      ~+ s: `' d. L$ K
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+! A6 {: w% p% I0 u) e* E0 [
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    ' u( H$ R3 H# o0 k* i% R   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+5 F1 d% T) X) K: z7 A0 I  y
        theme(legend.position="none") +
    ; o. u4 X  M6 S9 a9 n' m. Y  theme(panel.background=element_rect(fill='transparent'))+
    0 \! c" L# z" c  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    2 R' g4 Y0 k& m$ x" V. l) E  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+4 c$ O, E! v% B
       theme(panel.grid =element_blank()) +   ## 删去网格线
    $ D1 s2 S) o+ p4 w7 _  f1 x: S2 O  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    8 x* q4 a) C* r3 Y  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    : ^4 \! T% b$ D) N3 g  #scale_x_continuous(limits = c(0,6))+
    / b& ]. ^8 P) U6 n: Q" B/ J # Here comes the gganimate specific bits" x% e7 M! T! O( T0 _/ @$ J
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    0 Y- ]( g' a) P$ r, `#annotate("text",x=0,y=40,label=C,parse=T)+( |/ ]$ X3 L% g
        # coord_flip()+' c- j  s/ L" a) v. B; Z
         transition_manual(frames=as.Date(Date)) +0 K2 b$ `2 K# _3 v
        # ggdark::dark_theme_bw() + #设置黑色主题
    & ?5 D; d7 {" a  ^/ g9 G     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    9 `) I; M! @1 p! D3 a) ^     ease_aes('linear')
    7 Z' a/ D: q6 P8 f4 t4 d; v$ f+ zpc" Z8 ~# @  |  j' ~" e* J+ _+ T

    $ h/ N+ g  Q; B, g, j
    7 W* ?0 p: y2 t# n动态图合并
    $ ?  X  @/ O. u, rlibrary(magick)$ y: ~  i7 @/ X. [0 {( i
    ps_gif <- animate(ps,,width = 720, height = 480)
    7 v9 J  K7 _0 y! E  X& mpc_gif <- animate(pc, width = 360, height = 480)
    0 o$ Z( P$ f& x4 k# I' ?4 V/ n9 qps_gifs <- image_read(ps_gif)3 u" P- t8 j4 r' e2 {8 h& V
    pc_gifs <- image_read(pc_gif)( e5 T1 H, S8 \4 r# W6 S6 H
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    6 }! Z( V7 m) W0 Z' Bfor(i in 2:length(pc_gifs)){/ L5 t6 i5 L! k) W
      combined <- image_append(c(pc_gifs, ps_gifs))
    5 R. f/ L- b0 \1 U- K: |& {" Y* a  new_gif <- c(new_gif, combined)
    # z# J# M) O" ^. q}. `* Z% n+ n8 x' Z: B
    new_gif  d- S) Y- z- X
    结果展示
    6 h6 Z8 P5 X% u) U: W' R! h* ?+ k4 h! _6 f
    9 d3 e0 M2 ]  ?8 `: H# X9 H2 h

    " d6 k( }* t: J1 }  \1 M) r8 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-10-23 04:35 , Processed in 1.620243 second(s), 55 queries .

    回顶部