- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40219 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12777
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
5 d: }: h# B6 \! J5 Q) h. T$ o所需加载包
y9 v* f$ G1 G8 N l. glibrary(av)
1 L- \# U3 f/ P* ]+ F% H7 F7 Wlibrary(ggplot2)
3 a/ ~# t, a) L5 M$ ~% n/ ^5 R$ ]library(gganimate)
/ d {! D$ i5 N0 x1 u# h8 Z4 w3 Ulibrary(tidyverse)
1 J) O% U2 p9 dlibrary(lubridate)
+ E4 y9 t0 ?3 `$ a: Z, Slibrary(scales)
+ Y( {6 L9 ]; P. N3 Klibrary(ggrepel)
0 `: ~% G: Q* W. flibrary(cowplot)5 e* q b: o, [+ P9 e8 r
数据: Z% N- h- s3 x" }$ _5 ~8 y
6 ~8 }. N4 C4 {/ `+ D( e![]()
$ q9 y& I) ^7 R8 Y# }ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) + Y% D* a+ L/ h9 S" @
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +( `4 R d* M1 S6 ^: f) h ^! m
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
0 j7 L9 W' x# q2 }8 z0 d #theme_bw()+6 j$ h# s( m# N8 N2 q
#theme(legend.position="none") +$ q9 a/ B4 R% |6 k! n
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +% l) D3 R4 F# z( E. M% Y6 ^# ~: P
theme(panel.background=element_rect(fill='transparent'))+
" a; l$ g; s$ ]" p' _# [2 Q5 I theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+2 G5 G& \1 f2 B5 q
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
1 ?& _& x8 i) r5 ` theme(panel.grid =element_blank()) + ## 删去网格线
: B2 W2 O- \( c& q$ {2 V theme(axis.text = element_blank()) + ## 删去所有刻度标签
D" B2 f" f p( c5 o theme(axis.ticks = element_blank()) + ## 删去所有刻度线; b3 Z, t- y4 S4 y" T
# Here comes the gganimate specific bits5 z7 O+ C+ A4 y3 s3 ~% m! x
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
( w0 b' I3 k% ^" _; c9 E#annotate("text",x=0,y=40,label=C,parse=T)+
5 R# z- V! S/ X! ~coord_flip()+! J( g( k) u8 m l# H9 F9 m" U. C
transition_manual(frames=as.Date(Date)) +8 D% g# W" G) S4 b5 l4 z
#ggdark::dark_theme_bw() + #设置黑色主题5 x$ g* P- o5 p
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+& K1 \' n, i1 d6 Z1 W
ease_aes('linear') C; W8 ^6 ?$ W+ t4 J& j( ] j
ps6 K+ u7 u5 ]( L0 O9 k% @
结果展示2 U% u1 z. d' s% @/ r- J! b7 {( d+ Y B
( G# z" n4 u9 A ( \" Z- c. C: P
视频格式转化,加载BGM+ V0 `- `! U' S+ m7 M
#df <- animate(ps, renderer = av_renderer('animation.mp4'), & A6 D$ ^$ K, i7 n/ T2 O
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
3 i( |; r9 r6 m! |4 C# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3"), @- F9 y: Y# t: K& Z
全国新冠状肺炎26天增长状况
6 @6 B7 `; ^/ D; ]8 c! _( r' d" S8 e+ t' X
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
5 w" @7 t1 z$ F. o) D. ~; D geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +# ^5 L, L2 A' u* O7 {6 ~5 j% r
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
) g/ l5 k1 T% \( \0 \) ]9 | theme(legend.position="none") +
, P! C) g5 o9 G. R( B theme(panel.background=element_rect(fill='transparent'))+: i) D# `! L+ Q
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
, n0 H% o P9 F4 |6 T) w theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+, `" e) s5 s" f0 S8 l5 _9 F2 z
theme(panel.grid =element_blank()) + ## 删去网格线
- E4 v/ i9 y0 U- i. N) y theme(axis.text = element_blank()) + ## 删去所有刻度标签
9 G* g& A: h2 i0 P S! y theme(axis.ticks = element_blank()) + ## 删去所有刻度线
# P( M& F; u# d3 Q) z/ p4 g #scale_x_continuous(limits = c(0,6))+4 X+ m; r% y; P+ n3 o, a4 [4 I
# Here comes the gganimate specific bits
. c: ~9 P7 M1 r#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
' ?4 ?0 i$ z0 C1 L( @7 y#annotate("text",x=0,y=40,label=C,parse=T)+
/ F! ~' l+ n7 R3 z& u9 j- \ # coord_flip()+
5 o g% G. w: o- c& L/ i' O transition_manual(frames=as.Date(Date)) +1 c. K9 P3 @8 q
# ggdark::dark_theme_bw() + #设置黑色主题: [) a1 r* j# d) i5 r7 F
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+9 i5 w. L. q" W0 t
ease_aes('linear')
6 C9 e9 ^5 h; u" p2 cpc, |' C0 v4 k1 b0 s8 P3 W( z1 @0 T
![]()
( w& U, D4 w0 f F' o* R/ ~3 K1 ~
& k; Y9 F+ ]& \7 B8 l& ]* q动态图合并
, }5 e4 y& _5 V- \4 vlibrary(magick)
% D: l# e2 m1 f# r1 U. V* ~ps_gif <- animate(ps,,width = 720, height = 480)0 j1 x+ ]1 J. @ J+ W6 ^4 D/ t; d
pc_gif <- animate(pc, width = 360, height = 480)$ f; W/ \9 _ v# _* r% s+ z. I
ps_gifs <- image_read(ps_gif)
" v, ~: @1 A% C2 X$ @pc_gifs <- image_read(pc_gif)
4 u7 z+ H( o( e# L2 bnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))1 q# C2 _8 V+ A/ p2 @9 ^
for(i in 2:length(pc_gifs)){
: }* n7 Z( V. v Z, x combined <- image_append(c(pc_gifs, ps_gifs)). x5 K! d/ s7 H' n2 ]- j u% z
new_gif <- c(new_gif, combined)
6 r4 Z4 E {9 {6 n5 i9 @}
T* |( Q( B/ e: L3 Wnew_gif" w9 ]% }# d4 g$ z5 k1 S: b
结果展示
3 c4 R+ X% p3 K+ C% E: L) n8 c" h0 V% H) f" q% F
![]()
, W" h1 W' `: `! u- ^8 X6 t2 n! l6 |1 t8 V
|
zan
|