QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3771|回复: 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学习( Q( R# l0 q  |; }, X
    所需加载包
    4 C4 g2 m' |* k2 F; m$ Olibrary(av)# p: \( A$ X7 ~" c: {# V, q
    library(ggplot2)4 O- ^! Z8 a+ X* |: J9 J, P# c* n
    library(gganimate)
    6 }# m) v+ k9 _( P; \7 Glibrary(tidyverse)
    / A/ J: f* ~5 v6 P! K3 Clibrary(lubridate)" w" u- _" d$ p; T9 e3 J
    library(scales)
    8 v. M, E8 c7 Y+ R4 P7 |library(ggrepel)
    / M' V/ {4 e4 m: Z+ c  J7 b' e$ [) dlibrary(cowplot)
    * c! F8 N5 i/ _数据
    + Y# }' w9 I4 Y$ ]4 v1 s. Y8 q: g1 X+ Z$ ?
    , w7 H# L8 J* c, E+ P
    ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
    6 Z1 L8 v& }$ b     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +; E% f! v# U6 a8 m
        geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+: V1 Z4 N4 Y( B8 B  C+ o- S
        #theme_bw()+& A  E7 f, c8 [$ ~/ }& q' t: d2 L
       #theme(legend.position="none") +
    # @5 t- k3 X; w/ f    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +* \5 G3 Z. {5 e4 ^- W  ^7 s
        theme(panel.background=element_rect(fill='transparent'))+$ Q+ _9 z$ k9 C0 O9 G  P
      theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+1 |  A$ I2 l8 H- C+ y6 J! }
      theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+: \+ x* a% D$ [* V3 T# \
       theme(panel.grid =element_blank()) +   ## 删去网格线" c) z+ [5 c- f( z
      theme(axis.text = element_blank()) +   ## 删去所有刻度标签
    # O$ O* c+ B/ b& p" L  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
    " T0 c3 q7 r- d0 Q9 ^# Here comes the gganimate specific bits$ p# X# ~1 k8 O# r0 M5 A- s  y
    #labs(title = '日期:', x = '省份', y = '累计确诊病例') +2 |/ N& |( X' x; o" H8 c! H
    #annotate("text",x=0,y=40,label=C,parse=T)+
    # @3 k  T6 O3 Z- kcoord_flip()+
    + ]6 V$ g% z* dtransition_manual(frames=as.Date(Date)) +9 ~# a/ {% p' i* P
    #ggdark::dark_theme_bw() + #设置黑色主题3 ?+ D( w! A, s2 r" Q
    labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+2 l# Z2 c* e% v7 j0 ]
    ease_aes('linear')- o5 W; c* q( ?" p3 @
    ps
    : A+ f' ?0 x% n2 O+ ?' A: M结果展示
    & i5 Y/ _1 i# @: a6 [" t
    / B; n/ @2 X9 E/ H. q3 \" t0 E+ t# j4 H
    - ~  m. j8 W( f- Q视频格式转化,加载BGM
    / L/ [% Z' w' x, x  {- b! Y; q#df <- animate(ps, renderer = av_renderer('animation.mp4'),
    " Y2 ]0 j$ t( O  p3 I" c         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作1 q$ C! {5 l, ~! r7 c8 v
    # av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")* b0 c: l; G7 J" K# ?
    全国新冠状肺炎26天增长状况
    # G" `/ g8 R$ Z. V( ~+ i. ~  Z# q7 P* h6 j" g
    pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+9 `$ y# Z* L" O# I0 |2 q
       geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +" L" o' ?. \) ~6 W
       geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+2 P0 P9 l. G" P- O  b- h) Y) @' [! Z$ k
        theme(legend.position="none") +( q3 @8 i  {- R% ~* k) R" L5 y
      theme(panel.background=element_rect(fill='transparent'))+4 X* G* B% ?" T7 V9 l' p0 R+ w" T
      theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+7 B, Z2 q( m, Z. N; h; o
      theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
    & w( L& j. ^9 i3 K   theme(panel.grid =element_blank()) +   ## 删去网格线
    ! K: o5 c$ Y) y7 N  theme(axis.text = element_blank()) +   ## 删去所有刻度标签' q- @0 C2 l$ N& n6 p" l
      theme(axis.ticks = element_blank()) +  ## 删去所有刻度线+ {- r8 K/ i1 l; @: v9 U/ }# O
      #scale_x_continuous(limits = c(0,6))+4 x7 S- o# y4 G
    # Here comes the gganimate specific bits
    * h" q9 \8 ^9 u1 l7 t, ?#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
    . ^' j' Z: n" D; L: l1 A7 C" J) K#annotate("text",x=0,y=40,label=C,parse=T)+
    0 O" c* X  M! P/ b- R) j" z! f    # coord_flip()+
    & W* d4 Z5 i" _     transition_manual(frames=as.Date(Date)) +
    . H1 F7 i- ^8 g8 V    # ggdark::dark_theme_bw() + #设置黑色主题1 k5 ^$ w5 t) ]3 n; |5 r
         labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
    ' `8 n: v9 d# U5 Z     ease_aes('linear')
    9 N5 F  p( E7 z" v; Rpc
    & N5 b3 i# u# t" b+ A' k6 k
    7 K# W! g& m1 Z0 m! n) U4 T/ `: z' S8 \8 \3 s; q
    动态图合并
    : F$ U& ~* ^+ l9 \/ t5 Llibrary(magick)* g2 a! s7 k5 [
    ps_gif <- animate(ps,,width = 720, height = 480)
    4 X2 w5 O" x" L  w3 S' b7 i* Opc_gif <- animate(pc, width = 360, height = 480): h6 G# }% n. }" {! }% u- L  N
    ps_gifs <- image_read(ps_gif)
    $ @) n( p3 {" E$ y# ~6 epc_gifs <- image_read(pc_gif)) W# O  X( P+ j# X. [
    new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
    ; n, D; t$ q. V7 D# a/ O6 Ofor(i in 2:length(pc_gifs)){2 q/ s) v$ b' b) m% p7 j  F
      combined <- image_append(c(pc_gifs, ps_gifs))
    $ X+ M: g  W" c- C* S' U9 }  new_gif <- c(new_gif, combined)( z; Z( q  m2 j/ E/ `  G
    }& b- h1 \+ g. \0 Z
    new_gif
    1 N5 w  ]' j: G结果展示, R6 k# I! F" W/ P1 P* H

    7 g$ k& j9 r5 C4 P. k% @5 x! C, \* H7 m! o2 [) I
    0 {  |% _, `9 w/ Q
    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-9-28 21:21 , Processed in 1.777253 second(s), 55 queries .

    回顶部