- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40325 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12809
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习
. R) P z2 E5 | p$ d所需加载包
, P8 J9 E" |- x5 b, F$ ^8 Olibrary(av)
: w% f4 Q# |2 O9 D: X5 P2 wlibrary(ggplot2)
) z0 o/ \ w& {library(gganimate)
5 @2 U7 `: I3 `6 S, G5 S7 s# k1 Llibrary(tidyverse)
; `" n: ?; S6 b% Y& Alibrary(lubridate)2 |- B# D2 p, L, q9 I" `
library(scales)# V U5 L$ z& O, N6 D
library(ggrepel)
6 O0 L, O$ @3 @ jlibrary(cowplot); y9 N0 c$ g' e9 o
数据
0 e# t, {7 L; j/ C% }5 y3 X+ e/ Y- n$ p4 z/ ~- @
![]()
9 u) e5 e8 a& r8 b: c; R# b0 dps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
# R) c1 N8 c. M8 Y( w( N7 ? geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
4 X6 l. j2 w' y. t+ e+ t1 Z% q" ~ geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+2 j# C3 S7 }/ Y
#theme_bw()+
2 i9 e+ U( C1 v$ U f2 U& j #theme(legend.position="none") +- c# D) g( F4 l9 l" m
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
$ D: l) ?3 S" }& `- h6 T' G' Y! { theme(panel.background=element_rect(fill='transparent'))+
8 }( Q: s% c7 N }8 b) j8 I2 z theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
, ~7 _0 F: I% c; |- u/ { theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+- T, ` g) x9 K* |& n/ u
theme(panel.grid =element_blank()) + ## 删去网格线
9 _* I, G; u8 S; L' x8 p0 l- p R theme(axis.text = element_blank()) + ## 删去所有刻度标签
' Y( {3 T5 S% O0 r theme(axis.ticks = element_blank()) + ## 删去所有刻度线
% T7 s# W& z5 k/ s# Here comes the gganimate specific bits
# l$ W# r* y5 a1 A9 a: ?#labs(title = '日期:', x = '省份', y = '累计确诊病例') +" Z2 t' v% i5 S1 y0 } O
#annotate("text",x=0,y=40,label=C,parse=T)+
1 Y! m8 [: v1 j3 Kcoord_flip()+1 Q0 B- U J4 |1 ]- V0 N4 J
transition_manual(frames=as.Date(Date)) +3 P8 h9 p/ R) ~; C" z) b7 m9 Q2 r
#ggdark::dark_theme_bw() + #设置黑色主题
' y+ ^# g5 j7 l- c; ^! Mlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
# i7 K; y, h1 Y$ Oease_aes('linear')/ j1 K/ \0 ~ t# t( n) p
ps
4 i& r* L) E9 m8 H6 b4 L" N结果展示6 ]- {/ [* S; G: Y& s
5 Q1 {1 w2 ~: P% P0 [3 b! V# c6 b 6 k! `+ _# M% J9 ~ i
视频格式转化,加载BGM7 m! }- q5 ^" x" Y+ R5 B0 ]
#df <- animate(ps, renderer = av_renderer('animation.mp4'), 6 \1 m0 |4 B0 a* V
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
* S9 X- V! q+ `9 Z# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")! i) W# o- U2 ~6 X5 ~
全国新冠状肺炎26天增长状况
1 d& w3 M+ ?% s$ S8 ?: h/ A
4 s7 g& Z: z$ ] | H; l0 s9 T! ?pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
2 u* q/ o, [% \1 t# }- |# e geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) + \; e. O- Q9 r
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+1 r9 s' ^, S# W# ]! U
theme(legend.position="none") +1 Q% m, S. p- m, i
theme(panel.background=element_rect(fill='transparent'))+
8 r+ b7 t8 w7 \" G5 V- n theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
5 l+ t. h, c5 q' M9 B theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
6 t- L4 z6 G) T theme(panel.grid =element_blank()) + ## 删去网格线
d7 q/ S5 s3 M theme(axis.text = element_blank()) + ## 删去所有刻度标签7 w4 U) l- a8 m: O) h8 O
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
. y2 g: g; z2 g! P* l' m #scale_x_continuous(limits = c(0,6))+
" @6 e5 e1 G' w) g# Y # Here comes the gganimate specific bits
1 l {. p) ^. F9 C* c: j8 R#labs(title = '日期:', x = '省份', y = '累计确诊病例') +/ b0 ~* p b P3 b2 J
#annotate("text",x=0,y=40,label=C,parse=T)+
: i1 G \5 |. h2 N5 n6 _+ P # coord_flip()+6 M, o3 k- z' d% P
transition_manual(frames=as.Date(Date)) +: o+ ?# ?. k; S
# ggdark::dark_theme_bw() + #设置黑色主题5 n |7 x* Y C. @) B
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
: t& F4 C: \4 M7 X/ J- g7 E+ k; Z; h ease_aes('linear'). b* r$ F2 M- V! Z; k+ H
pc
! V2 ]- X# n" h( Q; I ( K$ G, _" _9 D! m6 [/ R3 b7 v
' X% }& w9 d" H
动态图合并! U, g0 Z, }! L' ? ~
library(magick)
5 r7 {8 v+ R. p: V! z, Xps_gif <- animate(ps,,width = 720, height = 480)/ z \2 ` Q! m( x
pc_gif <- animate(pc, width = 360, height = 480)
7 A+ p7 d( q/ }ps_gifs <- image_read(ps_gif)
* X( ~1 k- {9 I9 qpc_gifs <- image_read(pc_gif)1 `. j \6 @- x2 n6 K, N
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))8 L, b0 e9 A3 F! J4 [0 P
for(i in 2:length(pc_gifs)){
- K P7 z3 Z- |3 q$ u combined <- image_append(c(pc_gifs, ps_gifs))
! [9 D9 z* y& M& p! I [ new_gif <- c(new_gif, combined)
# a) x$ f1 [6 y2 n. D' \" w}
0 k; p( S& z/ L& m. znew_gif8 u9 `0 G, T; }3 \ c9 |4 k+ \5 \
结果展示5 \6 u" B9 ?% J& L# w2 d) F( y
, x& E( E) r4 u1 L5 ]: ~
8 I" y2 ]* f8 S* L: _$ S
6 b! a7 \- ?* f( z. @ ^ |
zan
|