- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40293 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12799
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
" R+ H, s% Z: x3 J ^所需加载包. P" K4 ]% T- o
library(av)
0 ?8 w5 U& q+ C0 T, I, Ylibrary(ggplot2)
! Q, Y5 J* r. u5 {0 O2 C% m) Y7 Tlibrary(gganimate)6 q6 a9 R& Y+ ?4 | V% V% R
library(tidyverse)
5 a/ p4 {9 C9 q( H! n/ ]% F( slibrary(lubridate)3 q1 y7 f" d: o5 p# W
library(scales)
4 r8 f+ N6 j8 m) |, X& [* ylibrary(ggrepel)/ D; {# K8 e" A( G+ K. A+ z
library(cowplot)' d; L2 ^/ @% k- c. v
数据+ L; p' C, p3 j1 g2 Q
1 I- X* L W/ P+ q1 e! Z
![]()
1 m8 |1 A9 I0 C+ V0 H9 O2 Kps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +2 ]8 k$ w8 _8 M: x* \ ~9 m5 \* m! y
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
& E4 }5 }* ]# f% X: C; s* N geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
: V, g, }* m3 s# c4 R4 | #theme_bw()+
2 N% ? n+ y: X# s: o #theme(legend.position="none") +
2 C! x9 `5 d. N W8 {/ _ theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +: f4 `; j! `6 f+ I# Q6 [
theme(panel.background=element_rect(fill='transparent'))+
- k; k# \! I; [* G$ o' r# l theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
0 m6 }0 M) R2 U; u3 h G0 H' i theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
" m7 t: Z+ A, ~ G% ?% x" ] theme(panel.grid =element_blank()) + ## 删去网格线
0 R+ W$ }' F. V. ^ theme(axis.text = element_blank()) + ## 删去所有刻度标签
! D' G) r$ m. O1 t theme(axis.ticks = element_blank()) + ## 删去所有刻度线
, c' Y# L) J" [6 \# Here comes the gganimate specific bits
$ N0 K! C3 f! r( r#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
* |7 d- D# Z; L1 u1 B#annotate("text",x=0,y=40,label=C,parse=T)+; t$ H$ @2 v6 A. o
coord_flip()+
0 x* Z7 i; [, @0 H2 j. J, Ctransition_manual(frames=as.Date(Date)) +
7 w# O% B! U6 V; `: J#ggdark::dark_theme_bw() + #设置黑色主题
7 d' Z( h* }" H$ zlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
6 ]% @# Y7 F% eease_aes('linear')
( z9 X4 O' ^5 c S3 H: O9 s gps
0 ^7 D+ Q& f0 X1 |% |( g结果展示
& I' d" D( l ^, f# h) i( m0 ^+ i3 {. @, M# ^$ B5 F+ y0 [0 C, |2 f
' z s. U3 {+ u! i% N1 }$ Q. v9 `1 B2 X
视频格式转化,加载BGM
9 n4 q# h: x- b- H- d8 i& H" t#df <- animate(ps, renderer = av_renderer('animation.mp4'),
6 a# w' R/ z, C3 w- @! ]( W # width = 1280, height = 720, res = 100, fps = 10)#视频制作
8 }/ M6 v' ?. m& M) J: o, _7 q# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
, H& m1 z$ _. w' w全国新冠状肺炎26天增长状况
1 _& |% l1 S( b8 e
' L9 R; Z; ?5 Cpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
8 \! \6 \- O8 U$ d3 {# Q geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +% W6 J' I& w8 ^* ^- N) _& c
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+; o1 c# |" V0 n! Z: X: A
theme(legend.position="none") +
/ ~& {/ o, n5 m0 ~" d6 O" K theme(panel.background=element_rect(fill='transparent'))+
. v2 Y( H; m3 P n theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+# e7 s7 ?* w9 m1 s/ I& g$ f1 ]4 T
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
1 @; ]5 i2 F' B/ O! h2 K1 b theme(panel.grid =element_blank()) + ## 删去网格线
3 p3 T9 I1 v) h0 Y theme(axis.text = element_blank()) + ## 删去所有刻度标签5 O, H+ z4 F: i, O7 q$ D
theme(axis.ticks = element_blank()) + ## 删去所有刻度线$ R! i& }1 r6 Y- D7 i( s
#scale_x_continuous(limits = c(0,6))+
5 ?% Q! Y8 s) b) U# W) B* s- c # Here comes the gganimate specific bits
# r& w3 a% y, e) ^, O#labs(title = '日期:', x = '省份', y = '累计确诊病例') +) f3 N1 Z h* T1 X9 m# [
#annotate("text",x=0,y=40,label=C,parse=T)+/ b8 q' q/ V& H1 [! ?; j/ _1 [/ ]
# coord_flip()+( |2 P' {* n# k' i- _2 |$ L
transition_manual(frames=as.Date(Date)) +
* d. p4 w( y1 B # ggdark::dark_theme_bw() + #设置黑色主题
, m" k9 j$ d: w' _ labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
. I Z5 Q2 H8 r5 Y+ z ease_aes('linear')$ ^! |' K9 p" R5 ^+ D8 k( j* D
pc4 B G" M$ p, m0 a- C' J
$ p. h' c% @. X/ V2 x, s% C* S6 B
2 O; L5 g+ D$ i- @% e: q( P& {
动态图合并
, V6 Z+ y: Z& m8 dlibrary(magick)
7 W% g! L5 {* W: zps_gif <- animate(ps,,width = 720, height = 480)
9 f) m1 p+ K# x1 H7 b7 Npc_gif <- animate(pc, width = 360, height = 480)
8 N6 G- H- c. Ups_gifs <- image_read(ps_gif)
/ c' ]: \3 o7 k& Npc_gifs <- image_read(pc_gif)
( E; }/ Q. i* [/ {' h7 [2 S8 cnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))2 q3 _; n2 }3 a2 ~) [. h0 g
for(i in 2:length(pc_gifs)){
5 M: h# ~8 [$ N7 c& M* E combined <- image_append(c(pc_gifs, ps_gifs))
# j' b! |2 [* y8 v new_gif <- c(new_gif, combined)
: K$ K0 R1 _; O4 `( G}
# P2 a% w4 x+ rnew_gif
1 O/ N" ?4 \# i1 I结果展示
9 p x; l5 l7 a7 F# F4 F* Z! F3 H; Y- m8 c& r# @; h
![]()
7 h* I* J A& o1 x* c
3 V8 A5 P: s) K2 i |
zan
|