- 在线时间
- 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学习
; e' x) c; m% D1 J% N& h. v( X3 k8 p所需加载包) K m' Q7 z$ D* \! i
library(av)
% i0 w9 [: O/ }) c: p; Blibrary(ggplot2)1 A, T7 {' P& k$ |( t
library(gganimate)' M+ R: J, w6 k4 ~( ^: X7 @" w
library(tidyverse)
/ ~$ t0 K$ L, X2 \library(lubridate)
1 l. |- n0 H! ?library(scales), N- H G4 o5 V9 C2 c+ |
library(ggrepel)
' j! r; S, ~% V6 zlibrary(cowplot)
% P7 i9 ]" c( }数据$ F6 {: s G3 V9 a; q' z
* }4 ]% ~' K3 ~8 b; f0 O![]()
% q5 h) o" U% o/ wps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
7 a# P6 F5 u* @* z) S9 l% _ Z& O geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
' G1 |+ q( H; V; i2 m& ~( C geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)++ q* H" L% G$ w; x. h
#theme_bw()+$ K/ P Y6 A4 e! R4 B3 Q
#theme(legend.position="none") +; n+ ]0 {5 m. A, a0 G, {9 H
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
: v* Q5 o* i# a ]# @ theme(panel.background=element_rect(fill='transparent'))+# o) v7 m! z, b- l: I
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
9 R# ~: d. M9 f' t% o/ B theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+2 D! v( a' x. b" s* k* K, w0 @
theme(panel.grid =element_blank()) + ## 删去网格线7 [5 Q/ D( u- |6 }% t1 ~, N+ O3 ~$ X
theme(axis.text = element_blank()) + ## 删去所有刻度标签- V9 l. T/ X' f$ C
theme(axis.ticks = element_blank()) + ## 删去所有刻度线* A8 p- O* G9 A( C1 T$ D# W8 u$ v
# Here comes the gganimate specific bits, b. S' L, e# K0 M4 S" E7 C
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +8 D8 o( D- c" a$ n# F# ?! F
#annotate("text",x=0,y=40,label=C,parse=T)+2 b" [4 P! l6 A8 Y! j0 H8 B
coord_flip()+
$ p$ k# j, n/ w/ L W3 u4 @4 w3 Ktransition_manual(frames=as.Date(Date)) +" E1 }& V" N" w0 K3 c
#ggdark::dark_theme_bw() + #设置黑色主题) N3 P2 E( S, y- q( ^- C/ Z! z; N
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+# `# A6 U3 Y! y, c. F# U% z
ease_aes('linear')- S% s4 T: V1 ^0 ]( I
ps
7 Q) w, b2 q9 R$ u, Q* i结果展示
+ z! w* `# e6 O# A# v; p' M: D2 H& c$ P
: d* n; u' J' j- t8 L7 D
视频格式转化,加载BGM, G5 S8 t8 l ~; h* z
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
; L `6 A6 [- ?% l6 ^' n5 P # width = 1280, height = 720, res = 100, fps = 10)#视频制作
/ b2 m* I1 |2 p6 e2 D# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")# ^3 I) D2 i* c( ?1 Y' Q/ P
全国新冠状肺炎26天增长状况& G# D& j: B2 I" ~& S6 U
4 V7 o6 O# T5 N- w- m
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
% a* B/ J/ x7 i# n5 i geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +- b2 r7 r3 C x8 x. _6 A( l
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
. X. t0 K' o6 l) @$ B" N1 _* g theme(legend.position="none") +
6 p+ Z. Y" ?0 {" \0 O# Z! C' z theme(panel.background=element_rect(fill='transparent'))+
5 |9 Z) @7 O6 R; ? W theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+# m. E" K9 H5 ? N
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+" X" A" P7 k }4 d* ?8 G4 ~: n
theme(panel.grid =element_blank()) + ## 删去网格线
' @ s3 k) c. T( u5 u: ` theme(axis.text = element_blank()) + ## 删去所有刻度标签- P$ D8 a' U! H2 U8 ^; a
theme(axis.ticks = element_blank()) + ## 删去所有刻度线2 O% X$ `: F+ V) ?0 u+ j" F, s
#scale_x_continuous(limits = c(0,6))+
! s7 L5 y0 k4 A0 L4 E # Here comes the gganimate specific bits7 j9 |$ |7 T- a
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +9 O, I: k7 Z; f* }2 d/ y" Y
#annotate("text",x=0,y=40,label=C,parse=T)+7 d' o) F! C" H/ A! U8 H: W# o
# coord_flip()+6 f b2 u7 J" z/ ^
transition_manual(frames=as.Date(Date)) +7 P( o) O# }* q
# ggdark::dark_theme_bw() + #设置黑色主题
- B% t0 T6 M" Z labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
, b8 w. o3 _- ?4 R3 W2 J1 o7 N ease_aes('linear')
/ M; Q- N9 k0 l8 X# Hpc
0 b" m3 L9 u* f: U& b![]()
; g3 ?4 }" A) h) h; o$ m
& A _* w/ V ]+ g7 I; ^动态图合并
0 f+ m1 f, Y5 Rlibrary(magick)+ L) c& l7 i! i/ P
ps_gif <- animate(ps,,width = 720, height = 480)
l! F" b3 T- q& d8 }$ Kpc_gif <- animate(pc, width = 360, height = 480)
Z' K5 g) J. O* j3 X# h9 B# c) {ps_gifs <- image_read(ps_gif)
7 q& ~1 X3 w) w% ?, ]7 r5 D* Y. Opc_gifs <- image_read(pc_gif)& D# t p, W' B+ e. f. V
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
2 M) E" M" h/ N& m; jfor(i in 2:length(pc_gifs)){
' [ }8 r) ]# h! w8 _7 o combined <- image_append(c(pc_gifs, ps_gifs))) M t3 N( T3 j
new_gif <- c(new_gif, combined)3 C# S. Q, J- c3 y, h3 G& Q
}8 ]; z& I; \7 Y1 V5 _
new_gif. ~) D9 E, ?. M8 {
结果展示
- A/ j# ]* t4 z2 p- a8 W: S8 w/ L; f0 U, e3 Y9 N5 Q# k. M
- Y& V Y/ n5 X9 V5 C) L
/ N8 a2 M; A( C) T
|
zan
|