- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40100 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12741
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习/ B0 T( n3 v( n" z& o
所需加载包
( e$ A! e1 u" ?library(av)' O, Z) p) i" b! F9 U. W1 @( _
library(ggplot2), l5 H+ w5 K9 h' d; @9 E, M
library(gganimate)
5 L. e3 S0 Y' n) l2 Hlibrary(tidyverse)
9 \3 P! [1 R7 U; ]library(lubridate)* e' z5 J" r9 N2 _6 T P+ e5 w
library(scales)
7 u/ a h2 d' G6 flibrary(ggrepel)
+ b ?' p4 J2 D" ^7 y: ^- v$ t2 Alibrary(cowplot)
% b% k F6 P7 n% j数据
' ]! Y( M6 j8 X: w! c
: x. v: Q1 J4 @8 ]/ t9 K6 }" Z' F![]()
: r( U3 d1 h+ D3 k) t4 u! i0 Y3 Vps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +) n' x6 ^1 f! M# n
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
$ s9 m) S+ {3 q# B( V* I geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
) r9 J( z% _! } #theme_bw()+
% d& S+ l8 |5 y1 P #theme(legend.position="none") +* W, m( @9 }9 f; P( Q+ r. `
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") ++ t- Y% _! T- m9 R, u
theme(panel.background=element_rect(fill='transparent'))+
3 D2 _2 P' n: ^1 D& z) @0 t theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+. ]6 P. t( N( Z7 ]
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+$ S- M7 g6 D5 y$ C
theme(panel.grid =element_blank()) + ## 删去网格线8 ?8 L/ L% |5 L h, D
theme(axis.text = element_blank()) + ## 删去所有刻度标签
7 M J1 V W- F/ a, [! e theme(axis.ticks = element_blank()) + ## 删去所有刻度线
' a5 E" T. B) Q6 Z3 @) o# Here comes the gganimate specific bits! @: X+ q2 K5 w8 w
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +6 }! V* _( Q* W2 }
#annotate("text",x=0,y=40,label=C,parse=T)+" A& `$ B2 U+ K4 @) s6 G# N
coord_flip()+" ~) p8 R/ I* e
transition_manual(frames=as.Date(Date)) +
$ w0 t( b* c; T) T9 [#ggdark::dark_theme_bw() + #设置黑色主题1 n# u! Q( x9 O+ Q) b' m
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
# F6 ~0 Q* W, i9 Qease_aes('linear'), P; J6 y9 }: ?& T8 k
ps
! N% z/ |+ @& l$ b结果展示: |2 j2 E! G* P
4 A" ]' F1 f$ M% V2 D+ a9 p- X![]()
+ \0 {8 z% f# Y# X视频格式转化,加载BGM# k) a" n7 e0 r( N( m& u7 ?
#df <- animate(ps, renderer = av_renderer('animation.mp4'), b" q# Q! `. R* A( V/ \5 j
# width = 1280, height = 720, res = 100, fps = 10)#视频制作; W2 U/ p4 q! ~8 @, L' c' P
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
) N. `, u) W. O! S全国新冠状肺炎26天增长状况
6 O$ }% |! h+ k8 W
/ F& H4 U- b& [1 b+ _+ w! apc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+- i2 a$ M& O7 r& n* L8 Q4 l& @
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +6 m6 m1 p2 {# \: u" l0 _3 c/ w
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
6 s7 Q2 E8 @. K0 T3 o theme(legend.position="none") +
) Z+ _. _ ]* d" b: R! v, W5 R theme(panel.background=element_rect(fill='transparent'))+
7 b& A% C: |6 u theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
; g! c3 b9 s$ N* q5 e( \, k theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
; O% C4 a" s5 R ~! y H1 U" q6 | theme(panel.grid =element_blank()) + ## 删去网格线
" J# P$ C5 O5 N" [! | theme(axis.text = element_blank()) + ## 删去所有刻度标签% @3 E# ?( ~" w7 c$ I1 q. @
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
$ B, A, s* \- z4 m: v" ^0 ~0 p- } #scale_x_continuous(limits = c(0,6))+4 x" ~2 a: c, K+ a
# Here comes the gganimate specific bits) L* B1 K# ?" A9 _
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
0 v0 K: O( r! t& h: }" k#annotate("text",x=0,y=40,label=C,parse=T)+
* X5 l. v* Y }/ Z J. y0 l. a # coord_flip()+
3 n. f/ Y( O* \) R# w transition_manual(frames=as.Date(Date)) +
7 q: ?7 W7 s5 M ]& S # ggdark::dark_theme_bw() + #设置黑色主题0 @8 }4 Y3 s! g- l
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
/ j' f1 S/ B4 N# y ease_aes('linear')1 `7 c3 d0 t( c5 E5 O# ?+ O1 J
pc% }- h1 w: k. f6 b0 l3 ?
![]()
6 N: Z' C& ^* d" R" D) P, v( d6 l+ N7 r+ j
动态图合并7 E+ u3 { J' V, K
library(magick)' z/ s+ L9 N, K/ r
ps_gif <- animate(ps,,width = 720, height = 480)
* l' e) R8 V8 L! dpc_gif <- animate(pc, width = 360, height = 480)3 h) M1 o3 H% T' T) d
ps_gifs <- image_read(ps_gif)! \, O% k; f3 E( K* \3 f" j
pc_gifs <- image_read(pc_gif)
! u% G/ P, S. pnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
/ ^' j- u% |1 Tfor(i in 2:length(pc_gifs)){
, M# y1 y6 [3 k# P2 e; l* O x combined <- image_append(c(pc_gifs, ps_gifs))" {; e6 N8 z! o: c: d& R6 j7 p
new_gif <- c(new_gif, combined)( L Y0 Y$ y. l, `, h" i
}
# R i& c0 n3 {& m5 nnew_gif
) p: T. H; x, {: S+ L& D0 R结果展示
. s& s4 w5 @1 X" g. i: u, B G/ W; E4 [4 j2 x) v
![]()
0 l6 m1 m/ ]3 j: A2 V5 S, s4 {8 b; w8 k) g
|
zan
|