- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40219 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12777
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
; N# ?2 o4 x, U, c) L- y7 N所需加载包
& q5 a" s6 J0 ^0 D- R6 ]6 tlibrary(av)
+ H; p% E2 E$ Clibrary(ggplot2)
" `7 e. e# A: `+ Olibrary(gganimate)0 \! u$ n. J$ r- {6 ?: E
library(tidyverse): h: S* X3 ^: L: g
library(lubridate)* p- N+ J4 b9 O' |% e
library(scales)
v7 [! p; e1 j1 T& R# N) f, Ylibrary(ggrepel)( r# U- i. {! m3 K8 j! b$ n
library(cowplot)
8 }1 N( K3 `6 E9 _1 ^( b数据' e5 w( e% E6 I6 _/ s, G
* H% K o; y! j& C6 ^9 I& O
3 a% z1 e/ I' I9 j
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
) W+ G) J& w4 [ geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
+ x2 J+ |9 h2 T- ?* c1 F/ z) E" b7 W/ ] geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
! h: U: F4 l; K1 X5 x# \ #theme_bw()+1 A' x3 ]( }9 r% j& ^3 Y
#theme(legend.position="none") +
0 }. b# h) F3 N/ Q4 J! D0 u theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +) R7 |0 ]1 V4 H: O; V, q
theme(panel.background=element_rect(fill='transparent'))+# O# U& ^; ]8 _9 y9 W
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
- Q" y0 f8 N( O3 n) k theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
2 L( ?% ?5 S5 p4 q( K theme(panel.grid =element_blank()) + ## 删去网格线
: u' x3 L# l7 j theme(axis.text = element_blank()) + ## 删去所有刻度标签
2 O2 r; L; F5 c$ K, }5 _" u theme(axis.ticks = element_blank()) + ## 删去所有刻度线
P% g7 n3 k$ _5 @3 p" Q& G4 U# Here comes the gganimate specific bits7 o- m; L' {& c" m( e5 t/ y
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
2 y4 i* P. i( `, F#annotate("text",x=0,y=40,label=C,parse=T)+
6 E! l5 w L0 ?coord_flip()+' @3 K5 X7 m/ l# n7 ^* U
transition_manual(frames=as.Date(Date)) +* Q9 Q9 ]5 @. x# j6 s1 e
#ggdark::dark_theme_bw() + #设置黑色主题
: x0 \/ G) l# A8 a% tlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+# r% L# r1 m) z% R7 B
ease_aes('linear')# s5 w. T8 S& Z$ g& h- `5 H. R6 @
ps! q( v5 v' [4 Y, E
结果展示
2 Z, s, ^% s$ m k) t" b- i; b- ?2 C2 F; \6 U9 \
% Y% z9 R2 c2 i8 I0 P
视频格式转化,加载BGM9 [) g7 c. h/ S1 x$ z( D M! |
#df <- animate(ps, renderer = av_renderer('animation.mp4'), % f6 J8 ~! s& [
# width = 1280, height = 720, res = 100, fps = 10)#视频制作* t1 u3 ^, T7 N0 u
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
1 o; k0 A2 V5 |" F( z: _+ B" |" d全国新冠状肺炎26天增长状况& g1 y/ Q- v P) v8 Y$ R5 Y$ q
N8 e- a! H. Q% K9 F: t- ]# n# Kpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+/ R) u2 b" W: X8 y" D) p7 }5 b0 v( `
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +4 }" N; s* j3 V/ c7 {' _. d0 J
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+( s% S7 L+ t( v- n
theme(legend.position="none") +
+ v' p7 p$ T4 n6 G theme(panel.background=element_rect(fill='transparent'))+
$ x U# [$ V! k2 N( ~7 D) z; W theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
# Q2 H# {6 o- k7 \4 H) S0 W theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))++ u, j& t2 x: T# M+ R
theme(panel.grid =element_blank()) + ## 删去网格线; `' y" Z+ C) j8 j3 \
theme(axis.text = element_blank()) + ## 删去所有刻度标签
/ }% L/ G8 P' h k6 i theme(axis.ticks = element_blank()) + ## 删去所有刻度线3 b M5 W& @/ _( v9 F* e, y o
#scale_x_continuous(limits = c(0,6))+
4 u$ Q: o& E' f4 R* F0 k # Here comes the gganimate specific bits
5 t& L/ J' R9 ~. L; O% |#labs(title = '日期:', x = '省份', y = '累计确诊病例') +4 n' f d3 g+ x1 @% m
#annotate("text",x=0,y=40,label=C,parse=T)+
: x; g' Q7 O! d) ` # coord_flip()+
% l& @7 x" I. @. m- e- P transition_manual(frames=as.Date(Date)) +' @4 g1 d1 ?( z; E6 d% O
# ggdark::dark_theme_bw() + #设置黑色主题
: L( D7 w. Q* m1 Q labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
$ m ]$ F( Q5 u5 t( I$ a ease_aes('linear')+ y+ d0 D* @& T3 |2 S
pc
8 A- a4 J+ \% c0 ]! S+ _; n! }: i![]()
( B/ G5 z9 z! \1 [: {) g
0 u5 w* Q; Q4 n动态图合并; |5 B* t9 b, J! e
library(magick)/ a5 G: k5 g( v* h. {4 Q" x
ps_gif <- animate(ps,,width = 720, height = 480)& o, I7 o9 J* O E/ f
pc_gif <- animate(pc, width = 360, height = 480)
; i' z u4 C0 x7 Pps_gifs <- image_read(ps_gif)& _; A# Q8 b" ` e3 s
pc_gifs <- image_read(pc_gif)
/ ~' A6 E" N/ Q0 U) H8 \0 Cnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
6 M0 g! ^) X) n' O3 Y& mfor(i in 2:length(pc_gifs)){
7 e `" m9 Y2 c6 k. D9 d6 b* j combined <- image_append(c(pc_gifs, ps_gifs))
$ J, o+ R9 ^ t0 m new_gif <- c(new_gif, combined)! `, T5 f! e) H2 O0 S, f) a7 B
}! F: \8 t+ Z$ H- R2 B) ]' y& P. t6 s
new_gif
# j2 G& W4 Y0 j% M8 d2 B结果展示
2 D; |% t4 s8 d* H1 n* h1 y% R; S- o. y! r6 K$ ?- a: Q1 H( z" }' ~
![]()
! J! z" m" J; c0 n' h' Q% q0 a8 `2 B; M7 |5 z1 ~8 M( w
|
zan
|