- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40038 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12722
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习1 [9 i3 D+ M* t
所需加载包/ R0 ]9 L9 F3 Y' h2 P! N& N
library(av): L6 H2 N5 D1 Y/ c/ v- y
library(ggplot2)' J* _" E# X/ e- N! \
library(gganimate)) w6 G7 C3 b6 r, {7 q& x1 R
library(tidyverse)- n1 _- b ?4 n$ g/ N
library(lubridate), H. K9 q+ h" M+ x
library(scales)5 D7 B* B, d- d( W( o
library(ggrepel)/ {. R+ ~: |$ d% n D2 x4 t/ T
library(cowplot)
* T4 v1 j9 v7 o e/ ^) y: u- z数据
' z8 r3 e9 p" z9 z: ]# ?5 V$ a, w3 M4 W, B! M& u( q3 W
![]()
* |. M: V+ k4 G% H9 jps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +# Z# ~% X; Q5 b+ E4 I
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
# E* _' n6 g; t/ y0 ] geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
6 e) G3 c4 K4 u! ^ q2 [+ c" X #theme_bw()+
4 b+ A) B" k5 K& \8 a #theme(legend.position="none") +8 u% C, |: [ @1 k
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +. P9 G# t( o7 N! s, @$ g: O5 l& e
theme(panel.background=element_rect(fill='transparent'))+0 H+ ?9 y' h+ v; V7 L
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+
& b7 T, Y# L, V+ T- I% b theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+& u- j, u! M' r: c/ i/ F& {+ N
theme(panel.grid =element_blank()) + ## 删去网格线
/ a6 M+ ?. q( K4 r theme(axis.text = element_blank()) + ## 删去所有刻度标签. O" z- W( b8 Z0 S* t
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
- E4 }( u( K5 e# Here comes the gganimate specific bits4 C' O, j' P: e$ h
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +7 e. T+ h- \5 s/ {
#annotate("text",x=0,y=40,label=C,parse=T)+3 ?% g- m1 J# {6 H2 s
coord_flip()+
6 p0 @, i3 \0 }6 C2 d5 a% Htransition_manual(frames=as.Date(Date)) ++ _) r7 q( O3 P N( J
#ggdark::dark_theme_bw() + #设置黑色主题- `) W, D: g, K: h
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+8 t# Z2 ?0 V; j C9 a8 ]1 M# w" x
ease_aes('linear'), u0 q7 P8 e9 h$ s
ps
7 {. I) v a8 ~( ?$ B结果展示
& K( H3 ^/ ^6 a5 l O
# R, O! }' r6 ?1 f5 |3 e![]()
9 U$ F h$ K" d# H, d5 I视频格式转化,加载BGM
4 K- c: S G2 W+ J#df <- animate(ps, renderer = av_renderer('animation.mp4'), . |. ^: Y0 N1 N- ^4 j3 a& g
# width = 1280, height = 720, res = 100, fps = 10)#视频制作$ E% h' `; W _* b2 u, F0 ]
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
: M+ C/ X d( ?/ H9 S3 |( {全国新冠状肺炎26天增长状况
2 a: \7 j3 ]; V* P X# N# v N7 D6 c6 U: c1 E
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
: T* {& u% G, q: Q/ E geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +0 c R7 x5 @ s0 U5 \: d
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
: s3 Z; b5 x8 v8 P2 h, c' w theme(legend.position="none") +
# J: W" W; _! u0 ~ theme(panel.background=element_rect(fill='transparent'))+0 }6 z( @# }# c. G! d* L
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
c U6 V3 C1 \6 W! D, {0 b7 l theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
+ D: `* v: P; A P, N. G theme(panel.grid =element_blank()) + ## 删去网格线
$ Y" e" j) ^) N: z theme(axis.text = element_blank()) + ## 删去所有刻度标签& Y) Z, M: g7 j6 e! A
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
3 R8 X' Y+ V1 U #scale_x_continuous(limits = c(0,6))+
- [- `1 `2 U4 T+ d# W8 B J) a k # Here comes the gganimate specific bits5 O; l4 _! {! i/ c: R$ a4 e8 b
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +/ {8 u$ x! f0 B! \' H. x
#annotate("text",x=0,y=40,label=C,parse=T)+, J" \% g/ M3 f
# coord_flip()+* B$ B( N9 o# b7 v
transition_manual(frames=as.Date(Date)) +
& C/ j4 L2 s0 T3 _+ T% M% \' { # ggdark::dark_theme_bw() + #设置黑色主题8 D a7 b7 |5 J( l
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+- [ F* p b, F2 c8 E" C4 f& q
ease_aes('linear')2 Q: W$ K h! P0 ]. C4 C
pc5 c0 }2 i% U) P$ O6 T0 `* E
0 N1 r2 `% e0 N# z" A. O; {, M, c
0 L# E3 v1 @4 O* {. M动态图合并: |% O5 G" H# s' G
library(magick); S4 P# F; |, r% O; y
ps_gif <- animate(ps,,width = 720, height = 480)
- F* e0 i- K8 Y0 S, x; g) Bpc_gif <- animate(pc, width = 360, height = 480)
8 w7 q! B3 m/ S7 E0 \0 rps_gifs <- image_read(ps_gif)
# _- d6 A; Z( Zpc_gifs <- image_read(pc_gif)6 S/ \7 ?4 Q/ a, f) B9 K
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
2 Z8 B- a7 T, o& O% i, Bfor(i in 2:length(pc_gifs)){
5 I; r" z# i. V$ { y# N combined <- image_append(c(pc_gifs, ps_gifs))2 I m; _ _1 }2 x8 J( l, y& B
new_gif <- c(new_gif, combined)7 S9 Z$ W+ X/ c4 G
} G+ k3 \- [1 h [4 L
new_gif
. ?3 ^; ?8 D6 r- r9 M结果展示" r6 x8 e* l, F# l7 x7 r. y1 P8 d% i
; q) _" u& p5 r4 U% }1 W7 B
![]()
1 [0 F& A u4 C o0 f" l8 A) R, _2 U
) _: t( z( O, z& P# B; b! H |
zan
|