- 在线时间
- 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学习
8 p6 u, ^" y5 F( k# \所需加载包
9 `% k. u2 U3 u! o0 \' X$ Plibrary(av) v: v! n# T" Y8 d. a( F
library(ggplot2)& r# f! o" ~1 g0 ^8 u* ]7 f
library(gganimate)1 I' ^# n* a+ b5 f
library(tidyverse)
8 u) K m0 Y0 [- J% Ylibrary(lubridate)
. v" l' |2 d8 i/ F4 @7 m7 X9 Xlibrary(scales)
9 F4 k9 f1 L4 glibrary(ggrepel)
2 U- t# G j& M( h6 ?; Ilibrary(cowplot)4 B U* I) O! Q
数据) D7 b! Q0 {7 f" ?3 u! x
7 m) {' _2 i" f$ L: U5 S- u![]()
9 ~8 Z* D S, P7 T3 { O5 @ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +4 Y4 V9 E: h2 ^6 q" D
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +4 A0 I8 E- y9 {- ?1 z- i- N
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
5 n8 a1 v! @7 H6 I$ I8 E9 A #theme_bw()+
- A4 Q2 Y$ I4 g. F2 p #theme(legend.position="none") +( k( [: g/ W8 e" [
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +) V7 h% |) ?% ]8 A% I$ Q
theme(panel.background=element_rect(fill='transparent'))+
- E' _4 T/ @4 Q) S L4 R theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+1 I: C/ e( Y- q
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
6 L8 Y, D6 z9 L6 L theme(panel.grid =element_blank()) + ## 删去网格线8 L6 H9 R" B( u0 y. ?/ @3 [
theme(axis.text = element_blank()) + ## 删去所有刻度标签
" j- k( I" p8 p4 W theme(axis.ticks = element_blank()) + ## 删去所有刻度线$ F! |6 }% ^% M6 w* d. z
# Here comes the gganimate specific bits: n$ l5 f! n- C0 W% Y
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
9 q9 ~3 H4 z3 y2 M#annotate("text",x=0,y=40,label=C,parse=T)+. ~' O0 c5 s4 p
coord_flip()+
) u5 @- f# g2 y* G" @4 P) k3 ptransition_manual(frames=as.Date(Date)) +
% s- m8 d3 y, d$ q1 _1 l#ggdark::dark_theme_bw() + #设置黑色主题" j3 P3 B- s0 X
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
D4 M; O" x! k5 x- aease_aes('linear')& x# _, ]- G7 U. E9 ~! c
ps! Y$ g% ^( [0 u% ? }! A! t; w( e
结果展示
8 o5 O# B; W! I# L6 M, _
6 i. U/ ?- h/ Z g![]()
9 j/ J4 Y: G& m: X% j( n视频格式转化,加载BGM. q) H* @* m# W1 m
#df <- animate(ps, renderer = av_renderer('animation.mp4'), - K" `) l# m$ T$ {& w! }0 C
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
/ N6 n; W/ N, w* A: d. E8 x# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3"): v9 k6 ^+ p3 ~, I9 K% y) [4 F: C
全国新冠状肺炎26天增长状况! ]' j9 J/ Z, a" e w. Q
# Y0 B) r$ z$ }2 P. s& F. L Ypc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+( w+ D! ^ ?4 d: e! V7 r
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +! g/ @% Y' h: Z6 }0 J
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
, h4 g" ?2 G% W$ D: E$ Q theme(legend.position="none") +
l( X/ S1 ]. @& H theme(panel.background=element_rect(fill='transparent'))+
9 t7 b3 t6 g: ?3 J) X0 ~ theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+6 C8 s2 x7 q, K4 P( J* U$ |
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
6 h, L, b* F3 _( J7 d7 w theme(panel.grid =element_blank()) + ## 删去网格线
. q- q. |# a6 u! X0 c& V" V theme(axis.text = element_blank()) + ## 删去所有刻度标签; e$ s( E1 `- S1 x
theme(axis.ticks = element_blank()) + ## 删去所有刻度线- w; i# ~- ~' ^
#scale_x_continuous(limits = c(0,6))+' L3 l" w2 _/ t% T% J
# Here comes the gganimate specific bits
% V0 t& {7 ?3 T/ w1 M% Z% M' X/ r# Y, g#labs(title = '日期:', x = '省份', y = '累计确诊病例') +" \+ y* e( b9 P0 x5 \3 A% P" T
#annotate("text",x=0,y=40,label=C,parse=T)+
' `( c. h7 R# ^& v& Q # coord_flip()+: g" @' n0 l \6 X) a* _+ V I
transition_manual(frames=as.Date(Date)) +
% i9 N2 o% w6 `" |1 C9 | # ggdark::dark_theme_bw() + #设置黑色主题
& D/ U, s9 K2 E! L labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+; F! E# I$ _# M u" |" B5 c
ease_aes('linear'). G* h, O7 U# {/ k; ]) h
pc; H% ~! ?9 i8 F2 {% K
![]()
/ @" h" w8 _) e; T; e X$ O
; p1 G2 l L8 ]: P动态图合并
( R0 |( n# |# y' P5 zlibrary(magick)
8 S' b+ K! U0 ~$ w" I6 X$ ~1 T+ ips_gif <- animate(ps,,width = 720, height = 480)8 V2 z7 p: i6 v& j9 J5 ?" c5 F
pc_gif <- animate(pc, width = 360, height = 480)2 j( f* `1 X! e
ps_gifs <- image_read(ps_gif)
; J* S ]. p$ b% T, _2 ?, q7 fpc_gifs <- image_read(pc_gif)
/ f3 u# ~2 ?$ T7 N& z% [: ?new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
! m, }- q, l3 qfor(i in 2:length(pc_gifs)){
+ U0 v6 F7 {) f1 t combined <- image_append(c(pc_gifs, ps_gifs))8 R! f- E7 [: v
new_gif <- c(new_gif, combined)9 p. Y% ^ V6 W7 ~1 o8 ?
}1 n; W0 L0 J( v w9 v* ~" s! `* A b
new_gif
+ }3 b, Q! a- D: ~& M/ w" \- B结果展示1 T }1 [+ Q% v+ s
1 u! o" V8 B* E 6 q! l. k" ^/ I* J- ?
! R( y0 ~" I9 g* F# {* X$ u
|
zan
|