- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40294 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12799
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习3 [) R# o0 x: n' J
所需加载包, }5 G8 {3 \& U' Y! O3 i
library(av)
: D# _$ c6 h* c. M* ilibrary(ggplot2)
2 A0 C, A+ j k) Llibrary(gganimate)6 M4 M7 |. O1 _3 Y$ v! K7 U* l
library(tidyverse)
: X5 N! F$ T# \" T6 `library(lubridate)# ]- c2 J) W7 Z! M" k3 i- |
library(scales)
" q. H! [; X6 A# ^; n# u3 y1 Vlibrary(ggrepel)
% _$ w7 X" q0 k0 p T0 K1 D5 e+ c7 X7 klibrary(cowplot)
- D( Z% C& w5 |% ]; [数据
: W6 {% K4 k( p2 P- D2 {! }8 w' \ |' ~7 P
$ `5 L# [, P/ f5 V6 i; K
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
- J1 ^" E7 s# e3 V1 ] geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +% C8 t( b; }0 J/ \ E1 ^8 P
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+1 i+ ]3 W6 U w: @( u8 {7 q
#theme_bw()+, K, N5 ~5 |$ i8 T1 ?) ~
#theme(legend.position="none") +5 M, W# ?) {% _' J" U) a& {
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +2 W) |! {' r3 q e
theme(panel.background=element_rect(fill='transparent'))+( ]4 i/ n1 g" _/ u' @" a/ T- s
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
0 H2 K; J7 N4 N/ t6 g theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
- F! Q$ ~: x) @& Y5 f6 Z: \ theme(panel.grid =element_blank()) + ## 删去网格线' h5 d1 c! `; w. k1 e, V3 Q8 d
theme(axis.text = element_blank()) + ## 删去所有刻度标签
, S; R) w$ F/ U' D% K5 | theme(axis.ticks = element_blank()) + ## 删去所有刻度线
' X: e0 _: `: s; U9 T# Here comes the gganimate specific bits6 F' \# u2 N9 @( O
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +) ^) E4 T( y( E+ Y k% M( \
#annotate("text",x=0,y=40,label=C,parse=T)+
, q( A) E& a# Q9 |1 ]coord_flip()+% w; I7 {% K4 f: A
transition_manual(frames=as.Date(Date)) +
" E; d: i0 f* P5 v) `7 Y T#ggdark::dark_theme_bw() + #设置黑色主题
% e7 K6 C; L1 Q) ]labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
h2 L X' r7 a$ S A( iease_aes('linear')
7 `2 P" ?% w* X, k' w5 kps, z }2 d# N2 K7 P
结果展示5 y8 Z1 ~0 [) j3 a. P. ]; ?( p1 Z
* Q, q& n+ o; a, L6 k8 W# v5 z; R) O
![]()
9 K' L9 Q+ {" Q3 W9 G# Y视频格式转化,加载BGM$ @, ?; p# i- k. E# J: D8 z& a3 I
#df <- animate(ps, renderer = av_renderer('animation.mp4'),
9 ~, d; p8 Y' J C( Y1 p # width = 1280, height = 720, res = 100, fps = 10)#视频制作
5 M& @+ p8 X) ]# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")% w5 l6 R# J6 o. _, X
全国新冠状肺炎26天增长状况
; L+ a* w4 H# j( l* z/ @% m: {8 }# L9 s' n/ j& C1 m( n
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+3 w* J# L4 [7 a; r& U& \
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +( { Z0 x: _; N4 `& V
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+6 f# m, a( q# s
theme(legend.position="none") +% W+ Q0 b4 }/ `
theme(panel.background=element_rect(fill='transparent'))+2 f9 p" s K) b! R8 R& e
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
/ J+ Q( [' V4 w3 \ theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+2 w/ n5 |3 w/ L+ p9 v P v
theme(panel.grid =element_blank()) + ## 删去网格线
/ \+ B4 q! k2 F& G9 G2 `6 E theme(axis.text = element_blank()) + ## 删去所有刻度标签
4 b: M' @8 W9 E! p theme(axis.ticks = element_blank()) + ## 删去所有刻度线0 P# q2 T/ P8 p1 X7 Z1 B
#scale_x_continuous(limits = c(0,6))+
9 Z" \: [5 W* P) ^. q. v # Here comes the gganimate specific bits' Y# Z5 V( C% _2 [" y3 \+ W
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +0 o# `7 A" n& _3 d, D8 e* [
#annotate("text",x=0,y=40,label=C,parse=T)+# a6 q; I: w3 Z3 b& {4 ]
# coord_flip()+
! R3 {# s( ~: ~; x transition_manual(frames=as.Date(Date)) +
6 T K q% ^& ` # ggdark::dark_theme_bw() + #设置黑色主题% [) @; {0 A" n Z, i+ t
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+- U* H) o6 V( B
ease_aes('linear')- Y, x* a' n/ F, t; ]
pc4 r5 ~$ z0 ^0 o- s
![]()
$ c: T" W0 D' F, m0 ^4 P0 h, e( y' \+ N. q% ?* f
动态图合并. R. O9 C- _4 @9 S: E1 Y2 r
library(magick)
+ M4 g1 [, E- |- Ups_gif <- animate(ps,,width = 720, height = 480)
0 w- ?2 V6 q" i1 q- spc_gif <- animate(pc, width = 360, height = 480)
* Y* N. g1 b+ b. U/ y+ m `8 {1 l" R3 Aps_gifs <- image_read(ps_gif)+ I, m' |: }& b9 B; L0 K0 q+ D
pc_gifs <- image_read(pc_gif)
& L/ x; ?6 x E% v6 ?new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
: N0 Q) n7 ?" X$ C2 F, Hfor(i in 2:length(pc_gifs)){3 c: Y5 }" q3 x: _' D" B, U
combined <- image_append(c(pc_gifs, ps_gifs))2 M" }1 {/ E2 s( W, O8 Z$ c; C
new_gif <- c(new_gif, combined)
7 G/ x( ?) v( z( g7 C9 |}
5 O& R" R" a1 B+ c3 bnew_gif
' i5 x) B+ D' u* v- E1 z4 p结果展示5 h8 x9 z$ |: x' r! h F* L
7 e3 R* b' k& j$ _! L
![]()
* J9 G; @& s. ]/ H3 }* L1 {# U \% s! E7 l: g3 X/ X; K
|
zan
|