- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40222 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12778
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
' p$ W0 m2 Y! t, F, Q所需加载包
. S7 O1 \! X, H3 Q0 l1 r" ~0 m- }library(av)
' E+ n1 v K# z/ ]& wlibrary(ggplot2)
# p% ~7 n8 t/ Q7 }' Xlibrary(gganimate)1 b, u& w& p$ q" _! {# ?) [+ W/ A
library(tidyverse)
2 H$ E+ f: Y: Q. {- i2 ?library(lubridate)
5 T" v: n/ @) `+ ~+ mlibrary(scales)5 i5 L: d$ `- b0 t6 `0 b& N
library(ggrepel)6 K# ?( M9 X) s: ^4 q1 o+ A
library(cowplot)5 d4 F0 ^: @. h# {! _0 P/ i
数据
8 f; C( K4 `2 E& h9 M k6 o4 _; B5 j2 m8 |. B
2 Y* [4 T$ c3 `
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +# ^0 G9 g% k8 E E8 L
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
9 F v$ `- t1 ?, j5 y, m" U* B4 Z geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
( }+ ~$ \' G4 r/ E/ x #theme_bw()+8 H- \) P$ R; L% A
#theme(legend.position="none") +
N) ~/ Y+ K2 @; F" e. b/ C5 ~ theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
$ G B6 `4 u# ~4 j/ r theme(panel.background=element_rect(fill='transparent'))+
: v8 n& F# B4 s4 g4 ^" n" W& `0 d* p4 w theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
. Y) N0 r+ k9 D& r( h! a; F theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
3 f8 l7 [6 y. r% n4 G" x" g8 z theme(panel.grid =element_blank()) + ## 删去网格线
5 m. o+ k. F) i theme(axis.text = element_blank()) + ## 删去所有刻度标签
% u& T/ N) E7 _; I% O. W6 c theme(axis.ticks = element_blank()) + ## 删去所有刻度线) z( b& ?" E7 F4 {7 s7 B' J
# Here comes the gganimate specific bits
x- _$ p+ R7 V6 }1 M% L#labs(title = '日期:', x = '省份', y = '累计确诊病例') +' C: J. {( V V5 a$ ?
#annotate("text",x=0,y=40,label=C,parse=T)+5 W# }1 Y7 [/ }- F3 V% H$ M: I% ~! a4 q
coord_flip()+/ N1 C! Z2 O$ p0 z0 [5 u
transition_manual(frames=as.Date(Date)) +
* }4 H* z: I- |7 g6 \1 [. ~( C2 o#ggdark::dark_theme_bw() + #设置黑色主题
5 Q: m3 q8 i' V! T' Q; H6 llabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+& ^) Z# c8 R1 l, u6 z7 ?/ @% J
ease_aes('linear')
! `) w1 }2 v9 n) b: J7 Cps
: y0 C. e' O# T9 D, [6 Z结果展示2 Q6 M: S( e9 C# b, u
- [) ^9 F* I% h' K![]()
, }4 F8 O& I* ]2 T( S6 Z; P a视频格式转化,加载BGM$ \+ E* \' Y! k; }
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
! A; ]& `2 w* B) a& c # width = 1280, height = 720, res = 100, fps = 10)#视频制作
: v& W0 S* I( s: P- G# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")" ~/ r, }7 G# a. N J2 O$ r
全国新冠状肺炎26天增长状况" Q, \% o3 F# X# q+ D1 n
3 i$ @9 {& Z4 ^; ?1 E9 | ^$ |
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+ j- B T6 C' l6 A3 a) l* n# m
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +3 t) u2 q! W7 N3 F8 I* q
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
9 B- T. z4 o, W5 E/ v theme(legend.position="none") +
3 Q! G2 B& z. E+ F6 a, B2 [ theme(panel.background=element_rect(fill='transparent'))+
! K4 t$ q) l8 A$ u- a theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
, y7 V& Z; _: _; O( }* l theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
# G# q7 e$ T6 o& Q* u: q: c theme(panel.grid =element_blank()) + ## 删去网格线
' H. C- ?. L. c5 d" j [, H8 b2 a" n theme(axis.text = element_blank()) + ## 删去所有刻度标签 q) e$ p5 L7 K9 }9 S, F( J
theme(axis.ticks = element_blank()) + ## 删去所有刻度线$ \2 ^7 p' d7 T' ~6 e! B8 t
#scale_x_continuous(limits = c(0,6))+
6 W5 y8 h& C& w/ @/ {) i, Y # Here comes the gganimate specific bits
: }0 K% Y: }3 D& k#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
% ?8 k0 D. V; J1 \7 q7 c#annotate("text",x=0,y=40,label=C,parse=T)+
) w# t2 d4 u- V4 `- K: J # coord_flip()+- c7 q% V* U7 ]! r# ?
transition_manual(frames=as.Date(Date)) +5 s& a T& H5 I e9 P
# ggdark::dark_theme_bw() + #设置黑色主题) W9 B. C \+ ^* c
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+& S y# L9 Y1 r7 X
ease_aes('linear')6 b! t& t7 p% G/ j! e
pc
% K" \ k! C) {: `1 w 0 j. `* A7 m4 q# O' y
9 R; Z9 h8 ^$ @$ q9 K' n" B1 }/ M
动态图合并
. ^* D- Q* k. u3 W) R* i W7 b5 a9 Klibrary(magick)
% Q! |2 P/ k4 r: ?ps_gif <- animate(ps,,width = 720, height = 480) q2 i' P- x( |9 ^
pc_gif <- animate(pc, width = 360, height = 480) y b* z( x$ Q' k
ps_gifs <- image_read(ps_gif)
, r+ T! g/ C0 c2 G) }5 ipc_gifs <- image_read(pc_gif)
3 C, g! Z) J7 Q1 K' Unew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))% l1 I7 l' b$ s* ~+ Z9 o5 E6 k
for(i in 2:length(pc_gifs)){
: M, ?& [9 N- T% H& j& P8 D combined <- image_append(c(pc_gifs, ps_gifs))# o0 t" O1 `# B' }: ^
new_gif <- c(new_gif, combined)4 C8 v1 n0 y: r) d6 k- @6 T
}& K; e/ Z! N; h6 }) M- L2 ^* O
new_gif
9 J2 D8 s. b; L e6 s) M9 }+ a结果展示
* ^# H+ }3 \" h- _% W y. S
& m; `/ P3 Y0 [4 m' K; A# _![]()
( j9 N; H6 A N( w# L
# d. l% y. I4 d* B |
zan
|