数学建模社区-数学中国

标题: R语言gganimate疫情数据动态可视化 [打印本页]

作者: 1047521767    时间: 2021-10-28 19:38
标题: R语言gganimate疫情数据动态可视化
R语言gganimate学习# v% l- r1 v6 j5 ]4 _
所需加载包
: I+ k+ g* v" i" Glibrary(av)
! ?* H6 e& B) B6 Z  qlibrary(ggplot2)( x' k3 M6 ]* q1 G6 L" V4 R
library(gganimate)
- V0 l' F5 f) a# l& U4 }: blibrary(tidyverse)3 ]. n6 H! o9 g$ p' A
library(lubridate)- S9 F  x) ~! Z6 u* J# f6 R' N
library(scales)
4 ~6 v1 {( n( r3 h* Wlibrary(ggrepel)
" x! J1 I* O+ j# q% g! p$ }* Hlibrary(cowplot)
: k5 K0 \1 S7 x$ F数据$ D4 i+ o3 ^4 [+ Y3 y
5 I6 @: N7 k% r5 E* C8 L0 p

) @2 c! V  _. z& k" l2 B! ]ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
/ J: W" y0 z4 Y3 g     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
9 w! G5 J1 J2 S6 Q    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+; T; B- l! \& z9 ~3 F5 d
    #theme_bw()+3 H; j: ?' g9 t, j# Q$ b7 h7 _
   #theme(legend.position="none") +* i3 e) T5 C  b8 {& M
    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +2 ]7 }% j5 s$ }- X: R- S+ Z: G
    theme(panel.background=element_rect(fill='transparent'))+& A# ^+ U8 }  A  e
  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+; B+ c% A9 d' `
  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
  |+ {. k8 M# B, e3 M$ d; K   theme(panel.grid =element_blank()) +   ## 删去网格线
, k, x8 e. P6 ^, P% N# b/ S0 w  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
' d: R4 |# m0 F( |  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线0 i. A' s9 E2 t, p
# Here comes the gganimate specific bits
1 H8 q8 d3 H& s! G, ?#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
4 y3 ^! U: G( l5 P#annotate("text",x=0,y=40,label=C,parse=T)++ V+ Z+ I1 U9 s
coord_flip()+* d- o3 _2 @/ I% Q* G
transition_manual(frames=as.Date(Date)) +
# @/ M' |5 C) K4 {! D3 }( t( \3 F+ l: C#ggdark::dark_theme_bw() + #设置黑色主题" _7 }$ H) l2 r
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+; i' d# ~; I* c9 p$ _  t
ease_aes('linear')3 T- |5 j; e; m0 R9 c1 v% K
ps" ~' l/ `0 q( X
结果展示" T  u4 ^4 _. z% T
1 V2 e3 H. v1 d7 s: F
' l. B# I0 V$ |% O9 _
视频格式转化,加载BGM
8 i" a, g6 o' }1 _! i7 d#df <- animate(ps, renderer = av_renderer('animation.mp4'),
7 E0 T9 i3 f# k' B         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作/ _7 b% F/ H& s( m* o! n
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
" m/ B  I' S- B- e. d全国新冠状肺炎26天增长状况
/ u* r% b1 ~+ b2 ?! X- u3 W/ q! B/ _. Z% N; o
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
" g5 ]4 I: B# k# p# e3 h9 [   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +, j" t" U$ i2 o3 j3 @2 L
   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+5 s1 h: j! S0 z! q' G7 P3 Q1 Z; j
    theme(legend.position="none") ++ ^$ f+ r/ F4 C/ T% S! n: S
  theme(panel.background=element_rect(fill='transparent'))+' |- I, C9 D+ }2 p9 L4 {
  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
1 @+ ?+ _% E9 j2 {0 {/ ?; d  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
/ n' G( P% H  t% Z8 p   theme(panel.grid =element_blank()) +   ## 删去网格线( f$ k" n7 P( h
  theme(axis.text = element_blank()) +   ## 删去所有刻度标签8 ^+ s6 N- T5 v6 U) Q8 |* _
  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线5 q! D  {! V0 X) Y
  #scale_x_continuous(limits = c(0,6))+, P3 ^: ]7 C0 |& q4 d; L8 g& t. H8 k
# Here comes the gganimate specific bits
* e9 _& W, x# _- E) h#labs(title = '日期:', x = '省份', y = '累计确诊病例') +0 z% Z  B8 [/ q
#annotate("text",x=0,y=40,label=C,parse=T)+2 _5 m0 E8 r: v  ]* h
    # coord_flip()+2 l+ f5 f# O* Z. Y5 p! O
     transition_manual(frames=as.Date(Date)) +
) I* w" E8 G2 W* Z; F/ I8 m1 E    # ggdark::dark_theme_bw() + #设置黑色主题. {0 T9 g) O$ f9 h% W* R( b8 e' ?0 \
     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+- T$ Y: H2 h  m7 m$ `2 Q
     ease_aes('linear')
3 u4 j2 w. L# R" q' Ypc$ h4 w( E: t, J: P! A* i! L7 p: r
& \# e& S' K5 [& @  `2 C
; L5 H* p  D& z# q, a5 Y  u. `
动态图合并" c! ^% k+ {' k9 a8 V# Y8 I' O
library(magick)
. U" _$ b1 L3 E* l9 Y0 }; H' wps_gif <- animate(ps,,width = 720, height = 480)4 b& u# W% `' r4 U3 }5 h
pc_gif <- animate(pc, width = 360, height = 480)5 p! U3 ^9 ]9 k. X
ps_gifs <- image_read(ps_gif)5 J4 H: B7 e# k6 C0 g; c+ t$ E' m8 m
pc_gifs <- image_read(pc_gif)! l/ s& P. L3 ?# m  p8 J
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))7 I% X3 n; j( `2 m1 Z8 p: p4 V
for(i in 2:length(pc_gifs)){* N" ^' r1 P9 @. `" ]
  combined <- image_append(c(pc_gifs, ps_gifs)). h2 O/ c) ~/ R1 B
  new_gif <- c(new_gif, combined)
# f/ o7 P$ Y* S5 C6 u}
6 [% w# J8 S" Y# }2 h, L1 ]new_gif
8 Q5 z! v$ N  ?8 f% ]结果展示: P0 {1 k! ?9 h% Z
, o, c$ q! N8 L% O1 ^  u
- d: t* Z) n( j, S

1 ]( k& L% c1 ]% m1 y
作者: sjlxdn    时间: 2021-10-29 12:57
111111111111
% W  ^+ X- y1 ]8 b




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5