QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3499|回复: 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学习  j; w9 n0 J  k. w
    所需加载包
    % b& S0 ]3 v. y# D4 }8 c' alibrary(av)
    % y. G, L6 |% ]; E2 ?8 zlibrary(ggplot2)
    ( U9 b8 T" U5 ~2 j; slibrary(gganimate)
    7 p8 z( a# v/ g3 Llibrary(tidyverse)9 B+ M$ u; x3 |% x* p" {% ^
    library(lubridate)
    4 J( E! `! ^0 s; mlibrary(scales)8 S# e' ]: w- e( J5 `. x, p& T
    library(ggrepel)" D9 ]) h1 ~( g& t4 P4 Z
    library(cowplot)+ b8 J, y2 Z1 J  l
    数据
    / m* |+ l7 N! E5 I6 `+ k& q) U, K5 \4 m! @) r' n) t' g

    . U+ i" N4 d) L; U3 v* K2 _ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +% X% R5 B( ?( y$ v
         geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +# _3 A0 ~  [  @8 z5 k
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    2 j7 K  \! `- v" l    #theme_bw()+6 V- ^8 ~6 Q4 _  s  k/ ~
       #theme(legend.position="none") +
    ' n% E# h( B, S; T& x/ g* o( W$ V    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
    * V4 T/ [/ u. \8 T# j; L    theme(panel.background=element_rect(fill='transparent'))+
    4 n% W. P' u5 e6 ?# e2 Y6 [* K  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+" s' g( f* U& G
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+8 C9 P5 {9 V; _" M) K( _
       theme(panel.grid =element_blank()) +   ## 删去网格线
    ) c$ n& m; F* m  theme(axis.text = element_blank()) +   ## 删去所有刻度标签5 d+ O0 n' S: F* c* g6 T8 q2 H+ r
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    8 |, e  l+ m- O8 k# A7 }* q# I7 V" _- p# Here comes the gganimate specific bits
    + a! Z% i5 k' T/ Y& d% s+ W#labs(title = '日期:', x = '省份', y = '累计确诊病例') +  ^2 z' c: h( X2 A
    #annotate("text",x=0,y=40,label=C,parse=T)+: |% H! g: d% J  f' [0 \2 P
    coord_flip()+7 L4 L) N# v4 s6 L" o$ W+ T8 m
    transition_manual(frames=as.Date(Date)) +( T6 N% N8 D0 _, h8 L3 z( v2 O
    #ggdark::dark_theme_bw() + #设置黑色主题  S2 u* r: T: J7 ~) {/ B
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    ' |5 U% x+ r" a6 X/ U' V8 m# X9 \; Z5 \ease_aes('linear')
    3 r) h1 ?! d' ^+ X+ Lps
    9 N- K. ~! h/ @$ o- k; U( ~结果展示; J# H# Y2 t* [/ l( c

    " }$ i) }% e  J; W% T9 L& l/ R" C, b
    视频格式转化,加载BGM
    5 k. Y) t* q; F" j#df <- animate(ps, renderer = av_renderer('animation.mp4'),
    + v7 T) v9 d" ]         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    & _8 ^' f$ R) q* u) c# [) F# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")* J8 k. \6 A/ Y9 {7 K
    全国新冠状肺炎26天增长状况' z* A2 t3 _' {
    ) Y$ d2 ~# k+ I5 x) Q. n
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
    8 K! K6 f" z# O! C* ~# Y4 s7 ^   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
    - A4 j6 V3 T$ @   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+& j# h% z4 ~8 k, ?
        theme(legend.position="none") +
    , }: U/ N1 @- I4 N2 ]8 N" o! X  theme(panel.background=element_rect(fill='transparent'))+
    ! W) |, @: K3 w: _7 A  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    ' l2 M( \6 B# p1 c" L4 m0 M  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+% r3 C' M: x& M$ {7 {2 f0 m
       theme(panel.grid =element_blank()) +   ## 删去网格线+ s- w4 f0 q7 J0 q
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    9 d  @2 U9 H6 S( u  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    . {" M  |4 x2 s; q/ y" k! ?  #scale_x_continuous(limits = c(0,6))+
    6 t4 g2 p1 E- W/ f5 \  M- d # Here comes the gganimate specific bits: R  ], Q/ |0 C* j: a
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    6 k) w9 M3 K1 L( a1 S5 y0 G#annotate("text",x=0,y=40,label=C,parse=T)+; G0 J, p" V0 G
        # coord_flip()+
    # Z# |$ J2 N; \1 x2 L+ e; G  C     transition_manual(frames=as.Date(Date)) +
    + ~2 o* q5 a+ v1 b3 f+ }4 w2 a    # ggdark::dark_theme_bw() + #设置黑色主题
    9 s" B3 k0 I; E8 x& ^# @) r     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    & }" o+ d5 k" B" W8 m/ D2 n! d" T     ease_aes('linear')" x& Q* ~' o1 _( C
    pc5 f! [) }" ?' s: x6 Y
    . |- @) N6 L6 @; R
    1 r( w* ]9 W$ B5 @; ?
    动态图合并
    ; b! f. |% m( N( x1 l) f# ulibrary(magick)" c8 O$ ^5 ~7 t0 c+ l7 n6 |
    ps_gif <- animate(ps,,width = 720, height = 480)
    3 _& G6 f; f# b3 \* }pc_gif <- animate(pc, width = 360, height = 480)
      I4 y! c8 X, I, s. zps_gifs <- image_read(ps_gif)! n. [0 X& c$ n: g
    pc_gifs <- image_read(pc_gif)
    " H9 w- K" ^1 d5 B* Z, {) Y# dnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
      [8 I& z! R+ pfor(i in 2:length(pc_gifs)){( ]& J& Q0 e/ g" U5 a
      combined <- image_append(c(pc_gifs, ps_gifs))
    2 \% Q2 C; w8 w  new_gif <- c(new_gif, combined)
    ( ?9 `4 t. d+ {( F$ i' S- u}2 {' D" \' u6 D
    new_gif  H2 S% g" Z$ |! M* K' `
    结果展示' D# V7 ?8 v' E/ Q/ n

      q- B8 n3 s$ D0 _, z' b7 Z0 w; q& l5 z# f: X6 \, @2 ]8 Y
    . L' c3 Z; b% k2 Q. b
    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, 2025-5-11 02:27 , Processed in 0.436950 second(s), 55 queries .

    回顶部