- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40345 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12815
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
/ V. u9 ^8 ?2 I: |2 @6 t所需加载包
8 a4 N z8 J" v. v o( U1 d; N2 l7 P: ~ Ilibrary(av)& g, g3 `( Q4 m& i, y8 h/ U7 [
library(ggplot2), z9 Q" G* J, }) }* z
library(gganimate)
' Y) a* X' }! n; }library(tidyverse)
) n) F3 N" U4 N- I3 k* ulibrary(lubridate)/ A# ~& a# W* N6 w' k) I; r) R* e
library(scales)3 \9 N8 Y% {5 p) ^% C
library(ggrepel)
( x6 ]$ m! D5 p' U" Hlibrary(cowplot)
$ d, S* I% F0 t数据
5 P) r* l1 _& b: N y w+ i) ^* w% D' G, {. [* O. r9 K1 O
![]()
! A) s: \& i( d" O4 Hps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
" Q- J) }2 o2 U% L1 a geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
?; P/ f' @1 B- M geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+3 T* j! }1 w3 T+ t2 l1 ~# \9 Z
#theme_bw()+: M+ ]* l+ g; k& ~
#theme(legend.position="none") +
1 h1 W( G! A8 @2 p$ U theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
* S3 _4 E9 N! A6 i5 h/ r1 z5 x! O- l theme(panel.background=element_rect(fill='transparent'))+
9 Q- ]: z! R3 h( Q theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
0 p: d- G# A! r! ]' U theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
; w- P6 [% b& s# S theme(panel.grid =element_blank()) + ## 删去网格线
* H9 r' |9 y; Y2 p2 x5 h$ v9 m/ } theme(axis.text = element_blank()) + ## 删去所有刻度标签
) X8 B O! P( D" M2 a9 N" \ theme(axis.ticks = element_blank()) + ## 删去所有刻度线' w- ~; |& Z5 p
# Here comes the gganimate specific bits/ S5 V; n' x* s) \/ ]
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
$ e: ?# a, H7 p4 y/ K#annotate("text",x=0,y=40,label=C,parse=T)+9 m6 ~( ]7 @, U6 V/ c3 H. ]# v
coord_flip()+8 {7 l F2 {4 O8 p
transition_manual(frames=as.Date(Date)) +
' k6 B' `% ^6 H! F$ N, `#ggdark::dark_theme_bw() + #设置黑色主题9 y. a+ U. G8 Y. |+ }' ?
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+& p# a! W0 i7 B0 i2 W5 R
ease_aes('linear')4 d( @' o/ C+ I- u* Q
ps: D: F- p, B4 A
结果展示: u/ t' L7 V( o2 ~4 C
$ }. _( B( W: N' _ K( }# v+ t
![]()
& \7 U* p0 W8 a$ o$ O3 s视频格式转化,加载BGM
% A* ]5 }. |$ {#df <- animate(ps, renderer = av_renderer('animation.mp4'),
# b# p; _) ]7 a: m # width = 1280, height = 720, res = 100, fps = 10)#视频制作# `. V/ ?) W O X
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")" G/ v5 A( f. u3 l9 B. M' c
全国新冠状肺炎26天增长状况$ q$ c( E- u% M$ X* v& l
1 M; a3 a" _0 `, v( s7 r9 s+ ?, Mpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
2 N/ W- s+ j# k! N geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
" K: j7 a" z2 N$ V( N geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+7 v6 u9 O- K! V
theme(legend.position="none") +0 D3 q: y. w: Z0 Z& D, ~
theme(panel.background=element_rect(fill='transparent'))+. ~$ I7 O- O3 K2 w( y6 v
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+7 _2 N+ l" E: R' r% \5 j, {
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+4 n' K% e( x+ _5 l2 h6 |" l$ ?
theme(panel.grid =element_blank()) + ## 删去网格线3 X1 T* T# |$ a3 H+ W. J; c
theme(axis.text = element_blank()) + ## 删去所有刻度标签, W6 Q2 o6 Q& ]: u% V9 T% I
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
( M7 x* s0 K2 v7 u/ T1 s #scale_x_continuous(limits = c(0,6))+
2 m: T: ^- x, L- k o+ k # Here comes the gganimate specific bits; W! K" G7 R( d' w+ A% [
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +) j' X0 A- f7 b- P8 \$ l ~
#annotate("text",x=0,y=40,label=C,parse=T)+5 _! h, x- W1 D! o
# coord_flip()+
9 ? D$ G' Q- `) h, \ transition_manual(frames=as.Date(Date)) +, q6 E7 J& ^! o/ P3 O3 a) e- @
# ggdark::dark_theme_bw() + #设置黑色主题% @8 W; ]" D: @4 t/ C
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
+ ^+ J7 r/ V4 i4 n ease_aes('linear')2 \0 {6 b6 N+ S# N. Z* P4 g
pc
- E3 j2 b# |- j) d; ?/ o' S![]()
( {+ b. {0 v4 V, E" c7 M% G# i2 U. D* h& A8 m( v
动态图合并
, V w3 h' j# a7 [* g7 nlibrary(magick)
$ ^ l0 v) W1 x1 B6 s- rps_gif <- animate(ps,,width = 720, height = 480)
# W; @2 e$ D* h( B8 wpc_gif <- animate(pc, width = 360, height = 480)7 s+ D; n" Q; _+ M: D9 J4 X4 W
ps_gifs <- image_read(ps_gif)0 _7 W0 e* b/ g9 ^; Y3 ?" }
pc_gifs <- image_read(pc_gif)2 f7 i) O/ s2 V& [6 V% X' }$ d
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
& j. L, E0 H9 t) X( w4 r$ b$ kfor(i in 2:length(pc_gifs)){5 l6 K- P$ h! \* Z* w" E% _. H
combined <- image_append(c(pc_gifs, ps_gifs))
1 r! i0 I7 ^! a3 k. q new_gif <- c(new_gif, combined)4 }6 t: A* V( ?2 J
}4 y7 e" e5 F: z" X( X
new_gif
! m! ~; V% i- |0 o1 U结果展示/ E* O9 P' f7 x W6 k
! |7 e: a! c" ~5 c! b7 n
![]()
$ M7 {" q$ p% O- M, L" ?% t8 a6 K1 |' q
|
zan
|