- 在线时间
- 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学习9 W9 e0 r* \7 U! z
所需加载包
; K2 ^7 `: B, ~- X+ Y- K! nlibrary(av)+ K+ Q! g# e0 E: n! a. ]0 u
library(ggplot2)+ f( P$ Y6 _/ A
library(gganimate)
0 g5 j& B- p& L7 ^( t7 ]3 Llibrary(tidyverse)4 Z2 B) h, K- z% ~
library(lubridate) l4 n& |5 l( m; H" Y7 {2 e! p
library(scales)0 T$ @8 \" o% u$ g/ b' J/ a2 f
library(ggrepel)4 F# O2 b9 c* J: ~3 a# f- P
library(cowplot)
5 t% Z3 a* x/ i: \0 d数据 w8 Z2 j2 \/ Z9 `1 q
/ j/ p! y1 G" w0 t" p# U 4 O' E/ u7 Z) ]
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +, [* F8 W: a) `* q& J
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +. l. y4 f4 g# j5 i0 `
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+1 l, N" W5 t1 m. [2 e! B- ?
#theme_bw()+8 g9 P+ ? ~0 B5 g4 D+ d5 N) k5 l9 _
#theme(legend.position="none") +
$ q' t; M9 d2 {& `2 K theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
" a. L* M- c1 j6 { theme(panel.background=element_rect(fill='transparent'))+) n6 G" v, _; B+ G
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+2 g# n3 v5 X' H: O5 k
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
) x$ Q2 f" [# k+ U theme(panel.grid =element_blank()) + ## 删去网格线
, F6 m! K! W0 Q8 Z theme(axis.text = element_blank()) + ## 删去所有刻度标签1 o, p& [1 E4 F) }# W- L
theme(axis.ticks = element_blank()) + ## 删去所有刻度线( v! l" P/ R2 |! {. F2 P
# Here comes the gganimate specific bits7 J! C8 M- r; L+ m' W# }( a
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
- t4 ?2 U9 p) J1 h$ k$ l#annotate("text",x=0,y=40,label=C,parse=T)+
, o/ i8 W1 O# n4 t; _; e& Fcoord_flip()+: D1 [$ ~; d" l& c
transition_manual(frames=as.Date(Date)) +
' ?; N4 u+ j8 n$ G1 @#ggdark::dark_theme_bw() + #设置黑色主题
1 E' t1 e5 |* J( D1 W3 u) R( h. _. Blabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
4 b2 ~6 K, ^9 A/ ^' E- Bease_aes('linear')2 F8 E5 B. t. @% M4 Y2 z
ps
# }. b6 [ u& g% Z8 h( f( x3 @结果展示
) D, Q& p9 z+ L/ p
7 ^0 H! k; |) {. s: D2 P. n1 c & ?: M! I S& z( Q% w" M
视频格式转化,加载BGM
' }$ m, K6 J+ F1 |* f! a#df <- animate(ps, renderer = av_renderer('animation.mp4'),
: K2 d$ S9 z" n7 m+ r # width = 1280, height = 720, res = 100, fps = 10)#视频制作
; y' P2 N3 d p0 H# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")0 I0 q: p5 L7 w" h
全国新冠状肺炎26天增长状况! ~/ e, j: j/ b; f5 o
: F! h" @7 T: X$ L/ Dpc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
2 f# ~% K+ q8 ?4 j U! I% ^1 [& a geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
# A" v3 v7 w$ ^" y' h( g geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+& R9 w; a! |$ d) y0 s: A& g9 t* n
theme(legend.position="none") +. T8 o# Y' b( S, T
theme(panel.background=element_rect(fill='transparent'))+. u4 p9 k: g7 j Y/ a% r9 H
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+4 p0 O1 e/ }& ]4 k
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+( u! n% M9 C9 f* m
theme(panel.grid =element_blank()) + ## 删去网格线
1 Y) B) N Y( {# l+ H" f theme(axis.text = element_blank()) + ## 删去所有刻度标签7 q' }1 X* a! O( `: n
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
1 A8 F6 X% |& \* q #scale_x_continuous(limits = c(0,6))+5 z2 D+ v* [; G
# Here comes the gganimate specific bits
+ F( C, v. t I, W/ ~+ |# x#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
- n# o9 h) M' O* z6 K- W#annotate("text",x=0,y=40,label=C,parse=T)+5 |" ^: i/ u- p9 w
# coord_flip()+
. d- s5 z0 A' D% @- U0 q9 { transition_manual(frames=as.Date(Date)) +
* y( m/ b5 T& f7 c! I" p: h# } # ggdark::dark_theme_bw() + #设置黑色主题: Y e0 h7 A6 p) D
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+7 E, Q% S2 [. U
ease_aes('linear'); J- `1 D4 J0 P' c- I+ `
pc, y' O: d7 ^4 Z2 ~/ k. S( f4 y% e3 a
![]()
; d/ V% j6 ?& f+ y! \2 H* q9 L Z3 P& s0 v/ [ E8 b
动态图合并3 r+ z/ N, [; c; `
library(magick)
1 P2 f0 a/ Y$ c lps_gif <- animate(ps,,width = 720, height = 480)
2 T7 B) {8 L$ l& opc_gif <- animate(pc, width = 360, height = 480)
+ w9 s* f" x. Y8 H8 I9 Z: y7 n: tps_gifs <- image_read(ps_gif)
# h, \1 m5 m9 h( N% e4 ? X3 Spc_gifs <- image_read(pc_gif)
7 T% P0 |3 V g8 ^! ~ ?6 u. snew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
" u9 r3 a+ T3 ]1 ]4 |for(i in 2:length(pc_gifs)){
$ Q8 P. n5 K/ Y. ~/ z8 U6 B+ r combined <- image_append(c(pc_gifs, ps_gifs))
$ D0 c9 @$ g4 |* ]; v0 v! L new_gif <- c(new_gif, combined)
' t$ U9 l6 W1 }$ J, |& }}
' g% ~1 y+ [- l; T# \new_gif
9 w- T$ K4 u, I }结果展示
1 y+ _ m4 C2 d. a# h8 [' u+ _/ Z( A
" G+ Q5 r$ f( {7 h* Y 7 G" [8 c6 f+ C* \7 @+ @" n
' \1 d9 J+ ~6 g1 R, z: C7 e |
zan
|