QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4135|回复: 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学习
    ) k. N8 v+ N7 |% T1 ^/ f* h. D所需加载包5 i5 X* l, V2 ^) _
    library(av)2 C* o9 k+ H( v+ K
    library(ggplot2)
    8 x1 A  {( ^7 R$ l& I" M5 Olibrary(gganimate)
    8 Z4 B/ B6 ~& |library(tidyverse)9 C1 X' e, S# }+ f6 d* \8 B7 _) |
    library(lubridate)
    3 r4 o( A- b' e( t7 u% zlibrary(scales), j6 @) v5 @0 c' I, z; n
    library(ggrepel)
    . @. U/ ]3 Z- M* P) c2 rlibrary(cowplot)
    0 B" u0 O/ o: J  H6 t数据
    2 d! b- ]) G  t
    , x) k8 u& J+ w0 U5 v0 z" j9 u0 U, @3 W3 v" L
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    5 y# P. U; [9 U  @1 J     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +/ V( F  ^2 J: N7 x; z+ z
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
    ! G5 P8 z, C; O4 I) T    #theme_bw()+6 ~) F# z8 i  y# m8 s! n( }5 {
       #theme(legend.position="none") +  c* W: {& I, G0 c0 Y! y- M
        theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +5 V' s# h& V2 W
        theme(panel.background=element_rect(fill='transparent'))+
    % ]+ J6 @  R6 ~1 {( {2 P0 a- r6 n  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+: f# ?. r  J0 d0 m3 y4 D. U& y
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+. o& _& _! s  B, F8 k
       theme(panel.grid =element_blank()) +   ## 删去网格线
    . L3 S$ t. r* {2 _  M  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    , }6 B: O# z$ Z2 _0 R) f  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    5 E' @# L! D8 q; v, C# Here comes the gganimate specific bits2 t* t: B$ o; d- H$ f9 Q* }
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +0 h) O6 o7 {; O
    #annotate("text",x=0,y=40,label=C,parse=T)+5 W% Z# b8 ?' q9 O1 q6 C% v6 v
    coord_flip()+
    , n( e3 [* h2 Ntransition_manual(frames=as.Date(Date)) +
    . h5 d  _( F9 i' u5 n1 n. x- S% X#ggdark::dark_theme_bw() + #设置黑色主题$ ^, d. ]% _8 U* W  R  N7 A
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
    & `9 ~6 `" c3 Q6 m8 a. w/ Rease_aes('linear')2 J/ ?* k' |9 E0 r
    ps
    * N' e$ N3 w$ m) l, _3 U结果展示8 e- |4 F3 w$ m$ Z9 q
    & |0 S* S+ O. U: X! a

    ' C1 a* r/ P$ c- D5 `7 u2 E6 \. @9 t5 B视频格式转化,加载BGM
    / h% K0 {0 H4 M& X' Y#df <- animate(ps, renderer = av_renderer('animation.mp4'), & q6 }4 i+ e+ `. e
             #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
    + F- ^) b/ ]4 B/ H, _" v& s: j; T# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")* N6 T3 u/ Y9 `6 M
    全国新冠状肺炎26天增长状况1 V- h2 F) p# ~7 M
    5 p4 Q) E: Q. k0 d! a
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+' O/ W2 y& Z7 e) I9 Q
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +4 a, Q" v4 o/ p& K( E
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+& v3 B7 H" X" ~. Y5 n, h
        theme(legend.position="none") +
    4 Q" n* `' J* `5 a+ c7 J) s  _  theme(panel.background=element_rect(fill='transparent'))+
    ; {: U0 h; J+ [  h2 i+ K/ |! `+ g  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
    * A: N5 @6 B" t" ^  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+4 `2 i2 g1 Y* K% o0 i+ _; @
       theme(panel.grid =element_blank()) +   ## 删去网格线
    $ K9 K( F* t& E, Q# y) ~  theme(axis.text = element_blank()) +   ## 删去所有刻度标签- e. x- G, Z. U% b
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    ! S7 L, B, ]0 d* i0 A  #scale_x_continuous(limits = c(0,6))+3 O7 b) ]8 x9 g5 M( l( @
    # Here comes the gganimate specific bits
    8 l: v2 q, g* `/ g#labs(title = '日期:', x = '省份', y = '累计确诊病例') +* Q, F6 ^3 S2 H1 b+ h; w. k* ]$ ]  Q: f
    #annotate("text",x=0,y=40,label=C,parse=T)+: G7 b) v7 e7 S5 W8 M
        # coord_flip()+, V! ]5 X. X' `9 ~* q* \. I8 ^2 s
         transition_manual(frames=as.Date(Date)) +; E) r' V. e: I) w' O' ]/ a2 p  d
        # ggdark::dark_theme_bw() + #设置黑色主题
    5 N' F0 v4 }2 a( i3 X     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    " h# k# S3 @4 G" b- }5 c     ease_aes('linear')
    9 l5 P, r# F& t! h& G9 gpc
    . H' \3 y0 @$ [1 ?2 j& i9 C+ W* b# {1 x& M+ E6 n$ l
    6 [, l. N( |5 r" Y5 x! S# d. ?( T
    动态图合并
    ( g. T+ d' N3 p2 J$ D8 X2 |6 q0 Slibrary(magick)$ q* [  p6 s! i& X( {
    ps_gif <- animate(ps,,width = 720, height = 480)3 K& s* r- H0 [; q
    pc_gif <- animate(pc, width = 360, height = 480)" M. J0 a0 e- T# [
    ps_gifs <- image_read(ps_gif); K" r9 s# g/ G- i6 P
    pc_gifs <- image_read(pc_gif)% ^) }2 F/ N* t$ p1 y: S: @4 N: j
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))4 p1 \( S/ s! S* [
    for(i in 2:length(pc_gifs)){2 C9 G, d2 y$ `% T8 R  ~1 ^
      combined <- image_append(c(pc_gifs, ps_gifs))  U$ ~$ ~: H. ?+ c4 I% C
      new_gif <- c(new_gif, combined), X; ^0 e1 E4 U: V, v
    }% Y* @4 H( J6 ^4 {6 d# H
    new_gif
    - o8 e  n: M8 Q; D结果展示
    8 s0 q0 m! Y) B6 b* p$ K& a# g( p' ?: J
    % \4 x' g" K( ]4 v
    / R7 g& i! _$ a2 r5 i( X
    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 19:48 , Processed in 0.410210 second(s), 56 queries .

    回顶部