数学建模社区-数学中国
标题:
R语言gganimate疫情数据动态可视化
[打印本页]
作者:
1047521767
时间:
2021-10-28 19:38
标题:
R语言gganimate疫情数据动态可视化
R语言gganimate学习
! n8 `7 ]# z1 C% p
所需加载包
4 Q8 _ z/ Z. W7 U) S6 K. s
library(av)
- u$ }6 F6 z! g# }" \* b
library(ggplot2)
) @1 U- M" [" O8 z& O
library(gganimate)
) E; O* j4 \$ F# k5 r/ z* L
library(tidyverse)
& _/ m* h' A+ `. i2 j& t+ ?0 e
library(lubridate)
- o$ m9 r+ d; I
library(scales)
3 o7 ?) M9 `5 e8 F
library(ggrepel)
, S. Q2 J7 x/ x
library(cowplot)
8 L( @/ F9 K: S, R( X' f6 P
数据
* k% @7 l7 p0 c. L
' m$ v E- z: r' W' ` U3 Y
) X) K% r: o) e: P* E- ]
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
5 V4 k& y" u6 q% F
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
) N! X& R ~% _" T; B7 g* \( G/ B
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
0 s5 a& U9 ~ b8 a+ m" M a
#theme_bw()+
% [5 u! e# R! f8 D- w" [6 w) J- |& O
#theme(legend.position="none") +
0 }- y3 g3 }( V& A! F
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
: j0 e" G) i& u0 B
theme(panel.background=element_rect(fill='transparent'))+
U* I8 w. W8 K J
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
; S) @* w' e% {: g3 \8 Z3 _4 D" z$ J
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
p+ q8 e9 m5 m& E0 E, y- L
theme(panel.grid =element_blank()) + ## 删去网格线
/ F6 p0 c4 Q, P
theme(axis.text = element_blank()) + ## 删去所有刻度标签
1 U( }# {( Q+ e* w1 K! c) \
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
2 p4 J7 e% d" U7 t! i$ T) X
# Here comes the gganimate specific bits
* i0 Z( P0 N( G' k
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
% _8 D0 E9 Q/ _" ?% p
#annotate("text",x=0,y=40,label=C
,parse=T)+
N8 L' ]1 {( z8 S; h
coord_flip()+
3 E1 b# c( |; B
transition_manual(frames=as.Date(Date)) +
# w. I1 }6 e& f) Y2 z" ^. s/ }1 ^
#ggdark::dark_theme_bw() + #设置黑色主题
, i0 @7 l2 J" c
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
* ^( }8 z$ ?& v" U& t4 u4 d Q
ease_aes('linear')
1 y, j5 Y7 }7 j: K$ ^ L
ps
5 E) v1 o7 R/ p6 e
结果展示
) \+ f7 `1 n8 C. t
8 R$ K9 `! Y! F' g/ c
( \& t3 \. _6 g. a6 T' M
视频格式转化,加载BGM
6 d G( B# \8 K2 R0 W3 Y
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
( G5 T' [, H$ z# K9 m
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
* {. v+ Z* `, s! y8 t* e% W
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
/ v; V- y; B% W" f
全国新冠状肺炎26天增长状况
1 Y0 A. O; b+ Y5 r9 V' d) u
P9 x' o, {5 n6 w6 n
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
" o9 H7 I. |. j& d* _# Y8 w- c
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
6 q3 A5 u& R( ^9 C7 U
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
; N: v3 v1 o" |$ Z8 e
theme(legend.position="none") +
1 }! X' ?* _# D9 }# r
theme(panel.background=element_rect(fill='transparent'))+
8 \1 a2 }6 j$ |2 J
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
) t" T: k9 ?. A9 R
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
# ]0 w5 @% n) w) f8 Y1 P3 Q3 x' w
theme(panel.grid =element_blank()) + ## 删去网格线
( p, g* o3 N+ Y, }7 h
theme(axis.text = element_blank()) + ## 删去所有刻度标签
: g; X; y5 a, q/ R1 z# A
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
) D" h8 C# a2 `
#scale_x_continuous(limits = c(0,6))+
6 Z2 F# G( C O$ C0 U4 d1 j& b
# Here comes the gganimate specific bits
1 |% E' ?0 `7 b! ~! A# b
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
% O+ H% i& m/ x$ C( O) z6 b+ Q8 t
#annotate("text",x=0,y=40,label=C
,parse=T)+
/ U0 U6 m' b: w3 ]
# coord_flip()+
7 P+ t9 o% X _" j
transition_manual(frames=as.Date(Date)) +
( ?& e+ w# G- i4 H( G8 x
# ggdark::dark_theme_bw() + #设置黑色主题
3 `- n/ \1 _% Z5 k) V7 ^" U- i6 A
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
1 G0 Q: J# G1 G; L
ease_aes('linear')
' |/ _ ]5 T# j: c, P
pc
$ w5 w/ g# D; S, F7 D0 r
. H8 t) [/ r( R; |3 y3 Y8 o
8 U2 d6 [( ~2 o' B% U2 P
动态图合并
5 A" T$ a- L4 Y# p
library(magick)
; ~7 f- D; @4 k- L# `( [" k
ps_gif <- animate(ps,,width = 720, height = 480)
. k x+ _9 X" @6 }
pc_gif <- animate(pc, width = 360, height = 480)
, E6 \# u% x- s8 `/ G% }% e
ps_gifs <- image_read(ps_gif)
) ?4 v0 E9 @; S/ A
pc_gifs <- image_read(pc_gif)
9 V) t- F" v e& D1 T$ F7 {
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
: R* _; ~6 c1 x% k& `( \& w
for(i in 2:length(pc_gifs)){
' q, g" b9 _- m( j7 O1 @
combined <- image_append(c(pc_gifs
, ps_gifs
))
2 j2 d2 Q! g& M* i) v1 `$ I$ [
new_gif <- c(new_gif, combined)
0 j7 `' {0 R2 W$ \( u8 ~0 P% H5 z
}
( G! {2 a( C* f$ v$ Z
new_gif
0 Q' z+ i9 s$ V" Z
结果展示
+ T2 B/ C+ R5 k* i
. u* {( z5 i9 y+ i( l$ b
2 H- i/ l- L. W% z3 ?
; [; L9 Z2 {5 H1 S1 s
作者:
sjlxdn
时间:
2021-10-29 12:57
111111111111
. n3 e& ~2 _! f) M3 ?+ O' ?1 H* x# T
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5