数学建模社区-数学中国
标题:
R语言gganimate疫情数据动态可视化
[打印本页]
作者:
1047521767
时间:
2021-10-28 19:38
标题:
R语言gganimate疫情数据动态可视化
R语言gganimate学习
# b* J! j* h* B2 R. v/ P
所需加载包
9 S, x, M: W2 t0 [
library(av)
1 g/ U, }* u$ C# ]3 u6 t* }- m( c
library(ggplot2)
6 N6 z* V: Z7 r0 Z* z6 Q4 J9 |
library(gganimate)
7 M1 x9 d# {4 x. R
library(tidyverse)
* ]4 J% E6 x+ L8 U2 U2 z; c
library(lubridate)
" E. Y5 J1 z) n8 _ e
library(scales)
- B$ K r' k% m- w
library(ggrepel)
# Q* [* P% |: ^' A
library(cowplot)
& _3 V1 u9 X& ]! u: a5 U7 f
数据
# D P2 c# _/ k, v; ^; z- Y8 _
5 P# I7 t' Q- I: l& b; b* S
. A1 ^4 s* q8 l6 M
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
, v+ H* P# p& `% {% @: I
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
+ `. U- g; a, s( m. ^' E; Z
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
, j4 l* Q' c4 a" |
#theme_bw()+
* L6 ^7 ?6 z! F7 @; e: s% A; V
#theme(legend.position="none") +
v) I K9 H" `; _- \' Z% u
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
9 G# y# Q0 T8 P: S" G2 s% a0 n# J
theme(panel.background=element_rect(fill='transparent'))+
: g# _4 k% h9 {
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
" b t n' n0 O* H* }8 K
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
) b. k0 J8 _, q4 B, p
theme(panel.grid =element_blank()) + ## 删去网格线
% B& V( x2 `7 p% F
theme(axis.text = element_blank()) + ## 删去所有刻度标签
$ E. {' F/ J) P1 ~ b
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
# M) s' z" {* [
# Here comes the gganimate specific bits
0 w4 w6 D8 W+ o6 J$ V. f
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
, y/ E) O5 z7 M$ ?* J, n
#annotate("text",x=0,y=40,label=C
,parse=T)+
( v4 i2 ~: v9 h2 J) L Q8 r
coord_flip()+
# p0 C: F7 N L6 j$ Q
transition_manual(frames=as.Date(Date)) +
& T i7 q* E7 H1 M1 P1 q+ _
#ggdark::dark_theme_bw() + #设置黑色主题
+ i7 R2 b ?: ^
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
- c, m/ p( ?( `* O6 s. t7 v1 I
ease_aes('linear')
* S* _* S) l% M
ps
- |, [; z8 z0 {" W8 u0 N; Q! j$ \
结果展示
6 t8 s; a- l. V& D, B
# {; |( I/ L& R# n
% x% H, t" _8 d( k# L8 b3 |# i* k
视频格式转化,加载BGM
2 ?* E3 ?" @" ~* W
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
a/ z# s7 U, r4 h1 T+ Q! I& @, N
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
$ x: |2 ^& A) S9 U
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
! v1 X7 M1 C5 O1 W" b) v7 r6 O
全国新冠状肺炎26天增长状况
U( G% B" u1 N1 I* J) h! K
! d; j3 H) s! v6 Y& D3 h
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
7 P5 B$ W; ^ i! `- |
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
& G8 f, ]* o! m% E( _ R/ o
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
' ]+ H& l, s% w
theme(legend.position="none") +
F1 M# Z$ D# t! s# f
theme(panel.background=element_rect(fill='transparent'))+
* o, J& }6 K2 u4 K2 M$ V2 U0 Q
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
& O5 T \! E% @
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
! N: J! |% W( I9 j; n; s8 w
theme(panel.grid =element_blank()) + ## 删去网格线
* I7 V9 `! y) l7 `, f2 L
theme(axis.text = element_blank()) + ## 删去所有刻度标签
: @! C+ G' O5 V' }+ t
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
9 B' O: A" W6 K1 @9 @6 \
#scale_x_continuous(limits = c(0,6))+
+ P# w! V I: e4 P# o. p. ]
# Here comes the gganimate specific bits
, I; x c& E8 [
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
* l" S" @" q; O8 h5 e. ]5 n7 I
#annotate("text",x=0,y=40,label=C
,parse=T)+
# S; d. L+ ^* ?3 C F; @# b; j4 n
# coord_flip()+
: y$ ]! X* H7 p/ C, a! l
transition_manual(frames=as.Date(Date)) +
1 f4 i# ?: R2 S
# ggdark::dark_theme_bw() + #设置黑色主题
( ^9 p4 S# V( G Y
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
) @' E5 A7 e! O
ease_aes('linear')
) u+ w9 F U: |; O: \* U
pc
5 m9 |1 m4 r; ]/ l, B, O
: d! E: ]% \9 B p: A9 G
& \ E! b# X! }$ Q6 x+ O- q
动态图合并
: b/ T; v/ w- `
library(magick)
0 w* A0 j! M: V; f; u3 x F' I
ps_gif <- animate(ps,,width = 720, height = 480)
Q T) ~' V; ^7 z5 K
pc_gif <- animate(pc, width = 360, height = 480)
' r+ U* L' R& |$ u
ps_gifs <- image_read(ps_gif)
$ }" m% X' l/ ^! ^/ R6 ]
pc_gifs <- image_read(pc_gif)
K' i7 R# B9 F! H! C5 A: V" p
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
8 B) n* O g8 r" u1 L) Y5 t6 E2 G
for(i in 2:length(pc_gifs)){
' g7 f! e* ^6 T+ Q" m. f
combined <- image_append(c(pc_gifs
, ps_gifs
))
* l- U, K o# T+ f, i
new_gif <- c(new_gif, combined)
2 \! k* N; V4 e
}
1 g* `" Y6 U1 R# w! f
new_gif
) \5 H( G) D: C3 X0 q
结果展示
* \1 m8 V; z$ L( H, U" Q
1 J5 \( h6 P: M n% w J% y
/ Y1 B" h$ c( M+ O1 n
; c4 B! t- c5 x! V* N* U/ o
作者:
sjlxdn
时间:
2021-10-29 12:57
111111111111
* m7 C; I2 ` y7 T, ]1 ~) Q
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5