- 在线时间
- 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学习
! y7 P- ^9 c# _% [所需加载包
5 s2 c% F$ ^. e* W1 Jlibrary(av)0 m, N0 o7 ~& K k1 R8 e( ]( ?5 Z
library(ggplot2)) ]" Z7 E1 p: \# E
library(gganimate)+ ] D- B1 |* J* }
library(tidyverse)
( m' e) ]2 [+ E7 Q+ b6 zlibrary(lubridate)
# u& z( u1 n6 ?! L; @- O2 H3 Jlibrary(scales)/ Z& o4 C+ R( H S6 g7 j( R O
library(ggrepel)6 q, [1 p, X- e$ H, e- c' e' c
library(cowplot)
/ h& O0 u" b& N- t( j+ x数据; F T- q7 p3 d8 R$ r& \( K
) O- u+ G. _8 `' l L" i5 G+ Z![]()
p7 w4 V C) P" p7 mps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +4 `3 Q3 w' s! v* [5 _( A8 _& g
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +* {, r( r1 K4 n, @' V3 @
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
" F7 c. _( r: [9 q9 V1 a8 }+ ] #theme_bw()+
4 R# H" U% f# V$ }) }" O/ {, [ #theme(legend.position="none") +
% ^' r( d) Q2 f theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +( Q& A& G" L& A
theme(panel.background=element_rect(fill='transparent'))+' S/ o I7 o K
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
/ D( P8 n- J5 }( f0 A1 [' f theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
3 |& R7 S) }" @# N2 O theme(panel.grid =element_blank()) + ## 删去网格线, J( R$ ]1 v6 M" ~. t0 @% T
theme(axis.text = element_blank()) + ## 删去所有刻度标签* }/ c C( i) }9 N: \
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
: I1 V8 |. q ?6 y$ Y# Here comes the gganimate specific bits5 t- {$ a* Z/ J6 ^6 H
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
& V& R5 Z/ T: M& K) p! c, s: e#annotate("text",x=0,y=40,label=C,parse=T)+
, e; O* e- C! R* u. p1 q0 E, a: Ccoord_flip()+& D. ~; r n% Q- u2 x$ f; D
transition_manual(frames=as.Date(Date)) +
: j5 l( v0 o- z#ggdark::dark_theme_bw() + #设置黑色主题
0 q! v" v% T# u5 T8 T" t: Ilabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
# C0 {; G9 c6 O1 ]. H: _" J# Zease_aes('linear')8 ~3 k- u3 n( T& [" G) z
ps
: Z5 q: b3 U+ ?2 N& o& b结果展示
/ a' I- Y1 L7 e4 p$ V# y& v% X" }. v$ i6 p. B
5 n; w, G8 f0 I! |( S9 B
视频格式转化,加载BGM
7 G9 p$ `& e6 h- A#df <- animate(ps, renderer = av_renderer('animation.mp4'), $ D% r l1 a6 O$ h6 W* B" l# X* y+ `' T
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
# i+ Y8 V4 _9 d$ ^$ v9 {# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
6 }1 E5 @( O) L$ L9 p8 ]$ o9 p全国新冠状肺炎26天增长状况/ ]$ q4 r2 n0 O2 c8 J) D7 M
! q! X' x2 a7 M" S
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
: C+ M& S# k, S/ D( c, R geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
9 _8 g t; e) H' s geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
' x& w5 d) |) ]1 [ theme(legend.position="none") +
' Z G! Z) C9 W1 J( h+ |2 H theme(panel.background=element_rect(fill='transparent'))+4 ?. {/ E5 W$ h+ F4 L( u6 K
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
2 l, Q' p- A9 e1 R( f- y+ E7 W theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+ V' n6 G- Q3 f; _$ N, b
theme(panel.grid =element_blank()) + ## 删去网格线# e E. s' D& c+ V# p, t2 X. A: ~
theme(axis.text = element_blank()) + ## 删去所有刻度标签
8 c4 D' h9 N5 W/ P' A4 _ theme(axis.ticks = element_blank()) + ## 删去所有刻度线* G3 g+ a$ ^6 J% e
#scale_x_continuous(limits = c(0,6))+( E+ Z: Z4 E& W$ f( M
# Here comes the gganimate specific bits
J7 `/ L5 Y7 v4 u. D# U6 ]& J#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
5 t% j- J1 {* I& `# v F; L9 J) l#annotate("text",x=0,y=40,label=C,parse=T)+
. }7 d' {5 V( f+ c # coord_flip()+
$ _8 _% B3 L, ]* B* { transition_manual(frames=as.Date(Date)) +3 B+ D& U1 E) N& E
# ggdark::dark_theme_bw() + #设置黑色主题
0 x: T5 @% |1 x% C; U6 n labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
) q$ C+ t' p% m, s/ h' h+ ^5 K ease_aes('linear')
0 ~+ w- R- A9 p/ r/ n( ~pc8 v0 E. z# z; W; u' o! e( b
![]()
k, z) f$ i6 I7 u" C$ c" H1 K7 N: ]. Y6 S
动态图合并
( w7 r3 R8 ]9 P$ g" g% |library(magick)
, C: j$ C1 S3 M& Z. Lps_gif <- animate(ps,,width = 720, height = 480)
3 E2 F& K5 q+ r) ^pc_gif <- animate(pc, width = 360, height = 480)
3 h) Y2 o. T P6 b# u& wps_gifs <- image_read(ps_gif)0 V3 {$ A6 _' w1 s+ u) H+ P F
pc_gifs <- image_read(pc_gif)0 p5 B2 V8 h! A* E) D2 r
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))9 i, a) r0 e$ e+ ~* J6 F
for(i in 2:length(pc_gifs)){
: @0 F6 ~8 p% G combined <- image_append(c(pc_gifs, ps_gifs))3 n& H; u- ]4 a# d M/ ]
new_gif <- c(new_gif, combined)
/ e8 E0 ~# N) x6 [}
# v7 G1 z6 I/ n! J6 H5 Lnew_gif
9 s0 [; X0 j" m* b, W结果展示
% u; C' G1 @3 y: P% Q$ U# E2 f8 c" P B
![]()
6 `$ E; f( c' m0 D
- F9 i$ O, e0 [5 w, B( C |
zan
|