- 在线时间
- 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学习
7 V# b: T0 H: R2 H) U+ l# c所需加载包
' s# p' h N! F' _3 Plibrary(av)$ @) x) \6 u2 C X
library(ggplot2)
5 Y5 o: E8 i1 V4 k8 T- N% t7 Tlibrary(gganimate)
! H3 g9 P( E# }- G* Z( klibrary(tidyverse)
& c! \/ w- d0 ^2 K$ wlibrary(lubridate), R6 t. ^6 R+ r5 w1 A; x r
library(scales)' w' O: K# ?; U$ `2 W/ K
library(ggrepel)
' A1 b6 E: M3 g3 L! C, A9 qlibrary(cowplot)
8 R7 L4 L$ u( [( ?' ~' r数据
( K, |) D" O$ R7 m8 U \
% b* f7 p0 B- C Z![]()
A" W- W. g) Y Z$ J5 ~ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
}( C9 \) `0 p, t \ geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +0 {0 @- [4 y% `! K
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
$ n! e- p4 G: y7 v2 n+ m+ b/ v% ^+ @ #theme_bw()+
/ ?6 T+ G/ @# `6 J2 b! A) c #theme(legend.position="none") +' A" D; j( t7 t, m
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") ++ E' ]& G2 i8 z, T! x" {; A
theme(panel.background=element_rect(fill='transparent'))+' _+ l1 c2 l' L/ u1 m8 A; l/ W
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+6 S" j W/ `, ~3 ?/ h5 d! D
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))++ y- M, j) ~- y }& {$ Z
theme(panel.grid =element_blank()) + ## 删去网格线+ u2 z, D* L" p2 x6 S
theme(axis.text = element_blank()) + ## 删去所有刻度标签- d, ?! Q* s5 T) }4 o2 z* ~
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
z& j/ u' [4 N1 N5 ~- _8 ` z# Here comes the gganimate specific bits
3 x; c. I& H( g Y#labs(title = '日期:', x = '省份', y = '累计确诊病例') +" D L! A0 |) T! B1 M$ G- j' ?
#annotate("text",x=0,y=40,label=C,parse=T)+ B3 C% J0 f, o9 t' v+ \4 I0 U" O
coord_flip()+0 A0 h K# [9 r0 R5 ]7 V) m# a. z
transition_manual(frames=as.Date(Date)) +
Q2 O+ j ]+ s* ~. E' v. t) w#ggdark::dark_theme_bw() + #设置黑色主题
* E+ q; c; k e, Rlabs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
8 n3 I q" L, n: ]. tease_aes('linear')
. S6 K$ O5 A" |ps
; A" p, k0 o% @! ]% r, \结果展示) N/ p7 X! [/ l' W
8 z: M2 n9 |0 t W m7 C* H( Q' ?![]()
8 ~ t& H: m" B1 ?& z1 K5 v视频格式转化,加载BGM
3 W0 [8 i: B6 b: J/ ~#df <- animate(ps, renderer = av_renderer('animation.mp4'), ' I% |( A' h) C Y& c2 V4 R
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
' M6 {$ }) w1 B5 u# ]& X# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
8 w `0 ^4 ^! Z1 b; h全国新冠状肺炎26天增长状况, d" D! V" k9 \+ |/ f6 \
1 X) I$ [) P) e' r
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+
# z% J, S5 I) u" n+ A" Q: A& k geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +: @% i1 @, F+ d: g1 R" G
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
& ?! ]% S$ p' r theme(legend.position="none") +
" y. S+ K) f* u( r( a theme(panel.background=element_rect(fill='transparent'))+& E* Y, D$ A2 w1 N, M
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+7 j- \: n& @% a9 f
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+ O7 M1 I0 p% }! ?
theme(panel.grid =element_blank()) + ## 删去网格线' D0 n' A6 y: B. N4 i
theme(axis.text = element_blank()) + ## 删去所有刻度标签
% Z7 K; y8 B8 a* H+ f' G$ ^ theme(axis.ticks = element_blank()) + ## 删去所有刻度线; K! R% ~2 ^6 n
#scale_x_continuous(limits = c(0,6))+
8 y0 g, y$ C0 P" ? # Here comes the gganimate specific bits" D0 c5 t, `" d! O8 B H3 g# h: Y
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
E! X! _+ |* m#annotate("text",x=0,y=40,label=C,parse=T)+
/ g2 _- o+ t7 _ # coord_flip()+
$ |; d* E H2 c0 B7 G transition_manual(frames=as.Date(Date)) +
) T7 K3 d. [: ^2 y b+ G/ ] # ggdark::dark_theme_bw() + #设置黑色主题2 P; w* E0 g8 t5 x
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+- ?3 h7 \; C' r2 W
ease_aes('linear'); F# I9 n; U0 I9 f: Y- W, \
pc
9 V% e0 K" N) P+ r7 d![]()
4 B) n' A i5 X+ `6 d& e) [2 i2 k% p! p- C( {" I* `0 Y0 y
动态图合并0 M4 r& u/ H; w0 G
library(magick)
- T% E" p# H: H6 a$ @0 e- z2 w0 Dps_gif <- animate(ps,,width = 720, height = 480)
# j5 A9 S+ ?( [! J4 b/ E6 F Tpc_gif <- animate(pc, width = 360, height = 480), P8 R& g* F3 @6 A
ps_gifs <- image_read(ps_gif)
0 B' V- | S4 J6 J( ^, _8 h- }pc_gifs <- image_read(pc_gif)
6 Y( a. a& H! K; Nnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))) i- X2 t1 E8 z5 k- L0 P& U
for(i in 2:length(pc_gifs)){: j$ l% K. Y$ F4 U2 j: C
combined <- image_append(c(pc_gifs, ps_gifs))
5 z8 R! D* e, v9 J4 J$ f new_gif <- c(new_gif, combined)
7 l: x4 \; b5 E" A& e}9 q- _6 o/ H i$ e' v7 M. h
new_gif/ j& s5 C; H/ [, g( O$ w' N
结果展示
; z* E3 E {6 k/ R$ p- ]' R
" r; k% x, I2 W& J4 j7 u![]()
$ c: a Z' l0 { f* ^0 V
7 L/ v0 T; y3 t" p. Y |
zan
|