数学建模社区-数学中国
标题:
R语言gganimate疫情数据动态可视化
[打印本页]
作者:
1047521767
时间:
2021-10-28 19:38
标题:
R语言gganimate疫情数据动态可视化
R语言gganimate学习
( A( y% F5 z5 |3 O1 W% w/ A8 k
所需加载包
1 b( L9 q9 a$ X# A! \
library(av)
; V7 S l# R0 u6 U% R
library(ggplot2)
- I1 K! O8 h/ |( ^# l5 o
library(gganimate)
" J' b1 R" ?1 R2 ~5 ^: y' Q
library(tidyverse)
# j2 { I l3 u* [' ?
library(lubridate)
+ D# B k0 m3 V- m* J
library(scales)
4 ~* m, q& s& W8 k& y; A
library(ggrepel)
+ v" a: z% y! r( D( Z7 S
library(cowplot)
3 b: n0 Q4 C/ x1 \' ?
数据
6 ]% [1 \+ b. ?" T& g4 e
! R2 v& ?$ n" |
! U q1 X1 U7 q2 h
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
( C9 i' o& s+ `( E
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
. G R) R2 o9 p, h
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
% ?7 Q. \$ E5 l! s! u
#theme_bw()+
v+ f6 y+ O' l
#theme(legend.position="none") +
* o0 u0 t% v: \" C% l. K( d2 e& `6 Y
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
4 H+ r8 J7 T% Q7 W" B
theme(panel.background=element_rect(fill='transparent'))+
+ ~: Z& j) O/ z6 g5 U* X
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
( \$ n* z8 {$ w6 L
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
1 j; w" F: |6 m' `, S/ ]( y
theme(panel.grid =element_blank()) + ## 删去网格线
7 a- C" M/ t, O. e6 d* w/ G6 f
theme(axis.text = element_blank()) + ## 删去所有刻度标签
% Z2 [- x7 {% o8 w# T
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
2 u% f3 p6 S# t b6 I1 C4 r
# Here comes the gganimate specific bits
/ p+ q5 f' `. C. r" ?* O7 y
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
1 i* O! P; ?/ I0 j' _
#annotate("text",x=0,y=40,label=C
,parse=T)+
$ f H% ^' O! x! \1 p( P# A9 B
coord_flip()+
4 l; l$ X3 A5 [
transition_manual(frames=as.Date(Date)) +
( v) j+ m: C: o3 u s
#ggdark::dark_theme_bw() + #设置黑色主题
( n m& H' Y1 W( V% b
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
2 t+ D: Q" p! q6 z8 N
ease_aes('linear')
, {5 @8 F' U/ b8 ^. }, [6 p* ]
ps
; K" X2 A" X8 M% H1 I4 B
结果展示
- o' H3 v# p7 v; s1 m+ x" R
M0 e3 g8 X5 n9 t4 k4 o0 _
2 w/ V0 W" z- J) n6 r
视频格式转化,加载BGM
1 ^" _9 X2 L3 G! M
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
, B8 j) F% |0 R# W) S+ N! q
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
% e" _/ L% V4 k }. B; P
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
, S' X* R' [' T; D
全国新冠状肺炎26天增长状况
, {" ?+ e1 R0 u2 f; @
* z+ O2 i: N9 i. M" Y( n' }
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
1 h% C! a8 q/ f' D/ a
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
0 M5 L0 M1 a0 s# Q8 {- z
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
- K$ X* A& |1 c2 j
theme(legend.position="none") +
+ h+ K. ?/ O+ M
theme(panel.background=element_rect(fill='transparent'))+
# y% j- {9 m* E4 g8 z* u
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
/ i8 v8 z# A6 f% p7 I% n' C
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
7 E8 v! G( c5 B3 ^ D; d2 a+ Z
theme(panel.grid =element_blank()) + ## 删去网格线
0 A& W+ o0 k( i+ S- l
theme(axis.text = element_blank()) + ## 删去所有刻度标签
( S4 D" P; F7 p! a* T
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
' _- O# |0 k* i, Z% `
#scale_x_continuous(limits = c(0,6))+
) P' p0 s, k! y+ R0 V. u
# Here comes the gganimate specific bits
: d6 R p# w2 h. y2 F! l7 _
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
, L4 T; O# v6 ~0 Z
#annotate("text",x=0,y=40,label=C
,parse=T)+
* D, @7 A& ]! \- Q1 t
# coord_flip()+
# o# X& ^' s8 x! u# p% U& U
transition_manual(frames=as.Date(Date)) +
4 S4 Y% I8 k, w! ~) O8 A
# ggdark::dark_theme_bw() + #设置黑色主题
/ S8 V. m& v1 L9 X* V3 @
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
0 D( `1 B! q/ A1 u& G
ease_aes('linear')
5 {# {: U( ^# E% {7 ?
pc
. l# [ _& m: B' m: W
; o, P! f4 ~# x! U" l
L+ v7 H! E1 v) Y6 A
动态图合并
6 ^* Z# L7 y$ s! _0 q; I* [
library(magick)
* e; v: j/ W5 S, L6 w
ps_gif <- animate(ps,,width = 720, height = 480)
( h7 ?( H5 n% Z0 P
pc_gif <- animate(pc, width = 360, height = 480)
" o: ~9 @" e9 G9 o
ps_gifs <- image_read(ps_gif)
7 d5 u* l) _6 {( h! r
pc_gifs <- image_read(pc_gif)
4 J- W2 i- H1 S# \8 {8 G
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
+ @2 L7 d2 M# u. e& e1 }
for(i in 2:length(pc_gifs)){
7 y+ _" L$ X" X; B6 j8 I Y
combined <- image_append(c(pc_gifs
, ps_gifs
))
$ m! E) n0 B# k M! d
new_gif <- c(new_gif, combined)
0 O2 v, i# d4 h. r# I2 E
}
/ L7 P! d% u$ l5 k5 S6 Z" [; ~
new_gif
( z, y# i- Y7 g/ U5 @- U
结果展示
. d# M( L# T& P- l. _' e$ {3 v: U
: A7 l: Y7 [8 Z; z) }: Q
5 @; J g& e; w: q
* D1 o0 Z* w# z
作者:
sjlxdn
时间:
2021-10-29 12:57
111111111111
# l' M( X6 |& ~$ I R2 f
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5