数学建模社区-数学中国

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

作者: 1047521767    时间: 2021-10-28 19:38
标题: R语言gganimate疫情数据动态可视化
R语言gganimate学习
$ e$ k9 a- A% L# V$ P2 V, |8 Y0 C所需加载包
& M7 T# ~9 T8 g* r& X7 j* v; R' Xlibrary(av), b- _8 g; |1 M+ K8 ?, I7 z1 p
library(ggplot2)
' N8 o) R& P: ]$ m5 z& y$ o5 wlibrary(gganimate)8 {  e. F+ N- R7 o* x$ }
library(tidyverse)4 v! R" [4 ?' U# L8 y. S. C
library(lubridate)
" n% J! {7 s5 mlibrary(scales)0 p9 h# W8 t( h0 Y
library(ggrepel)
' Q  }7 i# o9 V2 D# w. h- u8 Ylibrary(cowplot)! N9 N+ W/ `# R+ E1 ]
数据: ^: i. |8 H1 w, T: Z
8 }+ n2 v; k! s& {; f

( Y& r/ u2 R5 \* \ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
9 ?! @  o: f) J. ~     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +. o/ @7 o! P  r" |
    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+. v$ K) @+ ^5 n
    #theme_bw()+) Z! j3 J1 i+ X+ z
   #theme(legend.position="none") +3 x) i' z" ?7 _$ l: g5 ^# z# O' J. z& K
    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +% X0 m# C: T, f2 W- E+ u
    theme(panel.background=element_rect(fill='transparent'))+6 z+ x, {- e7 |1 F
  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+4 M& I! |! s8 p6 t9 Z  {5 C
  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
- V  k: P# x( i' F   theme(panel.grid =element_blank()) +   ## 删去网格线
' |: {/ s& f+ l/ @  theme(axis.text = element_blank()) +   ## 删去所有刻度标签0 a: U4 N0 W; G3 o& j$ G
  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线3 ]7 W2 v# b8 z3 X. i$ ?
# Here comes the gganimate specific bits% t3 [; z: b1 e, o+ y  Y, t9 ?$ {
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +3 J/ Q) I6 t6 _8 @8 \$ j7 g
#annotate("text",x=0,y=40,label=C,parse=T)+6 S5 {) M+ }1 c  H2 Q$ x/ C  V
coord_flip()+
% \" O) c- c, z6 x' a# Htransition_manual(frames=as.Date(Date)) +
& d7 q) y" t) o#ggdark::dark_theme_bw() + #设置黑色主题. Q# n7 k1 M7 v, e0 s. W
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+9 @. E0 _9 t: K. V" Q$ D+ V
ease_aes('linear')
8 m5 V, F( G0 G% N( Hps
  V& ]) G( ]( E# R结果展示
1 G+ Y7 B  q  f
. @% U  ?5 \" A( Y' }2 i" H: G$ X  W9 G7 C6 I+ x
视频格式转化,加载BGM: Z9 l: E0 Q0 g% d# v/ c) `
#df <- animate(ps, renderer = av_renderer('animation.mp4'),   J2 K4 [' E- p. a+ }' Y
         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
4 T/ h! ^4 G% J# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")& \  u7 t% f; W& X& Q0 t5 ~5 H
全国新冠状肺炎26天增长状况
& W  }' h4 _+ ]- a# }2 K1 r
+ ^# `) ]7 w, A6 g" [9 f- _* p. w5 R+ Q* Spc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
! z/ H. B8 h) Z   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +: ?, Q7 A6 n4 z' @9 _
   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
; N7 i' H& v# H1 j" c9 a" I    theme(legend.position="none") +
& u/ l, _$ d9 y# j8 \2 B  theme(panel.background=element_rect(fill='transparent'))+$ P3 r9 o: Q% W1 x! Q5 k
  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
' v) Y9 n  X  }. f; r! Q) W  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
0 H+ K  k; p% ?& z: q  h+ ?1 ^6 c   theme(panel.grid =element_blank()) +   ## 删去网格线. c3 J* W$ Y+ `# Z% a
  theme(axis.text = element_blank()) +   ## 删去所有刻度标签) k- `3 M0 K: B: Q) C
  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
( R7 l- P# X# j, C  #scale_x_continuous(limits = c(0,6))+: x7 \0 o$ u* g  Z1 A
# Here comes the gganimate specific bits
8 F' N: x& w& X1 d- M" E; L; @3 F#labs(title = '日期:', x = '省份', y = '累计确诊病例') +7 i# `. Y5 E* p7 B4 Q
#annotate("text",x=0,y=40,label=C,parse=T)+) k) t$ e7 Y- V- _/ t4 G0 B
    # coord_flip()+
. h4 z9 m! X) b4 O     transition_manual(frames=as.Date(Date)) +
8 j/ v" q& h! b    # ggdark::dark_theme_bw() + #设置黑色主题
6 x) D  d; X) _$ a7 O' b     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
8 z. x2 M6 u+ j     ease_aes('linear')
+ ?' Y5 T5 A+ l- m+ d7 B% Xpc1 F' k" }7 H( p- I! N. M* r
9 c! |) }; j9 b) s) b
2 r; v, k9 g4 {2 B' e
动态图合并/ ^8 e/ L" h" E; Y3 J
library(magick), \3 k' O9 D% ^! J+ x
ps_gif <- animate(ps,,width = 720, height = 480)- o( n8 l- \! p$ b8 k& Y6 H$ X( h
pc_gif <- animate(pc, width = 360, height = 480)
2 h* G0 i2 d9 p' W2 {" j- Aps_gifs <- image_read(ps_gif)
8 Y6 f' x% @, u8 wpc_gifs <- image_read(pc_gif)
: Z. u: Z6 ]% @- U  c* E2 nnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
$ y& R' \* c  o, i: ?for(i in 2:length(pc_gifs)){5 y$ j9 o" f/ @" v5 o. G
  combined <- image_append(c(pc_gifs, ps_gifs))
, Z" c5 ]5 a0 T4 G: u( W# V  new_gif <- c(new_gif, combined)  v9 ~! u9 ?+ i, K' K
}
% y0 j, T7 C: q  Cnew_gif2 a( p1 i2 O' |) J9 b" B
结果展示
) k: F6 [  j& U4 u( x
; D6 M( \) m* \- J! }3 f. M7 Y! X$ a) a2 i, a6 p5 {

# u' @/ E$ {% d
作者: sjlxdn    时间: 2021-10-29 12:57
1111111111111 [+ T+ G" U: Z/ K





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