- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40245 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12785
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
o$ H! S& E- N9 x7 ?, g- U所需加载包
& N+ @ v7 x" R3 vlibrary(av)
n2 G, L' t4 y# ~) ulibrary(ggplot2)
7 z8 V; X8 x( [, Tlibrary(gganimate)
! |( G( S) \" ulibrary(tidyverse)9 m- }* I' \2 g
library(lubridate)7 y! f* r( W2 I
library(scales)! S! a" V8 h2 [
library(ggrepel)
8 w$ q9 H& V- klibrary(cowplot); t) {( o8 w: R$ p" [6 i! ?
数据7 y9 }# j! w J
+ X5 N7 D }( q$ d8 m5 l
![]()
* o& m- J8 t) b4 |- g. l$ f: sps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
- b# F, U2 G& _# e1 f geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) + [( y( I6 P1 R+ W" y
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+% m9 ?/ a: S9 j* ]: Z" o% z0 e
#theme_bw()+, w5 L; g A4 x+ n
#theme(legend.position="none") +( |2 m+ V5 B. _5 ]7 Y3 ?9 n" B
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +- C* q& u ]6 D) d1 J4 \" K$ {
theme(panel.background=element_rect(fill='transparent'))+
. e u) R, |' i+ x0 o8 h. ^( t theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
& F1 ?6 z2 C1 A) d8 e0 b* P theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
; o: j9 p# e- U theme(panel.grid =element_blank()) + ## 删去网格线
# ]$ v9 H0 m9 L$ b' \ theme(axis.text = element_blank()) + ## 删去所有刻度标签
) b U H' k: b theme(axis.ticks = element_blank()) + ## 删去所有刻度线2 i1 m; C/ o3 Q1 I" [4 o
# Here comes the gganimate specific bits
& [7 Z6 K. j, }0 M0 u#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
: U, D6 c4 a. \( k4 B, L& ]1 [#annotate("text",x=0,y=40,label=C,parse=T)+
5 p2 B' N2 L, N, L6 W) G7 O$ m! A( scoord_flip()++ ^0 p+ V7 e0 Q* R s) }* d; `
transition_manual(frames=as.Date(Date)) +
7 @; E3 w9 f2 L+ Y% ~3 Y#ggdark::dark_theme_bw() + #设置黑色主题+ ?9 B: W3 o* O' p2 U. u4 J
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+8 V0 w! f* h( w' z) e, F
ease_aes('linear')1 J: [! |1 F d
ps
6 x9 N- g7 E5 [7 p3 K# L结果展示$ Q9 A1 [. W1 @0 F# |$ K2 s' i
! P" P; D9 [5 N
$ ]2 R7 f/ v) O6 T
视频格式转化,加载BGM
3 l* i6 q7 o( l4 v1 G4 I4 X/ ]#df <- animate(ps, renderer = av_renderer('animation.mp4'), . ~. i( K" k2 y
# width = 1280, height = 720, res = 100, fps = 10)#视频制作# D+ ]1 ~: I- ]: m
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")2 ]9 J6 m& y1 J& ~/ m
全国新冠状肺炎26天增长状况1 b( q# p# j* A
% V5 J% L1 O. b2 O; C }" h
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+! Q" ?1 ?4 x! q2 s1 ^
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
- D; e7 v6 E/ s4 k4 N, _ geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+! H2 ~; j: q5 U; G
theme(legend.position="none") +
0 U% y4 L3 p5 y, _+ J% j theme(panel.background=element_rect(fill='transparent'))+
0 Y5 x) V* l/ Y' G6 V5 V; ]7 I theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+ ~$ G$ y) \' _1 N7 d
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+! V( ^8 ]4 S) J
theme(panel.grid =element_blank()) + ## 删去网格线3 u2 L8 L9 x( b% v
theme(axis.text = element_blank()) + ## 删去所有刻度标签
4 b, M0 ], [" r theme(axis.ticks = element_blank()) + ## 删去所有刻度线3 K* x- X9 D( e( L. H! b6 ?
#scale_x_continuous(limits = c(0,6))+3 J( a9 _0 A8 O2 h5 o2 e) M" r( P0 l
# Here comes the gganimate specific bits
) `) m8 n+ L* D: d% z* | W#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
7 m6 L2 u7 e3 f# ~% Y5 ?3 O3 P4 H#annotate("text",x=0,y=40,label=C,parse=T)+
4 J0 d8 g' U N2 x # coord_flip()+
5 B/ B1 e: { q7 b4 ^ transition_manual(frames=as.Date(Date)) +
0 o2 X3 m/ R! S6 C1 z # ggdark::dark_theme_bw() + #设置黑色主题7 U, Y2 B& G0 ~0 C; {
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+. \. B. D6 A9 x7 y- R
ease_aes('linear')$ n! I0 q5 {: |4 G$ }9 p1 I& V
pc
7 S9 C1 N- x9 q) C " @5 e9 x( O. K" M! ^! O
% R& G' Z G2 h5 C1 `9 l2 c7 o$ S
动态图合并8 q* H, @: R, G
library(magick), C7 ]% U+ B" h. S# y
ps_gif <- animate(ps,,width = 720, height = 480)- Q6 V" R! e, i8 @9 o
pc_gif <- animate(pc, width = 360, height = 480)* c* S0 U$ p: ~
ps_gifs <- image_read(ps_gif)
( I. d( y. t9 G o1 M5 u1 spc_gifs <- image_read(pc_gif)' a7 X/ Q( C% l8 X0 s/ |% s
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))1 X% r6 O% X/ n! w0 `
for(i in 2:length(pc_gifs)){
( H r* q. ?* X) O combined <- image_append(c(pc_gifs, ps_gifs))
( M' T, x; S9 u8 B" T# E8 } new_gif <- c(new_gif, combined), k2 y( D6 |9 H: H# H7 o) q
}
9 P' Q( I' Z7 a+ D( hnew_gif
# Z$ r R, {( ^! s# C结果展示
& i5 n9 y2 T3 @- A+ A1 P# X9 p, v. A9 H/ F1 m3 A2 z/ l. \' L
, K5 F0 q4 {1 R
# Z$ M& v& y* X, ~" r+ l
|
zan
|