- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40325 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12809
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习, P: N. l. }! B8 k- c4 s% E
所需加载包
# B4 ^* G# V$ W5 U: K% Xlibrary(av); C1 q1 U* j. [& C9 K. U1 d
library(ggplot2)
' a8 ~4 L7 V+ H2 I6 ~library(gganimate)8 k1 r* y: d! |! J6 J$ s1 k7 Z
library(tidyverse)5 ?0 M$ `- J; a& s
library(lubridate)
. a1 l: h7 N4 v1 I" G4 m' E- j9 ^library(scales)' c$ u/ f3 q# J0 Z! V1 z% m! E
library(ggrepel)) ^! A: ^0 c3 L0 V9 _- c3 \
library(cowplot)
& \3 o- e# Y Z* P6 M( f3 H: P4 N数据: X: s( M8 F) @
8 D( w" K0 l& O! x! R. G
![]()
# ? i( C5 s, Z$ _9 d* ~: sps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +: z8 C! p+ \1 I0 a- u7 h# g" |# k
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
' n. w' v" m+ a y7 B5 | geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+0 u9 Q+ n8 U$ U2 q
#theme_bw()+
- G* t. j) J+ M z #theme(legend.position="none") +
/ W; U3 {0 v! |) \2 R# v' ~7 C theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +) w! `- n, ~9 R( J& T* }: L) C
theme(panel.background=element_rect(fill='transparent'))+$ z; D: m2 C5 S: S' q h) x
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+2 `! {, n8 T( o1 S2 G) i
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+" g+ ?6 R& H$ m# t9 u' B7 U0 q& N
theme(panel.grid =element_blank()) + ## 删去网格线( v |* U- o- T
theme(axis.text = element_blank()) + ## 删去所有刻度标签
, O6 B# t9 M1 B! Q& z theme(axis.ticks = element_blank()) + ## 删去所有刻度线
+ a% v1 q1 K5 P7 {* o& A( q# Here comes the gganimate specific bits
4 D3 Y: X8 r. J. s#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
# U* l: Z1 R2 e#annotate("text",x=0,y=40,label=C,parse=T)+
q1 ^ V: d* u2 p4 C: o2 r Zcoord_flip()+3 ~- @! k8 V* Z' X
transition_manual(frames=as.Date(Date)) +
+ T! r+ d, c8 e$ q#ggdark::dark_theme_bw() + #设置黑色主题
3 m+ T- J. t, l% @# qlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+( y, U, s' [0 q8 \$ F" t: n6 Z
ease_aes('linear')
: v$ W. E8 p' K, D3 aps! X/ c3 x0 p+ z% W, p8 O
结果展示
! O+ S* q: E, B2 v+ t7 ~: i' k3 s2 r, K; b, }
# X. ^: L0 K& Q
视频格式转化,加载BGM( K% Q$ ~7 f |! K! Z
#df <- animate(ps, renderer = av_renderer('animation.mp4'), - t z P3 O3 x9 ?9 f) P$ A4 U1 J& O
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
) W t) r5 @4 K* o I7 o# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
+ p2 u3 Z W: `- I% k全国新冠状肺炎26天增长状况) t. a5 H+ o3 g7 k, L, t
$ V3 p( C' t* N4 a0 o( X/ o
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
! G* x4 @0 f# C5 U8 Z geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
- e% N7 s9 C& B$ B/ z geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+0 K' P2 |7 m( H
theme(legend.position="none") +" Z" L. y) Z0 a/ Z
theme(panel.background=element_rect(fill='transparent'))+
6 b$ y3 d% `" ^# P* C theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
7 J+ n! d$ q1 n9 s4 D" e$ O4 f4 A theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
( @+ Z1 N6 j( _+ |4 U theme(panel.grid =element_blank()) + ## 删去网格线: T# z( k5 B- a
theme(axis.text = element_blank()) + ## 删去所有刻度标签
+ S# b- C# U/ l" R4 i! j/ K) c theme(axis.ticks = element_blank()) + ## 删去所有刻度线* V/ ~$ N1 D& J9 f
#scale_x_continuous(limits = c(0,6))+7 M* V/ i; W7 U* b
# Here comes the gganimate specific bits8 F4 n& g( y; K7 f& W0 C1 ]
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +4 V5 b/ x. ]+ D- [; m3 k: e% j, v- n
#annotate("text",x=0,y=40,label=C,parse=T)+8 e+ E: K5 Q1 |" ?: d, `) f
# coord_flip()+' H+ I5 k9 b6 s. u$ |0 f2 |1 Z$ V
transition_manual(frames=as.Date(Date)) +6 q5 S3 {3 g7 g( |/ A
# ggdark::dark_theme_bw() + #设置黑色主题
7 {; V/ D: O4 W9 V% S labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+5 ?2 L" P v7 b1 y, s$ J6 d! f) j
ease_aes('linear')) a0 b" g3 h9 [& P4 ?
pc; x/ u( m: ?0 r, P9 w, e* n/ n
; Y0 X. {& Q4 X7 z
; K5 A7 l- O9 O0 s% x+ l1 W
动态图合并
, J* |- \& `# K# ^1 j& [library(magick)8 e. `* U4 Z5 m5 V
ps_gif <- animate(ps,,width = 720, height = 480)$ F/ \& p* A; o: R7 @* q/ r. x
pc_gif <- animate(pc, width = 360, height = 480)
% k, @2 J) N2 `7 L; d- D# x3 ]ps_gifs <- image_read(ps_gif)
4 S/ I+ D" _0 L' z. L! Kpc_gifs <- image_read(pc_gif)! M% A9 ~/ o9 d. j& l% W
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))' b+ ~% y5 J4 i6 }- i; q5 T
for(i in 2:length(pc_gifs)){
% N4 J+ h ~* |: k$ t1 U combined <- image_append(c(pc_gifs, ps_gifs))
: H1 l) O: G! o& w new_gif <- c(new_gif, combined)
Y" a7 K3 o7 A1 Y. z4 s. [}
- J$ w& [. J9 C, M" Knew_gif
/ A6 O9 x n. f5 q0 T. w, G结果展示2 v; U4 j# |# B+ P9 O6 p
/ N- ?) s: ^2 j$ n) h
- p3 [4 T$ V- W; p' X8 U+ c7 E
3 A) E4 r: [; X- l8 C+ m |
zan
|