- 在线时间
- 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学习
: b1 N# t) l, a& e& a所需加载包. ^& k+ U0 M8 I; l6 _4 D. Z9 y
library(av)/ W9 U% U! @0 J
library(ggplot2)0 [% U; T2 ]0 O6 m
library(gganimate)
: }4 G0 @- W7 c. Alibrary(tidyverse)7 a% G4 w3 R7 _4 o; W
library(lubridate)8 r5 G% v& y/ P' _- ]! E
library(scales)' C7 a' B# } f
library(ggrepel)
( `$ R9 r0 N, r: K! wlibrary(cowplot)
1 C: [* q; i, ^" [5 r/ Y$ ~* Q+ Q( X数据
; v" n) U! I. F7 U; B) ]% V" ]0 G; B( n r
![]()
: z- V) r% e5 u3 O- _, yps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
, ~7 g/ p0 S- V) {0 O+ K7 G geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
* Q6 ^4 t i3 R1 F7 z2 ` R( [6 S geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
% E$ L3 d9 h6 j5 K5 H #theme_bw()+1 c+ M* q& @, u& }
#theme(legend.position="none") +
' v+ T3 }: t4 O7 Y$ B. V: \ theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +2 w0 d0 X- M' C5 M" s3 R
theme(panel.background=element_rect(fill='transparent'))+6 Q2 h/ \0 E3 E# l5 U
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
- u( P( ]7 `' v2 H; l theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
4 v# }: d _! W: ^' W/ { theme(panel.grid =element_blank()) + ## 删去网格线. l9 k2 P+ j( k3 r$ u7 n3 W. r/ T& m
theme(axis.text = element_blank()) + ## 删去所有刻度标签
3 V: e @6 X0 ~ O/ K2 L6 ~ theme(axis.ticks = element_blank()) + ## 删去所有刻度线
3 p: c. u$ G, {4 @ K( k# Here comes the gganimate specific bits
# h! o) ^4 y, A9 R9 j( v1 E$ |, u; k#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
( b2 n- U4 ]; ^: N3 C#annotate("text",x=0,y=40,label=C,parse=T)+) A* {$ V9 r% S! U
coord_flip()+
L& Y" _/ ?4 r, C! etransition_manual(frames=as.Date(Date)) +
- W3 t& z* a) V, v; [& o1 Q2 \#ggdark::dark_theme_bw() + #设置黑色主题
n: m9 p, T/ P$ o0 J4 a* glabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+. G9 A: L7 f0 s- ~# \
ease_aes('linear')
{6 M$ R, k8 J/ T; z1 \. d" hps' l7 \" f) K' l% X. \: R
结果展示1 m) E _& _1 y7 B( k2 [
+ o5 ~7 i1 X5 ]; a( i8 I" n
![]()
' \5 T8 R& F% \$ c3 B4 I视频格式转化,加载BGM
' J" u/ u! C- M#df <- animate(ps, renderer = av_renderer('animation.mp4'),
! i7 Z6 C7 d: V" H- ^ # width = 1280, height = 720, res = 100, fps = 10)#视频制作( ~ w. e" x' K1 [1 f1 K
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
) G! D t. s4 K8 W, O全国新冠状肺炎26天增长状况" t9 }% k! r6 @
. y( b1 H! |! J& M2 fpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
' v9 g ?+ @- E @8 W1 f3 u% P geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
$ l5 v- l* N8 ]' ] geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+7 \2 U: W$ I) ~; F
theme(legend.position="none") +
2 P4 b) \( J% G. ~, J" \ theme(panel.background=element_rect(fill='transparent'))+4 j( _; o5 w, o, O
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
0 G1 u9 h8 e. h; h0 L- ] theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+9 q2 `3 S' g8 z; J$ d! y
theme(panel.grid =element_blank()) + ## 删去网格线& |/ u( ^1 o5 \
theme(axis.text = element_blank()) + ## 删去所有刻度标签
2 y! H% e7 k7 h7 U! k theme(axis.ticks = element_blank()) + ## 删去所有刻度线
& r: c: b$ W' x, ^9 V9 j #scale_x_continuous(limits = c(0,6))+
& C( S, P7 L; _6 S; V* _6 _( s # Here comes the gganimate specific bits
; i6 n( z# h7 w( |#labs(title = '日期:', x = '省份', y = '累计确诊病例') +' s: ]& z# o- m) b+ C
#annotate("text",x=0,y=40,label=C,parse=T)+
0 ^2 m' G, I, o1 o # coord_flip()+6 H+ q: [) \7 v l$ c/ B
transition_manual(frames=as.Date(Date)) +
5 B" ]+ Z/ R1 A7 X# z( j # ggdark::dark_theme_bw() + #设置黑色主题, E f0 T6 x/ Q: Y6 ]
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+7 Z# ^2 X) A" N0 g8 |. A1 i
ease_aes('linear')% U; M/ b# `8 O Q: t! |$ O/ L
pc7 H# r' ?% L g: E9 a
![]()
* [$ G8 K) P1 p9 u c
5 D# N2 o9 j! i& f J6 M0 S& ^动态图合并
- ]! a) `$ u3 O, E" clibrary(magick). r2 |; ^# D I. N5 O" q3 @$ [
ps_gif <- animate(ps,,width = 720, height = 480); B$ q$ a, B; ?( w' L( d
pc_gif <- animate(pc, width = 360, height = 480)
; n- Y) `; N" z: Cps_gifs <- image_read(ps_gif)+ ^: }$ p) o' ]$ E) r
pc_gifs <- image_read(pc_gif)4 T7 m1 Y- Y6 a6 ~9 U+ C. A& p4 @
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))! e( k2 ^% V( B7 E' l2 [" `4 v2 D
for(i in 2:length(pc_gifs)){
6 p* {" f5 B9 ?- c combined <- image_append(c(pc_gifs, ps_gifs))
) b, n& U r/ [5 d8 Z; T9 [6 e% w3 ] new_gif <- c(new_gif, combined)1 R4 U/ j$ r' B4 x8 U
}: J" ?# B: `% c2 N
new_gif. L7 G5 t1 E7 W2 v0 d# H
结果展示) P0 c- O2 m. B; N3 l
( A) j* H' P1 S+ e# l 2 J8 n! J% }. E: e9 H: V& @
+ Q. i$ @/ V2 e
|
zan
|