- 在线时间
- 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学习
) k. N8 v+ N7 |% T1 ^/ f* h. D所需加载包5 i5 X* l, V2 ^) _
library(av)2 C* o9 k+ H( v+ K
library(ggplot2)
8 x1 A {( ^7 R$ l& I" M5 Olibrary(gganimate)
8 Z4 B/ B6 ~& |library(tidyverse)9 C1 X' e, S# }+ f6 d* \8 B7 _) |
library(lubridate)
3 r4 o( A- b' e( t7 u% zlibrary(scales), j6 @) v5 @0 c' I, z; n
library(ggrepel)
. @. U/ ]3 Z- M* P) c2 rlibrary(cowplot)
0 B" u0 O/ o: J H6 t数据
2 d! b- ]) G t
, x) k8 u& J+ w0 U5 v0 z" j9 u 0 U, @3 W3 v" L
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
5 y# P. U; [9 U @1 J geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +/ V( F ^2 J: N7 x; z+ z
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
! G5 P8 z, C; O4 I) T #theme_bw()+6 ~) F# z8 i y# m8 s! n( }5 {
#theme(legend.position="none") + c* W: {& I, G0 c0 Y! y- M
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +5 V' s# h& V2 W
theme(panel.background=element_rect(fill='transparent'))+
% ]+ J6 @ R6 ~1 {( {2 P0 a- r6 n theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+: f# ?. r J0 d0 m3 y4 D. U& y
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+. o& _& _! s B, F8 k
theme(panel.grid =element_blank()) + ## 删去网格线
. L3 S$ t. r* {2 _ M theme(axis.text = element_blank()) + ## 删去所有刻度标签
, }6 B: O# z$ Z2 _0 R) f theme(axis.ticks = element_blank()) + ## 删去所有刻度线
5 E' @# L! D8 q; v, C# Here comes the gganimate specific bits2 t* t: B$ o; d- H$ f9 Q* }
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +0 h) O6 o7 {; O
#annotate("text",x=0,y=40,label=C,parse=T)+5 W% Z# b8 ?' q9 O1 q6 C% v6 v
coord_flip()+
, n( e3 [* h2 Ntransition_manual(frames=as.Date(Date)) +
. h5 d _( F9 i' u5 n1 n. x- S% X#ggdark::dark_theme_bw() + #设置黑色主题$ ^, d. ]% _8 U* W R N7 A
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
& `9 ~6 `" c3 Q6 m8 a. w/ Rease_aes('linear')2 J/ ?* k' |9 E0 r
ps
* N' e$ N3 w$ m) l, _3 U结果展示8 e- |4 F3 w$ m$ Z9 q
& |0 S* S+ O. U: X! a
![]()
' C1 a* r/ P$ c- D5 `7 u2 E6 \. @9 t5 B视频格式转化,加载BGM
/ h% K0 {0 H4 M& X' Y#df <- animate(ps, renderer = av_renderer('animation.mp4'), & q6 }4 i+ e+ `. e
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
+ F- ^) b/ ]4 B/ H, _" v& s: j; T# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")* N6 T3 u/ Y9 `6 M
全国新冠状肺炎26天增长状况1 V- h2 F) p# ~7 M
5 p4 Q) E: Q. k0 d! a
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+' O/ W2 y& Z7 e) I9 Q
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +4 a, Q" v4 o/ p& K( E
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+& v3 B7 H" X" ~. Y5 n, h
theme(legend.position="none") +
4 Q" n* `' J* `5 a+ c7 J) s _ theme(panel.background=element_rect(fill='transparent'))+
; {: U0 h; J+ [ h2 i+ K/ |! `+ g theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
* A: N5 @6 B" t" ^ theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+4 `2 i2 g1 Y* K% o0 i+ _; @
theme(panel.grid =element_blank()) + ## 删去网格线
$ K9 K( F* t& E, Q# y) ~ theme(axis.text = element_blank()) + ## 删去所有刻度标签- e. x- G, Z. U% b
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
! S7 L, B, ]0 d* i0 A #scale_x_continuous(limits = c(0,6))+3 O7 b) ]8 x9 g5 M( l( @
# Here comes the gganimate specific bits
8 l: v2 q, g* `/ g#labs(title = '日期:', x = '省份', y = '累计确诊病例') +* Q, F6 ^3 S2 H1 b+ h; w. k* ]$ ] Q: f
#annotate("text",x=0,y=40,label=C,parse=T)+: G7 b) v7 e7 S5 W8 M
# coord_flip()+, V! ]5 X. X' `9 ~* q* \. I8 ^2 s
transition_manual(frames=as.Date(Date)) +; E) r' V. e: I) w' O' ]/ a2 p d
# ggdark::dark_theme_bw() + #设置黑色主题
5 N' F0 v4 }2 a( i3 X labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
" h# k# S3 @4 G" b- }5 c ease_aes('linear')
9 l5 P, r# F& t! h& G9 gpc
. H' \3 y0 @$ [1 ?2 j& i9 C + W* b# {1 x& M+ E6 n$ l
6 [, l. N( |5 r" Y5 x! S# d. ?( T
动态图合并
( g. T+ d' N3 p2 J$ D8 X2 |6 q0 Slibrary(magick)$ q* [ p6 s! i& X( {
ps_gif <- animate(ps,,width = 720, height = 480)3 K& s* r- H0 [; q
pc_gif <- animate(pc, width = 360, height = 480)" M. J0 a0 e- T# [
ps_gifs <- image_read(ps_gif); K" r9 s# g/ G- i6 P
pc_gifs <- image_read(pc_gif)% ^) }2 F/ N* t$ p1 y: S: @4 N: j
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))4 p1 \( S/ s! S* [
for(i in 2:length(pc_gifs)){2 C9 G, d2 y$ `% T8 R ~1 ^
combined <- image_append(c(pc_gifs, ps_gifs)) U$ ~$ ~: H. ?+ c4 I% C
new_gif <- c(new_gif, combined), X; ^0 e1 E4 U: V, v
}% Y* @4 H( J6 ^4 {6 d# H
new_gif
- o8 e n: M8 Q; D结果展示
8 s0 q0 m! Y) B6 b* p$ K& a# g( p' ?: J
% \4 x' g" K( ]4 v
/ R7 g& i! _$ a2 r5 i( X
|
zan
|