- 在线时间
- 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学习% ~7 w8 C+ q0 z
所需加载包4 M: w& d7 ^" ?9 k d
library(av)
6 Z3 I+ m( `% K6 H, ^% llibrary(ggplot2)
: E9 S: |6 H) K0 D' f/ D$ |library(gganimate)
( a* U& x# k# d% a* blibrary(tidyverse)5 _; X' X% u5 J# ^0 H4 I
library(lubridate). B/ M& }9 B! V! ~" o
library(scales)* }( W, S# O; c! u/ g
library(ggrepel)
2 O# Z, l# M1 y6 A. I) slibrary(cowplot)( c' m% w2 S! x6 c
数据, R+ y& E4 O* @% c/ M/ ]8 N
. t: l; m' T# ~' n5 y+ A1 \6 q8 R j
4 D- F) v q& s+ L' Z1 y( x; G
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
5 M( H& v, @- d( \" v7 `# n geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
, z9 N# p G, X6 B+ b geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+7 K0 f1 w. ?9 w4 S4 N3 t [0 T
#theme_bw()+8 F' Z! X# u, U0 t
#theme(legend.position="none") +
) g; u( y& `0 ~' k; Q2 P! q- q7 f theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +& |/ m" {6 i4 |8 Y- b; H+ F
theme(panel.background=element_rect(fill='transparent'))+" X% w8 K$ c& H" b% ?# R
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+$ K" W! _! q3 i
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+" ^4 v* E5 d7 V8 a& T
theme(panel.grid =element_blank()) + ## 删去网格线7 Y' E) v/ y+ ] C
theme(axis.text = element_blank()) + ## 删去所有刻度标签* |9 @( ~: Y4 R$ V% L( r
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
9 @" w' }% ~) u! L" n+ t* i Q# Here comes the gganimate specific bits
+ M0 }. B& B: m. z b1 T8 U# i& I7 t#labs(title = '日期:', x = '省份', y = '累计确诊病例') ++ `, ]. o+ l6 r) Z! ~6 ^9 E
#annotate("text",x=0,y=40,label=C,parse=T)+1 o& L# ^% F7 p& \1 ]
coord_flip()++ G& x/ W; O, L% k
transition_manual(frames=as.Date(Date)) +
1 \. G/ U1 W, d#ggdark::dark_theme_bw() + #设置黑色主题( I+ ]7 W7 }" X0 l/ u4 b
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
5 l+ ^ Q5 D4 V" n* A$ Dease_aes('linear')- Z. p- P9 m: e# ]+ U- [/ |: b
ps
+ v4 j2 [+ n9 g结果展示' o7 B' h' ~9 G& p! }
2 t* W h; V4 b& E/ b
![]()
/ \& }6 @# z/ v" L9 I视频格式转化,加载BGM6 _' I8 Z) r d/ c1 A
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
$ _* f8 M# M9 n# Y. L" j # width = 1280, height = 720, res = 100, fps = 10)#视频制作
6 j# s7 F! A( O# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
2 I" |9 v/ ?& t3 U全国新冠状肺炎26天增长状况
3 K0 O' K s' b( y9 \+ q& V0 T3 O: `: ]
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+# v. E; w4 R0 K
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +3 R5 \* w4 X" _# F' ^' G2 {
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
5 b' p- D9 S( o& S theme(legend.position="none") +
+ G1 W% z, V* e5 U" D theme(panel.background=element_rect(fill='transparent'))+; T+ V6 [7 b7 x: V
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
) L% Z; u; S% i) a6 q theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+" A5 z" Q* N, R, x$ O
theme(panel.grid =element_blank()) + ## 删去网格线0 H7 T3 U% E, G2 _- C* M
theme(axis.text = element_blank()) + ## 删去所有刻度标签7 t3 `. { s8 d" o- d
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
, h- G( `4 C9 w6 z #scale_x_continuous(limits = c(0,6))+
% d6 K) g" ]# A# s. S, \ # Here comes the gganimate specific bits
# b' p: ]- I# N1 }; N2 w#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
1 y3 i( [# j9 x. k, a#annotate("text",x=0,y=40,label=C,parse=T)+
- H/ {6 ]+ g% D3 v # coord_flip()+
4 k0 }& x0 E' M" I0 D transition_manual(frames=as.Date(Date)) +. W9 O9 p$ X) |# \6 S
# ggdark::dark_theme_bw() + #设置黑色主题1 n6 _' G+ }" ~- [3 x( Y6 _5 l
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
$ h; H5 f0 m) B) G. {* _6 e ease_aes('linear')
* K% L7 M: `* Y0 S2 t" \pc+ i9 a- o @, m. {* v7 ]
5 {+ J, l9 Q, ]# k1 x
& L# k1 [' o$ K) _: G动态图合并
' Z' T/ R2 y( x/ plibrary(magick)7 F7 K& `4 ^ M. m/ _
ps_gif <- animate(ps,,width = 720, height = 480): m0 d5 K+ ]5 `( D5 j/ ]0 j
pc_gif <- animate(pc, width = 360, height = 480)5 E" I2 M1 ?5 U2 M a& V3 n
ps_gifs <- image_read(ps_gif)2 n! T4 B9 n( x) r
pc_gifs <- image_read(pc_gif)3 Y2 O& {* ]8 J- K
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1])): c$ H9 | Y" F' m2 u
for(i in 2:length(pc_gifs)){
" H/ j) P. g& p) r) }1 o combined <- image_append(c(pc_gifs, ps_gifs))$ C2 h, `( j! p
new_gif <- c(new_gif, combined)
& H; `4 Z0 z' p9 b- s# f G# q}
+ H2 C. a1 n6 r. d }- J/ `1 K0 k7 hnew_gif
8 h' F$ S, w# B( y3 P6 o结果展示
2 {) X( C/ d! H% Y/ m, ]. U. Z1 O8 b& R, |
* P5 f. `$ H) A$ k$ ]. Y1 d
6 N8 d8 i% q7 `
|
zan
|