- 在线时间
- 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学习# z, q2 W; {! g4 |! ?% i
所需加载包8 i( r+ B8 g2 ?
library(av); e9 K ?$ E" U i5 J2 ^
library(ggplot2)- ]* a/ a7 ]) U) r9 [3 Q
library(gganimate)1 U4 @. E- h! H3 E3 K
library(tidyverse)4 Q" D+ I4 d& A1 t) }( Y. \6 F
library(lubridate). [+ {1 x$ p3 G' b' }
library(scales)) M: H; J- B+ c0 B
library(ggrepel)
2 o' P4 \! I; c% G. X! A' ilibrary(cowplot)
( N# j$ A0 i4 k; R数据8 O. D" {5 u6 `5 z
$ P$ I8 v: A K# D& b# N# e
9 A% i4 [! T/ O+ V v! P8 V
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
$ o4 Z* j2 y$ s geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
6 a' T( E/ R( [- ?9 F geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+" d# e0 ?% d9 |% @3 W; Z
#theme_bw()+4 h! ^! @0 B) g' @; n
#theme(legend.position="none") +& @! N# s5 S' l- X" h, R
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
" s; h* |: A6 T4 l! ?+ \$ ?& S. B; Y theme(panel.background=element_rect(fill='transparent'))+0 E; p& J% ]3 D' ^3 y; ]6 e
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
9 e2 f+ G; z2 ?- S theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+# P7 H& V0 M r: g s
theme(panel.grid =element_blank()) + ## 删去网格线
4 v5 H( z* c( r$ i0 L. W& V# p( Z theme(axis.text = element_blank()) + ## 删去所有刻度标签
! t0 X% S! q' z( P theme(axis.ticks = element_blank()) + ## 删去所有刻度线 p) {: V( W) D( N8 V2 _! E# P+ W
# Here comes the gganimate specific bits' ^1 @/ t6 @8 X
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
4 g! t3 a( c% d( T#annotate("text",x=0,y=40,label=C,parse=T)+
0 J3 d( A7 ^# q) K$ T8 Fcoord_flip()+6 c: f" \+ G: @4 N5 G P9 s: [
transition_manual(frames=as.Date(Date)) +
, R( e7 D3 @; W$ s#ggdark::dark_theme_bw() + #设置黑色主题
; L W& l7 E/ k, {$ `labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
) H& }& r' N6 `" i' n% h3 Uease_aes('linear')
$ W, y6 T& _2 {6 ]0 Hps
$ [" J" D ^- [' N" k9 N$ R: W: ?结果展示# z/ Z$ D: b* ^) X) b% P
- K" `4 n. |- D8 z
( J9 F. V8 B, G# { ` x& T
视频格式转化,加载BGM
& ~8 s/ c" `1 P#df <- animate(ps, renderer = av_renderer('animation.mp4'),
" \$ o3 K( ~: Q) K# H # width = 1280, height = 720, res = 100, fps = 10)#视频制作
+ n" A5 _9 C- h) e$ n7 j+ X# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
% a% z7 M3 p5 m; M! \全国新冠状肺炎26天增长状况
Y+ U% s/ t+ H/ P& w
! V/ f& ?% J9 P# A3 Spc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
9 ~$ Y- w9 V2 ^( V) a! k geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +! Q9 X: c0 M5 R
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+/ I4 _4 I( z( C% a9 ?; L
theme(legend.position="none") ++ X( S1 x5 L+ f! Z9 M9 U7 B! q
theme(panel.background=element_rect(fill='transparent'))+3 [. {3 {5 U! u) g+ x, {. N
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
" E9 h2 v+ K& ]- {2 N theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+; w8 p, f" {7 y! G
theme(panel.grid =element_blank()) + ## 删去网格线
- x$ ]9 H; z) c+ b5 N theme(axis.text = element_blank()) + ## 删去所有刻度标签
0 g4 ?: d' X$ {' v- {* T theme(axis.ticks = element_blank()) + ## 删去所有刻度线
: ` w0 S5 \$ U% c #scale_x_continuous(limits = c(0,6))+4 w5 f. T7 C- W7 {! f2 A7 p1 H* Z
# Here comes the gganimate specific bits
: |1 j# _6 N, N" C#labs(title = '日期:', x = '省份', y = '累计确诊病例') +) P) R% p1 D) V& q$ ]( f
#annotate("text",x=0,y=40,label=C,parse=T)+) g/ m {2 G. @" L; |9 L1 [5 M
# coord_flip()+
# z4 K% }2 t- C* d! i transition_manual(frames=as.Date(Date)) +8 p4 L, z0 G0 S' p( F
# ggdark::dark_theme_bw() + #设置黑色主题1 D5 i" [( _+ B7 {# |) D U
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+" v& {' t9 W+ q- q$ K, D. }* F3 {
ease_aes('linear')& g* Y* ?' [7 k/ R3 j
pc
8 ] K7 a w8 a1 V; c![]()
) t% \' j6 v) H/ o5 X7 N1 V
0 V& P$ L% r7 w' o8 s' I) @9 V. q动态图合并
+ {& g4 ]9 X( wlibrary(magick)" L7 O0 u6 D% W) A
ps_gif <- animate(ps,,width = 720, height = 480)+ V1 l* o# P$ L( X$ _
pc_gif <- animate(pc, width = 360, height = 480)$ j. r% ?8 Y: L; y! {: F( G% o0 x
ps_gifs <- image_read(ps_gif)) d- ]- n% T; m* E
pc_gifs <- image_read(pc_gif)0 g3 m. y1 R- t7 K" |9 S0 c. ?
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))- }7 ?, p! b7 Y; ^4 A2 H2 W* P
for(i in 2:length(pc_gifs)){
3 H: ?$ l& t! R combined <- image_append(c(pc_gifs, ps_gifs))
. [5 I+ u" G) N& | new_gif <- c(new_gif, combined)4 w8 g/ k7 ]( z2 y& g' E
}0 `! X5 d: t3 B) p0 q# U1 W! O
new_gif" Q% q+ h' U% h4 c! @# J( B
结果展示
9 o# U8 n0 b& F- |& M4 ]6 ~5 x9 _8 u
![]()
. H6 I2 a( k; E- A; D0 ]+ _% [2 }+ y1 B7 G# Z0 {. E- f" U
|
zan
|