- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 39951 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12696
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习 j; w9 n0 J k. w
所需加载包
% b& S0 ]3 v. y# D4 }8 c' alibrary(av)
% y. G, L6 |% ]; E2 ?8 zlibrary(ggplot2)
( U9 b8 T" U5 ~2 j; slibrary(gganimate)
7 p8 z( a# v/ g3 Llibrary(tidyverse)9 B+ M$ u; x3 |% x* p" {% ^
library(lubridate)
4 J( E! `! ^0 s; mlibrary(scales)8 S# e' ]: w- e( J5 `. x, p& T
library(ggrepel)" D9 ]) h1 ~( g& t4 P4 Z
library(cowplot)+ b8 J, y2 Z1 J l
数据
/ m* |+ l7 N! E5 I6 `+ k& q) U, K5 \4 m! @) r' n) t' g
![]()
. U+ i" N4 d) L; U3 v* K2 _ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +% X% R5 B( ?( y$ v
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +# _3 A0 ~ [ @8 z5 k
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
2 j7 K \! `- v" l #theme_bw()+6 V- ^8 ~6 Q4 _ s k/ ~
#theme(legend.position="none") +
' n% E# h( B, S; T& x/ g* o( W$ V theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
* V4 T/ [/ u. \8 T# j; L theme(panel.background=element_rect(fill='transparent'))+
4 n% W. P' u5 e6 ?# e2 Y6 [* K theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+" s' g( f* U& G
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+8 C9 P5 {9 V; _" M) K( _
theme(panel.grid =element_blank()) + ## 删去网格线
) c$ n& m; F* m theme(axis.text = element_blank()) + ## 删去所有刻度标签5 d+ O0 n' S: F* c* g6 T8 q2 H+ r
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
8 |, e l+ m- O8 k# A7 }* q# I7 V" _- p# Here comes the gganimate specific bits
+ a! Z% i5 k' T/ Y& d% s+ W#labs(title = '日期:', x = '省份', y = '累计确诊病例') + ^2 z' c: h( X2 A
#annotate("text",x=0,y=40,label=C,parse=T)+: |% H! g: d% J f' [0 \2 P
coord_flip()+7 L4 L) N# v4 s6 L" o$ W+ T8 m
transition_manual(frames=as.Date(Date)) +( T6 N% N8 D0 _, h8 L3 z( v2 O
#ggdark::dark_theme_bw() + #设置黑色主题 S2 u* r: T: J7 ~) {/ B
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
' |5 U% x+ r" a6 X/ U' V8 m# X9 \; Z5 \ease_aes('linear')
3 r) h1 ?! d' ^+ X+ Lps
9 N- K. ~! h/ @$ o- k; U( ~结果展示; J# H# Y2 t* [/ l( c
" }$ i) }% e J; W% T 9 L& l/ R" C, b
视频格式转化,加载BGM
5 k. Y) t* q; F" j#df <- animate(ps, renderer = av_renderer('animation.mp4'),
+ v7 T) v9 d" ] # width = 1280, height = 720, res = 100, fps = 10)#视频制作
& _8 ^' f$ R) q* u) c# [) F# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")* J8 k. \6 A/ Y9 {7 K
全国新冠状肺炎26天增长状况' z* A2 t3 _' {
) Y$ d2 ~# k+ I5 x) Q. n
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
8 K! K6 f" z# O! C* ~# Y4 s7 ^ geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
- A4 j6 V3 T$ @ geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+& j# h% z4 ~8 k, ?
theme(legend.position="none") +
, }: U/ N1 @- I4 N2 ]8 N" o! X theme(panel.background=element_rect(fill='transparent'))+
! W) |, @: K3 w: _7 A theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
' l2 M( \6 B# p1 c" L4 m0 M theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+% r3 C' M: x& M$ {7 {2 f0 m
theme(panel.grid =element_blank()) + ## 删去网格线+ s- w4 f0 q7 J0 q
theme(axis.text = element_blank()) + ## 删去所有刻度标签
9 d @2 U9 H6 S( u theme(axis.ticks = element_blank()) + ## 删去所有刻度线
. {" M |4 x2 s; q/ y" k! ? #scale_x_continuous(limits = c(0,6))+
6 t4 g2 p1 E- W/ f5 \ M- d # Here comes the gganimate specific bits: R ], Q/ |0 C* j: a
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
6 k) w9 M3 K1 L( a1 S5 y0 G#annotate("text",x=0,y=40,label=C,parse=T)+; G0 J, p" V0 G
# coord_flip()+
# Z# |$ J2 N; \1 x2 L+ e; G C transition_manual(frames=as.Date(Date)) +
+ ~2 o* q5 a+ v1 b3 f+ }4 w2 a # ggdark::dark_theme_bw() + #设置黑色主题
9 s" B3 k0 I; E8 x& ^# @) r labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
& }" o+ d5 k" B" W8 m/ D2 n! d" T ease_aes('linear')" x& Q* ~' o1 _( C
pc5 f! [) }" ?' s: x6 Y
. |- @) N6 L6 @; R
1 r( w* ]9 W$ B5 @; ?
动态图合并
; b! f. |% m( N( x1 l) f# ulibrary(magick)" c8 O$ ^5 ~7 t0 c+ l7 n6 |
ps_gif <- animate(ps,,width = 720, height = 480)
3 _& G6 f; f# b3 \* }pc_gif <- animate(pc, width = 360, height = 480)
I4 y! c8 X, I, s. zps_gifs <- image_read(ps_gif)! n. [0 X& c$ n: g
pc_gifs <- image_read(pc_gif)
" H9 w- K" ^1 d5 B* Z, {) Y# dnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
[8 I& z! R+ pfor(i in 2:length(pc_gifs)){( ]& J& Q0 e/ g" U5 a
combined <- image_append(c(pc_gifs, ps_gifs))
2 \% Q2 C; w8 w new_gif <- c(new_gif, combined)
( ?9 `4 t. d+ {( F$ i' S- u}2 {' D" \' u6 D
new_gif H2 S% g" Z$ |! M* K' `
结果展示' D# V7 ?8 v' E/ Q/ n
q- B8 n3 s$ D0 _, z' b7 Z 0 w; q& l5 z# f: X6 \, @2 ]8 Y
. L' c3 Z; b% k2 Q. b
|
zan
|