- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40245 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12785
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习6 x% E' o; @/ N% X
所需加载包
1 q" @: W; t3 f3 i, o0 Ulibrary(av)
+ E( b4 b% Z% g4 t3 m$ ^, wlibrary(ggplot2)
$ C4 T7 m# x" [, Klibrary(gganimate)0 i* H4 t6 G& v
library(tidyverse)
/ |$ c$ }& h- V" ]7 `library(lubridate)
: L+ \, d$ a8 V: C- h5 { p8 glibrary(scales)0 P' d3 @3 L }* K
library(ggrepel)
! f3 o d- G+ m/ s7 J, s" B! {7 c! K; ulibrary(cowplot)& l' e9 [% P( S
数据# t+ _# m7 Z" P& J {3 v$ m
" ^6 {3 T: i" E0 m
![]()
% U9 N/ s0 x8 _, b$ Zps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
4 Z. S* M: \. }5 ?% Q4 l- l geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
9 O. r& K4 ~4 g ?! W geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
1 r4 s' Q$ E; `) H F #theme_bw()+
( q# N t, |* o; [9 s$ s! g #theme(legend.position="none") +
2 q8 N3 d8 S0 T! \; e+ y n4 U4 d theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
/ }, x8 s' p6 r* }( q9 R6 { theme(panel.background=element_rect(fill='transparent'))+, e5 I4 Y- }( o' Q z+ {
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+3 r) s) ^5 \( I7 X I) g
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+, N8 |6 N* k% r5 Z
theme(panel.grid =element_blank()) + ## 删去网格线
3 ~) U* D- ^& Q8 o! n S3 L# r theme(axis.text = element_blank()) + ## 删去所有刻度标签7 e: s# D9 _3 g
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
* o! ]& t3 X7 p" D# Here comes the gganimate specific bits
# @& V* C7 f M2 |& n/ w#labs(title = '日期:', x = '省份', y = '累计确诊病例') +: J8 M& u' ]) s. s% ]6 X
#annotate("text",x=0,y=40,label=C,parse=T)+
0 p/ d$ ?7 u$ h! E; ^coord_flip()+& F- i5 o4 w; u( V, _/ Y3 ]
transition_manual(frames=as.Date(Date)) +1 F& \2 n0 `. U
#ggdark::dark_theme_bw() + #设置黑色主题/ H. i0 u' p1 e
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+( ]; P8 J) q- j1 v1 x) Z1 ?
ease_aes('linear')( _/ ~( q9 ?+ }7 m, q
ps. u3 ?7 E# a8 n
结果展示* D) A9 Z+ }; N! z+ w
* H( a1 a% l/ A: d- B3 |; m% y ; F2 Z3 Z9 u, O. `
视频格式转化,加载BGM0 q7 w" u$ B4 ?! ~( v8 i
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
# _- u5 M( X- Q/ K4 ^8 v # width = 1280, height = 720, res = 100, fps = 10)#视频制作
! t. t& g9 o" l5 R( Z# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
& Z! `8 a% i5 \ H" K; E全国新冠状肺炎26天增长状况
4 d+ G5 d# l& o! ~- ~! d0 W
' c& }. a4 D, ^$ h, t0 X0 Fpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+0 {9 {$ c( ^% U
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
5 w& N5 \; @8 [" y2 |- t1 D geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
+ c2 b2 r% c0 c/ ^% ^8 R theme(legend.position="none") +, S6 x$ a; G/ d9 Z1 ]1 C) T
theme(panel.background=element_rect(fill='transparent'))+# L9 p$ Z4 N- d
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
% h! N) D5 R9 o& W: W theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
7 z8 q# y4 E0 a: {5 _ theme(panel.grid =element_blank()) + ## 删去网格线
0 f& M* A9 m" T. P$ M) _3 D5 G theme(axis.text = element_blank()) + ## 删去所有刻度标签/ @5 {9 w# u9 \6 l- `8 y* l# A
theme(axis.ticks = element_blank()) + ## 删去所有刻度线( m2 S, X G' {( r
#scale_x_continuous(limits = c(0,6))+# h/ w& G# O% }- B. @. N
# Here comes the gganimate specific bits6 {$ C4 v& t% j+ }8 i& g& k
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
5 M3 B. W; U! a4 U$ X& \#annotate("text",x=0,y=40,label=C,parse=T)+5 e: `* L" B" P
# coord_flip()+
/ t) ^" f* r& n4 [9 l0 z transition_manual(frames=as.Date(Date)) +0 P: H7 Y. |$ ?& W& Z. o" K! f
# ggdark::dark_theme_bw() + #设置黑色主题
, B1 M0 ^: Y: y0 z) b labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
2 x# y7 w; c9 J/ A$ j; v9 ~ ease_aes('linear')4 v% y7 d5 Q8 L) L
pc
( A* N, N3 s, }# y2 r. w![]()
0 e2 V9 L5 |. ~7 @' Q% o x) {8 F4 @3 n
动态图合并# T$ |6 k/ c5 {7 c. |5 v
library(magick)
) G6 M6 U t2 ` L/ C, Z. s- A! }ps_gif <- animate(ps,,width = 720, height = 480)7 b1 a% ^2 C- B8 O" |' F* }
pc_gif <- animate(pc, width = 360, height = 480)2 ^9 i4 `/ r9 E" P* n$ q+ o
ps_gifs <- image_read(ps_gif)) m* T9 k! n- _0 _% e, J( Q
pc_gifs <- image_read(pc_gif)7 a" s8 R! @1 d- i
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
! |1 G' m$ Z) H) a0 ufor(i in 2:length(pc_gifs)){
" G7 U6 a+ h! q+ k3 E combined <- image_append(c(pc_gifs, ps_gifs))
* D- w2 | M0 {+ R new_gif <- c(new_gif, combined): ?# `1 y* d. p& F2 x! Y
}
9 g; X) }. @! B; ~, ~) Fnew_gif m" S" ]* A9 i& c( U' s9 G
结果展示
9 z7 D7 S( K% ?( S) e1 R2 n0 {7 |. i3 k: k4 ?
& t$ H/ K( N* h1 p4 y
T( }4 j- A& K& ]
|
zan
|