- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40347 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12815
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习8 {% t) O8 a J5 J/ s
所需加载包# x, o0 j7 L% m0 ^, f1 P9 o
library(av)
! k7 F6 x8 R8 V" e4 dlibrary(ggplot2), w+ ]' d$ R) X0 [; `0 l* C
library(gganimate)2 B* V! D) p# t. Y3 I3 G, Y
library(tidyverse)
9 x3 S4 i3 p$ B7 z4 ?' I8 {library(lubridate)2 \ z {0 `# |! M& i# `
library(scales)
& @6 F7 o4 B% glibrary(ggrepel)
c9 T! I; B5 ^ ]/ |6 m3 a9 dlibrary(cowplot); b' r0 E. B+ S' Q# h& [
数据. e$ l: P5 C- R0 A+ C$ b1 S
* r6 }4 m2 W o' ]
![]()
# {$ f% b# ?+ n: nps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +7 |# `( S; U0 z' _
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
7 `' b- X$ h5 _0 g% y/ k geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
) o" c( u% y0 J #theme_bw()+% S e( d4 y; ?3 N
#theme(legend.position="none") +2 H1 ?. l6 F8 h& l0 S) B k
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +$ F# `% I' x6 [, j! {# Y" @
theme(panel.background=element_rect(fill='transparent'))+
; p% V# D/ G6 |- b& l+ U7 R theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+4 ?" a% V( K% F9 k/ C, D% E4 V
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+; m8 t7 I- V; C+ t0 y* u5 Q2 k
theme(panel.grid =element_blank()) + ## 删去网格线
; u$ q) x# u, L3 b# D5 ?& `% Q, a theme(axis.text = element_blank()) + ## 删去所有刻度标签
* C8 H( X _% j: M8 M5 X1 L theme(axis.ticks = element_blank()) + ## 删去所有刻度线
5 B# b6 q( g. L& k3 \% ^# Here comes the gganimate specific bits
# Y4 z8 ~# [3 F; T3 S" b#labs(title = '日期:', x = '省份', y = '累计确诊病例') +6 A t8 N+ _0 }7 I) E* d/ [
#annotate("text",x=0,y=40,label=C,parse=T)+
^2 ^# r8 |9 T+ O4 [) Xcoord_flip()+% W( B4 g$ V E) w
transition_manual(frames=as.Date(Date)) +2 {3 _$ v+ I! E; f
#ggdark::dark_theme_bw() + #设置黑色主题# P' K3 Z% v6 a/ X4 [
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
# R& H8 \9 i. B1 aease_aes('linear')
5 ^! R3 V9 G( [2 k5 m1 g. X, h4 `- _ps# E/ O7 \! m% m( d
结果展示
6 h* W! H7 i+ m6 d) f( R
4 S0 [$ U( T" b. P0 y; {7 I2 _ 2 s7 n' a* O! P8 V) ^7 @
视频格式转化,加载BGM; i& b( U$ K; |' ~5 h
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
}* t `& [" ~5 c X0 m1 v # width = 1280, height = 720, res = 100, fps = 10)#视频制作, V+ [1 f5 ^ t% Y# V
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
4 {& A8 y8 W" p2 v( J# H9 j/ ~全国新冠状肺炎26天增长状况
- {: m( h8 S* ~0 @6 }& V
) g4 k1 }. |" O) P# E7 U, O* E& ipc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+. m6 W* a/ ~/ W9 m( l
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
. X& i3 @$ b W' P% e* S8 S1 b geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
/ C" I8 n- W5 o6 Q0 g theme(legend.position="none") +% V, p. _ K, l/ b' ?. h
theme(panel.background=element_rect(fill='transparent'))+8 @. Q% `1 o* K3 y* z, {4 ~
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+7 I) c% ]0 F/ H
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
* D' s0 g8 K) O' ~/ _# V7 W theme(panel.grid =element_blank()) + ## 删去网格线2 {" \( d9 L$ o
theme(axis.text = element_blank()) + ## 删去所有刻度标签+ J$ u( ?1 U: O) K- }$ q$ _$ Z
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
4 U- f# T: a2 R; i: Y3 L8 @ #scale_x_continuous(limits = c(0,6))+" ^- b F" ]+ u' m* _ O
# Here comes the gganimate specific bits
8 p# r, D# U( ^/ V: w: y- e) ]- M#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
E" {# N3 E, L; k# V#annotate("text",x=0,y=40,label=C,parse=T)+
6 r* F- e) J& c8 H7 G+ V5 K9 t # coord_flip()+: p- o$ U$ s& N' g4 u& h
transition_manual(frames=as.Date(Date)) +7 |5 S; _3 l5 U' Q9 ]; b- y' ` x
# ggdark::dark_theme_bw() + #设置黑色主题% C; A' T. u* a: T9 B; y Y
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
# O' f6 x5 H# T, I# y ease_aes('linear'); \$ j1 n+ e: x, D4 Q
pc2 w3 J m- S( H4 S5 `" p
![]()
: a# q: t+ _: _+ r3 w3 o7 R$ K$ b' y/ }3 I9 b
动态图合并
. C8 ~+ [) |* J, F Ilibrary(magick), B7 x+ f4 a# z2 [5 ?) J
ps_gif <- animate(ps,,width = 720, height = 480)' `- x9 n. }; V' a" ^% l
pc_gif <- animate(pc, width = 360, height = 480)3 n* G9 Z& P) [3 |5 ?
ps_gifs <- image_read(ps_gif)5 ~: V( F6 V& a8 ~& `
pc_gifs <- image_read(pc_gif)
- K. F4 a, m+ Y8 D* F7 Onew_gif <- image_append(c(pc_gifs[1], ps_gifs[1])); i2 j9 y3 m! h t/ p" _
for(i in 2:length(pc_gifs)){$ d. J2 N) t8 g2 n, b4 M6 S
combined <- image_append(c(pc_gifs, ps_gifs))2 n/ d2 a& u/ A F
new_gif <- c(new_gif, combined)
8 q- l. B' W, A+ a}1 m" }. m2 s/ S) s2 @' H
new_gif, I3 w& j$ x* K
结果展示
. ?4 k1 Z) _0 \1 Q: B6 ?4 H
: v `, v T, m 8 O; g6 o) P( K5 L- H& ~
4 A5 O- f' g, x# ?$ w |
zan
|