- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 39951 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12696
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习% |& y, B e* i
所需加载包% k& e/ M6 x* i6 C5 |/ j, M
library(av) h% U3 I8 l1 E! |; i
library(ggplot2)
1 \' f; v4 ]5 v0 V7 f5 _" Z( G+ r5 Ulibrary(gganimate)
$ g8 a' o& e9 Wlibrary(tidyverse)
t H" H4 W: z& R8 k3 glibrary(lubridate)
; s4 A! k+ r! _4 r. Zlibrary(scales)
% M% S5 @8 k6 i8 u- p$ clibrary(ggrepel)$ J3 [# O, q" v M5 g
library(cowplot)' `: { q( y& m; a. s
数据) B/ @$ O" y- r5 b9 t
7 O! D* R* C% b9 y* S) a: q+ N
![]()
! E; H- ]6 w/ V" c" u! ips = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +; c1 @+ `- b' u
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
% V+ R" S" e' j( v% x geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+( u7 s) e* C8 O( Z2 E9 n
#theme_bw()+
1 S* k4 i h) A2 J #theme(legend.position="none") +
0 h e$ C# g# x7 s( H" ]1 E3 X2 l theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
7 M' O; q1 E4 U1 \* I' m theme(panel.background=element_rect(fill='transparent'))+9 U5 o/ p( J: W5 a
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+0 g2 e- b4 J, b- r4 V* [! y# E
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
! a. V) ?+ E3 m9 ~/ x theme(panel.grid =element_blank()) + ## 删去网格线2 n7 j9 E$ [7 `+ x. u2 M
theme(axis.text = element_blank()) + ## 删去所有刻度标签( D# t g% j7 n w
theme(axis.ticks = element_blank()) + ## 删去所有刻度线. A5 U; K7 |8 z9 ~0 }+ G. K W. Z
# Here comes the gganimate specific bits
( X$ O8 x- y0 c0 t#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
2 z C" [8 |2 C#annotate("text",x=0,y=40,label=C,parse=T)+
$ F) \; f }( P8 v/ ~" r7 N3 qcoord_flip()+' A" U' F6 X/ U; G |6 d
transition_manual(frames=as.Date(Date)) +
# q* K: S8 a3 B+ L3 ?5 Q' d9 X& {#ggdark::dark_theme_bw() + #设置黑色主题' G% ?/ t5 s# X
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
% {1 k5 k$ r3 _0 n' {' Eease_aes('linear')
g( g- V( q) V4 C% Kps
' Q+ h' i* {8 k# G5 B结果展示+ N+ e6 G, q! `8 B; n( x1 f
9 o- E& i# e6 C) }2 e. J
" F5 a/ T- [- u2 T. Y8 s
视频格式转化,加载BGM5 ^6 T( V, Y: U3 y2 P# |
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
* r: h$ o& i8 H # width = 1280, height = 720, res = 100, fps = 10)#视频制作
, ]# C `' H% n9 Q$ y# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
/ O( G7 G- P# q; Q全国新冠状肺炎26天增长状况
5 g8 x7 J L/ h9 N0 D; l
6 Z! b, N+ V. O& k) l! wpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
; n z7 y2 \4 `" S7 I geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +/ `8 V3 X1 K1 H8 T3 p
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
8 Y' ~7 H6 i" I& S6 M% i theme(legend.position="none") +: C, C- z: t% ]2 C) V/ Z. t
theme(panel.background=element_rect(fill='transparent'))+
: U& C7 w/ `/ v/ S. ], v* e& G theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
+ R% R/ U* C, o6 z/ O$ ^ theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+! D! y( |2 q$ U3 s, N- A
theme(panel.grid =element_blank()) + ## 删去网格线: b% a: F% y1 w
theme(axis.text = element_blank()) + ## 删去所有刻度标签
- d. U5 d& R" L1 s" g$ } theme(axis.ticks = element_blank()) + ## 删去所有刻度线
* D. z5 [& ]8 s& q" J* n0 W #scale_x_continuous(limits = c(0,6))+" ?! V0 V; O, C& q5 s
# Here comes the gganimate specific bits
! P5 Z& O& [1 L: p) K# B& p#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
, F+ Y4 s( r: q' |3 K! `# ^. ?+ s0 C#annotate("text",x=0,y=40,label=C,parse=T)+
& y0 O4 S5 A& M$ _ # coord_flip()+7 U1 B6 ?+ B o0 [% R
transition_manual(frames=as.Date(Date)) +
/ [( X$ P( D' z" E" g # ggdark::dark_theme_bw() + #设置黑色主题
& f/ L6 ~! v m. X1 A- W( g labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
. T5 J! _3 Z. ], X ease_aes('linear')# w# n1 G9 C# L5 J% t3 i/ ~
pc& d4 G* U8 c. A1 ` n" b
![]()
( h8 y, K: t. r- `# Q3 f. ~) ~& i3 i( |& m- R5 W. b6 y3 R1 r( {
动态图合并
u1 R8 r9 j4 z# O& ]& nlibrary(magick)
' f3 M# A' ~ H8 tps_gif <- animate(ps,,width = 720, height = 480)) `) s! @: }& u, n) O
pc_gif <- animate(pc, width = 360, height = 480)
6 v7 J5 [& X# |; \ps_gifs <- image_read(ps_gif)/ T% {# f4 E7 g$ U
pc_gifs <- image_read(pc_gif)
$ y! K( q7 m5 T" M6 hnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
$ a! H) H2 U6 W8 Hfor(i in 2:length(pc_gifs)){/ c, M. c: U; \4 h% B
combined <- image_append(c(pc_gifs, ps_gifs)); p; Y a* {# T0 H0 M# ^
new_gif <- c(new_gif, combined)
6 s1 l6 c3 S: Q2 `! q5 W}
$ z, ?7 x% d I: `new_gif
4 A. |& f1 V" g6 W结果展示
) u( C* _5 ~3 f5 {2 t* S
% z5 c' Q q. j: _ F) s![]()
1 y0 U- C; O; `, Y9 Y, C
+ M- Q X" B2 \0 b' v |
zan
|