- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40222 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12778
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习; c2 b0 P9 L' e$ ?% X
所需加载包
/ O4 P3 Q5 O. Llibrary(av)5 q' D3 U* A( b2 Y7 X
library(ggplot2)9 P4 T2 [ G" B' G4 X3 G' b
library(gganimate)
+ M; A1 G+ R: M+ S& N& v2 q wlibrary(tidyverse)
" ~1 t0 ?0 c' alibrary(lubridate)2 O/ {6 L3 a/ ?( Q5 ^- w3 ^
library(scales)
' Z! |% ?* _7 K2 H$ ?library(ggrepel)$ d5 {4 }& [! [! j9 B2 A/ {
library(cowplot) m U1 g7 C) b6 ^2 [- @4 g
数据
' M X& M8 e! B$ H l. B( b' F0 J
![]()
! X" c! T2 J- Aps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +. Q4 _ P. n. N, h6 @
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +9 a0 l, w8 [* t# q
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+0 S1 r& D0 \; D3 v X3 s! K
#theme_bw()++ F4 _% m4 i ]$ N( Y$ W7 N
#theme(legend.position="none") +7 g5 d) {" m7 n' l9 ?
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
! I( `- s$ k8 ?: w7 s7 p theme(panel.background=element_rect(fill='transparent'))+
; p7 b5 {0 m3 F- K+ | theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+& V9 `" q! z. c. \5 e
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+; r: X; G0 j6 v& I
theme(panel.grid =element_blank()) + ## 删去网格线+ n& c3 o' O0 B- ?8 @: w8 C
theme(axis.text = element_blank()) + ## 删去所有刻度标签4 f. ^* D- Y' B2 n, D
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
4 B# Z+ g6 n& ^( Q7 @- y4 n1 y# Here comes the gganimate specific bits& e. S) g) L: p
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
. `' C7 [# Z! V- N1 n#annotate("text",x=0,y=40,label=C,parse=T)+
w2 ? Y+ ^) C; m% qcoord_flip()+
0 S6 E3 S4 w* o4 {6 t; y: ttransition_manual(frames=as.Date(Date)) +
! U' B/ i& t, `2 `#ggdark::dark_theme_bw() + #设置黑色主题
" ?. }! ]3 q$ P- M) Ylabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
2 g1 S( f7 u1 e5 N! W) {$ Dease_aes('linear')! B m4 k: Y4 z! J4 @ m- ]$ O; ~
ps
6 u+ R6 V" o9 `' |% M# B结果展示6 P) ~4 y1 \5 |# A6 v, y
}/ {3 c6 H, p! W" j- j! m$ O+ L
![]()
8 g2 Y1 ~" p' n5 T0 b6 F视频格式转化,加载BGM1 h, H. X8 @$ a$ I& O1 f
#df <- animate(ps, renderer = av_renderer('animation.mp4'), ( x) |& J- R( ~6 R# J X
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
! a8 {4 k# Z& x5 h; R5 c, H! h# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
1 B$ I" }# P1 i8 L2 P7 Z4 D全国新冠状肺炎26天增长状况. e1 H9 ~! t2 u
( g% O- X7 w) L: c2 Fpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
$ h" E% I2 Y9 t# s& H geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +5 s. {# D: h: c Q8 u
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+4 e( `5 j4 e# n) ]* F6 ?- M& h, j
theme(legend.position="none") +
! h" C& ~$ ?* t( S Y% c! M theme(panel.background=element_rect(fill='transparent'))+- Q, r( J" d: V0 b" B
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
0 u2 |- L% Y* M! V, w theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+9 J' i4 ~; ^3 h7 Y* v7 g
theme(panel.grid =element_blank()) + ## 删去网格线
3 W# K7 @4 P, W- ` I; ] theme(axis.text = element_blank()) + ## 删去所有刻度标签' t: F5 w; Y8 s$ g
theme(axis.ticks = element_blank()) + ## 删去所有刻度线, x6 ]) |! p+ Q
#scale_x_continuous(limits = c(0,6))+# J6 Z0 F( f/ j# u1 N: ? u
# Here comes the gganimate specific bits# o; m8 `2 m' D! [: b! W/ ]! U2 W
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +5 U# k6 T9 t; i
#annotate("text",x=0,y=40,label=C,parse=T)+3 l8 Z7 C0 [2 Y+ q* o$ o
# coord_flip()+
- X0 f, W* e8 Y. h transition_manual(frames=as.Date(Date)) +
1 B( ^: B' E9 g # ggdark::dark_theme_bw() + #设置黑色主题
6 m0 `. h8 [! D8 D labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+8 ]* y/ p, p. [# Z2 p
ease_aes('linear')0 r& p, G! X x- y' T
pc
6 W3 |5 f9 O' V- W% ?9 }+ N * y: I6 ^( W/ y
4 ?- r& l5 s* k9 C" ~9 q动态图合并
, o5 q# ], r; t% L' N" Blibrary(magick)5 z0 J" w) `& B' X
ps_gif <- animate(ps,,width = 720, height = 480)% B0 c5 Z3 A4 a2 u
pc_gif <- animate(pc, width = 360, height = 480)
! f5 d% ~6 o: I9 E+ R8 L4 Q! pps_gifs <- image_read(ps_gif)# P, P6 n) l$ a+ Q# A( R
pc_gifs <- image_read(pc_gif)
8 F$ d- z* d" _3 e9 znew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
3 V. G+ P2 ?/ O4 l! Cfor(i in 2:length(pc_gifs)){
8 P( P" t' c; `; O, G+ j* s' \ combined <- image_append(c(pc_gifs, ps_gifs)); {; H/ A7 Z% Z }0 z9 f9 @
new_gif <- c(new_gif, combined)2 {' v) @7 E K
}- ?1 z) N: Z4 c, c }& u
new_gif
3 o" B3 r! M6 N' C8 }结果展示. j5 z& V* B6 ~' f' s/ y# D' f! J( @1 d
) c- X+ s( q3 `3 S5 h% B% x0 b) J % ~! r1 v+ N" _) H
) \8 h; f1 }0 g7 k# Z& I# D) ^; L, m |
zan
|