数学建模社区-数学中国
标题:
R语言gganimate疫情数据动态可视化
[打印本页]
作者:
1047521767
时间:
2021-10-28 19:38
标题:
R语言gganimate疫情数据动态可视化
R语言gganimate学习
6 ?+ `4 b( y# J% R! H4 T/ N
所需加载包
7 H4 R4 B7 B+ z$ S1 F# v( q0 }1 Q
library(av)
, M/ [$ Q# j/ m/ i) ^' X5 a( x
library(ggplot2)
U% n% E y) i: k
library(gganimate)
# w; G6 j% U% d9 ?+ u# S( c
library(tidyverse)
' K' ^0 U, V; `7 r0 a5 S0 o
library(lubridate)
. x1 K6 I2 `4 D* ~. [& s! Y# ]
library(scales)
0 S# I' f1 E% l. I4 k1 z& |
library(ggrepel)
" d9 E! L& d5 N. d
library(cowplot)
. \3 }% \) H" t
数据
( ` V" B* ~) i9 w' J, x& t
6 T& z2 }, J+ E- r( u* x! E
- c' n5 \! b; M- u' j8 i# v
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
, I S% {/ v$ S7 j
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
; V6 a% \% t" ]9 o% b* N! r+ d
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
" o! ?7 `( c8 B. P o4 S' u
#theme_bw()+
0 {. z8 r# q. @" U0 x
#theme(legend.position="none") +
2 U) q+ i8 Q$ c" C4 i
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
& ^2 D# |! k. M7 H' n
theme(panel.background=element_rect(fill='transparent'))+
4 ~5 l' x7 u0 l- |4 {% g) ~9 e# |
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
/ }% Q' c/ v1 @8 Q: @) m
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
/ T s' G8 ^- u0 {4 Q9 L2 y: n8 C
theme(panel.grid =element_blank()) + ## 删去网格线
0 }% u- ^) U6 _4 l8 L+ P0 r( S1 ?
theme(axis.text = element_blank()) + ## 删去所有刻度标签
3 {) K. w. J2 v- M
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
/ a* b- i$ I* l0 g* @2 Z/ y! t
# Here comes the gganimate specific bits
m) n; X( N9 |7 k- X
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
! a8 ?& y0 l# q, N" U% _6 k |
#annotate("text",x=0,y=40,label=C
,parse=T)+
) G5 ?8 @8 \# V4 F: i- j* K
coord_flip()+
& c; h& ?- m9 ~+ g7 L
transition_manual(frames=as.Date(Date)) +
+ X* k! u8 D$ r* b, t
#ggdark::dark_theme_bw() + #设置黑色主题
" K+ b# V$ c6 k$ j. A8 K& g$ V
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
0 {- S; x& i! q9 f
ease_aes('linear')
8 N8 L8 I, I6 d* s9 \- J c
ps
: [ @! S& k1 p
结果展示
6 V+ J9 w6 T' R6 c8 Z( ?1 ^& W$ \9 f
. D* M) r. z# {% G9 l& T- O- Q( f# u2 M
1 E3 A! d( Z& Z8 l _; P
视频格式转化,加载BGM
; V% t( V: O! S( n0 ?# O- t6 @/ }3 R
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
1 \: F* ?) D4 R% E4 Z
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
' w3 s! f4 |* c3 |+ Y4 @' Y' \9 T
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
) N6 [! U- w. F( g
全国新冠状肺炎26天增长状况
( c+ C% g4 k8 }. o) g8 J
' v2 q: `! E! T8 [, @ R# z
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
$ \$ \( g6 M% `6 K* a
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
% o, ^, i7 K3 Y7 t8 K) J
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
( A# c0 p# U0 t
theme(legend.position="none") +
9 j7 Q+ i! Z5 c7 U9 u5 [4 g
theme(panel.background=element_rect(fill='transparent'))+
* _1 A; ?1 S! b' I3 u6 H
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
3 d' y9 x; N& ^+ F. r
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
/ Y L' h2 w a
theme(panel.grid =element_blank()) + ## 删去网格线
9 K0 p) x+ E( ~3 m8 N. B7 Z% }% v8 f3 i
theme(axis.text = element_blank()) + ## 删去所有刻度标签
- C8 o/ m% f8 O8 b
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
; A! O* g o+ K* a
#scale_x_continuous(limits = c(0,6))+
" p5 B: h- c1 N, n
# Here comes the gganimate specific bits
" ?2 u6 t3 F* n+ U/ W
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
9 T# Y0 ~: w& {: U& z" y
#annotate("text",x=0,y=40,label=C
,parse=T)+
7 `: Q: a- w, k1 B# o! b; k
# coord_flip()+
: o- V+ L( m/ {" V3 X! _
transition_manual(frames=as.Date(Date)) +
9 k* a4 n; r7 \3 {' t: C
# ggdark::dark_theme_bw() + #设置黑色主题
# E2 D* |! r0 U O% _8 Q
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
+ ]4 i& H! O# C) b8 v* O% V: ~9 K
ease_aes('linear')
' f7 \8 }- E) ?3 X
pc
6 j) X' O; b! q R% r: i1 T
- z2 u9 E* p" \0 F& ^! A
) Q; E& Q4 I( K
动态图合并
1 G6 A* M+ w/ k1 R% j
library(magick)
- D6 q% D/ `0 i3 C" e
ps_gif <- animate(ps,,width = 720, height = 480)
\: O/ \: m8 n& n/ T
pc_gif <- animate(pc, width = 360, height = 480)
' {, q* v* n2 t( y2 E; O
ps_gifs <- image_read(ps_gif)
/ X, a* v9 W g# `
pc_gifs <- image_read(pc_gif)
" y: c. E9 y f! _+ d: d, |
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
% A9 ~# O0 Y( T. |
for(i in 2:length(pc_gifs)){
2 _: G* c: a* R- F+ T& ]+ B `
combined <- image_append(c(pc_gifs
, ps_gifs
))
- j+ m" K# \. u* Q2 w; i4 y
new_gif <- c(new_gif, combined)
7 X! E7 M* h/ i, O0 I
}
0 k+ W% q. K! s" B6 |" l" L
new_gif
# W7 z2 D1 S. {; A* ]6 `! r4 ~3 j
结果展示
& j' E+ l. X0 D- {2 `; z/ z
7 w9 { ?2 X2 ]5 F/ U
* i7 w, w% w+ n# t& p* K/ j d
. D; J9 o ^8 `. s* [: |- [! @2 Y) n
作者:
sjlxdn
时间:
2021-10-29 12:57
111111111111
* \/ w# I5 N+ T, |9 g
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5