- 在线时间
- 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学习
! F' u& _( i0 z# d7 b所需加载包. j! N3 i: m: r u
library(av)
3 \2 i |0 u- B! alibrary(ggplot2)
; W! P! q( q/ I% llibrary(gganimate)
$ {( x0 J& `- K( elibrary(tidyverse)# X: A0 S& q" |- Z/ l
library(lubridate)
6 W" z" G* A9 a9 b- Nlibrary(scales)( \* A1 g; q. [8 Y1 |
library(ggrepel)
3 j, ^( G! h& j/ h7 llibrary(cowplot) W! V4 [# P; t' X
数据
+ x$ R: [; U5 y; P: `7 }7 V3 m' ~
5 l$ E& i( q, \! h![]()
8 P, z2 D. o& D- ~% Zps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +9 a6 `2 y; @" N6 h% C! m
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +9 a4 T; [* m* P9 W Y
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+. q0 s% \ r0 s, X
#theme_bw()+6 N( h0 C( l4 w5 t
#theme(legend.position="none") +1 l& I& Z1 ^9 G0 t, @ }
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +3 G4 {4 \1 X* q( g- x
theme(panel.background=element_rect(fill='transparent'))+
( ~! `3 R1 y8 Y+ N theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
8 g# V r- ?! T# t- B2 } theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
* G7 U" L- r* s( r2 m/ e theme(panel.grid =element_blank()) + ## 删去网格线8 J6 Y8 i g: s5 @* D1 y
theme(axis.text = element_blank()) + ## 删去所有刻度标签
7 W$ o- o# T, m7 L9 {' Y theme(axis.ticks = element_blank()) + ## 删去所有刻度线5 P! a5 x }' b- D
# Here comes the gganimate specific bits
" O7 u8 o6 O, ]. _: \5 `#labs(title = '日期:', x = '省份', y = '累计确诊病例') ++ Z& L3 ?5 h$ i8 @
#annotate("text",x=0,y=40,label=C,parse=T)+
7 R6 ]4 J6 a3 E* B* e1 Ncoord_flip()+
- p6 l7 X. }) L+ b/ H6 ltransition_manual(frames=as.Date(Date)) +
+ _% L4 ]; ?# d) A4 s a( \#ggdark::dark_theme_bw() + #设置黑色主题0 T* \, [- A- z; e1 f
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
( d* g8 ~" \, V3 t& s0 H3 s4 rease_aes('linear')5 I7 Y7 @7 l( I1 L5 s4 i" B
ps
. ~$ b: k$ {( H1 ]! D结果展示
' P9 i- {# [) [7 |8 n8 l
! M" w4 R- |* O, r6 z, w 9 q+ i1 ], z- a, p5 ]
视频格式转化,加载BGM
q7 f& K p/ |1 `! A: d, ?#df <- animate(ps, renderer = av_renderer('animation.mp4'), * g5 z* r, }) q* g
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
: s% @9 G; a s* L+ T: T# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
) E% \7 {8 Q, w: _8 P全国新冠状肺炎26天增长状况
' A& X0 i9 p! h- \2 _
3 k, T, q: V D6 X3 f# hpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
0 y- q( s$ h' z* t" j5 {1 C* P geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +3 O3 Y' N, x; \/ [6 G! F2 E5 J4 X
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+, X6 F" d6 S( ~6 I) H5 ~9 k) N
theme(legend.position="none") +6 j; J# Y! k: y; d1 R
theme(panel.background=element_rect(fill='transparent'))+
" ~# S1 ^/ a0 j/ C# C theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
# J* U/ w: d% R theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
5 z m) r: ]8 K1 m5 I$ Q theme(panel.grid =element_blank()) + ## 删去网格线& |( `1 L* a" i" J& b
theme(axis.text = element_blank()) + ## 删去所有刻度标签7 |) o# P% p% @9 k' U
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
. A5 n T. y9 \ #scale_x_continuous(limits = c(0,6))+: M$ p: X8 Y7 \7 _# H9 t
# Here comes the gganimate specific bits+ k8 L" i0 s8 ^# c K
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
$ y1 n9 j/ {) x' h8 `) I. S#annotate("text",x=0,y=40,label=C,parse=T)+
5 X) |% T: T0 Q # coord_flip()+
7 G; O$ H8 }9 e8 n+ c5 d9 Q' {. J6 J$ a" u transition_manual(frames=as.Date(Date)) +! s- T; A2 U7 B4 b( D% p: D2 T
# ggdark::dark_theme_bw() + #设置黑色主题
' k4 h5 f- }) I! ~' H+ n labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
, \! X1 o0 B' n$ ~ ease_aes('linear')
h- J0 @ a4 L1 d( f! @3 k* wpc4 k1 B& K7 ^/ u6 g9 y
7 h: n% Y) O" E0 H
+ i, g8 ~% t8 O
动态图合并
( w: A7 C6 {4 r0 E; i, ^6 ]# @9 Hlibrary(magick)+ M; D+ i- e$ y; i/ r3 T
ps_gif <- animate(ps,,width = 720, height = 480)
& X2 O' P+ C& zpc_gif <- animate(pc, width = 360, height = 480)! C# S% h: w5 E! C! _: C- N5 _
ps_gifs <- image_read(ps_gif)
3 K) `# r0 |( D# n; ^pc_gifs <- image_read(pc_gif)
0 x# [* h1 {; I; I, N0 Q8 C2 [new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
, u( r/ \) X) b7 x/ Z& Dfor(i in 2:length(pc_gifs)){$ ~- ^8 u" o; j9 t- D k- E, @
combined <- image_append(c(pc_gifs, ps_gifs))
) U& l9 d0 S7 ]# [0 r new_gif <- c(new_gif, combined)7 ^8 S; X, X" [; X; C
}
& W8 A( h8 g% E( c4 [new_gif' t8 V: T, n- U; A
结果展示
2 h* w: C' @" j4 X! R8 \! B3 ~& Y3 g! r8 S1 ?. J4 d- ~; S
4 B- o$ o; \* g/ ?* ^
/ [9 W5 U3 W5 P* @" J: k" I
|
zan
|