数学建模社区-数学中国

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

作者: 1047521767    时间: 2021-10-28 19:38
标题: R语言gganimate疫情数据动态可视化
R语言gganimate学习
3 Y& U: m  ~! p8 Q' [所需加载包
( L9 M, B  u9 p  U3 E: ?  |library(av)0 E3 k& i. l  g9 }# R) E
library(ggplot2)/ `9 A, p8 A+ ?- u3 ^" o
library(gganimate)
9 S7 d) I" C( t4 E5 olibrary(tidyverse)
$ F* B) \2 X* z" K# w) mlibrary(lubridate)
8 {" ~2 a3 t/ M( ^& P" ?. \library(scales)# f# k- v1 Y( s4 F/ ^
library(ggrepel)' Z0 |- x# M8 n4 E2 q7 A
library(cowplot)
6 T; Z9 z" j" Y! [数据9 j9 K& I' G8 F6 ^3 ~

7 S3 M6 a: A* {" e, _' w+ ]  u  j; E" r/ N
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) ++ ?9 q& i  `# O& b# U8 I
     geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +1 ]! ^; x- ^7 C3 X+ W5 u. ?
    geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
+ R/ l  D  |0 c8 V( J    #theme_bw()+8 o# _+ i$ Z( E7 G( b3 |
   #theme(legend.position="none") +7 l7 g+ T2 O$ |' K/ P5 U
    theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
. g6 }/ Y/ D# S4 b& V" O0 U+ Z% [    theme(panel.background=element_rect(fill='transparent'))+
2 z( e) W& k' Q3 _9 @0 @) X  theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
: Q- p8 T. O5 |# f/ r2 {2 m  theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+/ a* Y& b: Y6 U0 q
   theme(panel.grid =element_blank()) +   ## 删去网格线% n+ e7 ]/ h& E, D' o, C+ O5 [( z
  theme(axis.text = element_blank()) +   ## 删去所有刻度标签
+ |/ A$ L( d% J* f  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线
; u7 @" T8 {9 U  T# Here comes the gganimate specific bits
: g' }4 E/ r& k4 f$ [. b#labs(title = '日期:', x = '省份', y = '累计确诊病例') +; ~$ `+ V, X1 T2 w$ G* l3 x! U
#annotate("text",x=0,y=40,label=C,parse=T)++ \1 \# }6 }3 l( H# [, _7 r4 M
coord_flip()+9 T. W$ u4 z) }( {, [" P  j
transition_manual(frames=as.Date(Date)) +. u+ e  E# e6 o8 J9 f
#ggdark::dark_theme_bw() + #设置黑色主题. u4 g$ E1 O5 ~& ]) b1 C
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+6 X& d8 W& `/ t1 j* u# B0 H: V
ease_aes('linear')
0 O  k+ g# r. F$ i+ \; Zps
6 i# @4 n1 s# Q6 s% M) Y) u( i5 J( N结果展示' d# {0 V: i& D+ G( H) F* B' F
( H$ a& F: C/ R/ ]; t

0 Q* F  X9 O3 c: ?9 Q* y视频格式转化,加载BGM, H3 c4 f7 t  C% S( p( R
#df <- animate(ps, renderer = av_renderer('animation.mp4'), / r7 Y/ z6 [) J- r: O8 g
         #     width = 1280, height = 720, res = 100, fps = 10)#视频制作
- F- V( V! ]4 h9 t$ ^2 m# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
1 K9 c* ~5 c* p& R1 c: {全国新冠状肺炎26天增长状况
7 Z: j0 W( d2 ~" P0 v" D& F9 Z
( x. R# ~/ H- F/ lpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+9 w2 l( |$ t; e; |5 A( {
   geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +1 K8 @* v; m0 E* o$ [
   geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
; v' ]; D& t$ Q    theme(legend.position="none") ++ ^1 @5 t# O7 F8 p: k/ E9 o
  theme(panel.background=element_rect(fill='transparent'))+1 i3 j, \! x( X  g
  theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+2 X8 G1 i* O: s# a4 w! U3 P) i
  theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+) H" r+ D: K; k: o
   theme(panel.grid =element_blank()) +   ## 删去网格线
: _+ `& P4 D5 N( b7 ?+ h  theme(axis.text = element_blank()) +   ## 删去所有刻度标签) W" \3 f9 k, I* a0 |8 t" T1 h
  theme(axis.ticks = element_blank()) +  ## 删去所有刻度线1 L! y5 }$ |% a" ]! q0 l
  #scale_x_continuous(limits = c(0,6))+
8 H. J! K5 Q( |5 B+ x5 ~" z # Here comes the gganimate specific bits
8 s5 d( P3 X! H. ?6 B' V#labs(title = '日期:', x = '省份', y = '累计确诊病例') +0 n  D. A# r5 F% j0 w; E& l0 t. p
#annotate("text",x=0,y=40,label=C,parse=T)+
$ C9 E/ h; j2 Z( l& _7 V, E, S    # coord_flip()+
- X( k8 E1 r, m& `% W     transition_manual(frames=as.Date(Date)) +% ]; p0 k" J' m7 i# h- \
    # ggdark::dark_theme_bw() + #设置黑色主题
# ~  n; Y$ L9 f7 \9 R     labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
  B/ o- c4 r+ ~4 j  ?  O5 E5 t9 N2 o     ease_aes('linear')
- G! g: f/ s7 R3 L' M9 \pc" N0 z) B. \/ T+ E6 ~& @2 }: y1 G6 C" \

" h# D  }' L; e2 u/ l
8 Q+ J4 j  X* S& x# P! m/ Q# M& c动态图合并
- X; ^# N# Z. c+ ]0 m) alibrary(magick)
( W* Y+ H7 @7 Cps_gif <- animate(ps,,width = 720, height = 480)
# ?, j. f+ y0 zpc_gif <- animate(pc, width = 360, height = 480)
3 G: Z: c$ M& s' Z+ S, @+ eps_gifs <- image_read(ps_gif)
2 }$ q! F& x, Ypc_gifs <- image_read(pc_gif)
3 T2 ~0 Z0 B% E$ Y6 K( E& Pnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))" c) ]" L$ N- J+ x
for(i in 2:length(pc_gifs)){4 j' p1 s6 C& n
  combined <- image_append(c(pc_gifs, ps_gifs)); W6 w5 f) k6 n" n4 i+ {7 B( l1 y
  new_gif <- c(new_gif, combined)
9 K* b. W4 q. ]}
& b! Q+ f5 R9 K* s- R% lnew_gif' z. I, {4 o/ f  c: f
结果展示
2 ?& I0 ]; W7 z% b$ r4 `3 H4 D0 A! U% R* {9 a
/ I; a0 n' T" J4 t3 U7 K9 E

) k; x2 }: s( V6 G
作者: sjlxdn    时间: 2021-10-29 12:57
111111111111+ q, V, f- V) y+ k5 f2 g* h





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