- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40220 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12777
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
R语言gganimate学习6 f( d/ C7 q3 C- Z6 M6 g
所需加载包
9 T# H! I3 B$ [% K7 ilibrary(av)* m' [5 I, Q: ~. z& v
library(ggplot2) O* J. o1 n6 e* q" Z9 Q
library(gganimate)7 f1 U& x6 U! {4 b1 c4 Y" ~
library(tidyverse)4 X& u& d* V+ n. E! X
library(lubridate)8 w x5 e$ ^9 b- |* E0 V7 x
library(scales)
( _+ d2 `- ]; B! k9 H' Elibrary(ggrepel)
6 {: J4 H# _' z! }$ s1 y3 N. c0 Glibrary(cowplot)
" |( J1 X; K G数据
; R) [. U& N: T& t% Y% j8 m+ V/ b! N
5 |$ u! H* A* B d3 n+ a: ?
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +, U R R7 y, z4 }1 U A4 t1 R- o
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
, y, N' _; h) A4 Q geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+2 |9 u1 A( N6 k5 v3 c$ w$ d
#theme_bw()+
' t M# A7 ^, q: v _) V& b# y5 d #theme(legend.position="none") +
/ G0 {* x X$ U r i2 \ theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") ++ J' ~! D5 w# L m8 n& V6 F
theme(panel.background=element_rect(fill='transparent'))+* v2 y) T' n/ ?3 B6 |, h, K
theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+7 a- e% J C5 O( \& N# v
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
9 S* J; g; o4 I" S+ z4 u9 D theme(panel.grid =element_blank()) + ## 删去网格线4 e5 s3 n6 B* D
theme(axis.text = element_blank()) + ## 删去所有刻度标签1 B% [& [, H' ?0 `- c( `% ~' p9 p
theme(axis.ticks = element_blank()) + ## 删去所有刻度线
. O# K+ ]! u( a' A% p, R# Here comes the gganimate specific bits
5 B. w4 e# B( t q0 @- a#labs(title = '日期:', x = '省份', y = '累计确诊病例') +1 n7 G' `/ c, k d) _5 ]% v
#annotate("text",x=0,y=40,label=C,parse=T)+0 z) }4 X% W3 T: _
coord_flip()+2 g- V( P% b; j
transition_manual(frames=as.Date(Date)) +/ n; A7 `8 J' Z2 n
#ggdark::dark_theme_bw() + #设置黑色主题1 T8 }* z9 t$ b: @" P6 @) N
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+ R' v( g2 j' J T* X3 {
ease_aes('linear')
! A# K2 V }4 M9 r5 T) j3 hps
6 M6 T9 c! A7 m' n( S% j结果展示1 y+ n# Z0 r! i5 }1 \) d
/ m; ?; f6 e: ]; D: W, V % x4 z [7 d# m1 Y0 J; U
视频格式转化,加载BGM* M, o. O# V" X9 a
#df <- animate(ps, renderer = av_renderer('animation.mp4'), 1 V" T, _ N% ?$ r( m5 W
# width = 1280, height = 720, res = 100, fps = 10)#视频制作% v3 M7 u6 m. H+ ]+ _, n6 \
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
# [# k4 q$ o5 J- ^全国新冠状肺炎26天增长状况
# G4 h$ i& c. P! p7 q# n* O& o$ Z& b3 a$ ?% e
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+$ t: y! q- A, m
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
A" @2 _8 ^! V8 b2 C geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+, e7 q3 [) f8 o F' J+ P2 j
theme(legend.position="none") +3 z1 M3 |+ ^3 R U9 [. ~
theme(panel.background=element_rect(fill='transparent'))+6 d$ @3 x8 u5 D3 R
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+* `& c) T E d3 [/ m# g
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
$ e7 v) q2 _ Y! x! a' a4 | theme(panel.grid =element_blank()) + ## 删去网格线
1 C: \9 |* @3 x8 ?7 C theme(axis.text = element_blank()) + ## 删去所有刻度标签9 V) R d! x: `8 j7 k( x
theme(axis.ticks = element_blank()) + ## 删去所有刻度线) y8 W: V/ f/ _- T
#scale_x_continuous(limits = c(0,6))+' b! X3 o5 B) @6 u2 u- o4 M
# Here comes the gganimate specific bits
5 b9 P0 f S/ e#labs(title = '日期:', x = '省份', y = '累计确诊病例') +3 Q2 a7 |5 x3 U$ h b e' |9 `
#annotate("text",x=0,y=40,label=C,parse=T)+
! k1 E( _6 U2 }& d6 t # coord_flip()+
1 I+ e9 ?5 i! I; D: C transition_manual(frames=as.Date(Date)) +
2 p3 i! k1 t$ a# b6 C# u # ggdark::dark_theme_bw() + #设置黑色主题7 ~- n2 K" o: X e; ~
labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
; x2 @# M/ E! u: ?+ G5 o ease_aes('linear')
* ?; [8 I" H; p( ?$ C1 jpc+ G- E! Y# Y0 L. n
![]()
8 R6 N( G# h1 B1 F
1 U6 ?' c! T D+ X' L动态图合并. u$ W% ?7 l( H# c6 n
library(magick)
5 M9 t# C. n3 q& X$ Tps_gif <- animate(ps,,width = 720, height = 480)' P4 F$ U) g* |4 O: r3 u* ^; o
pc_gif <- animate(pc, width = 360, height = 480)2 ], k; j8 h7 I( r
ps_gifs <- image_read(ps_gif)- L N9 q; Q9 z/ b; y/ ?8 X
pc_gifs <- image_read(pc_gif)( }8 n8 e1 S: L; [5 v2 s& p0 Z
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))/ L0 ^4 A# l8 V- r3 \& I
for(i in 2:length(pc_gifs)){7 h$ \8 h( O6 P( T4 f2 g/ {9 ?
combined <- image_append(c(pc_gifs, ps_gifs))
6 I$ {( ?+ r; |9 o new_gif <- c(new_gif, combined)
; x1 D; p+ w/ g7 L}
/ i' T+ ]9 N- R+ w0 i3 x+ Ynew_gif) Y2 `0 O1 }% c6 J. Q; n
结果展示
( g% t5 T# _" a9 c! M
5 O n0 m) O O1 F( X9 T![]()
7 J' a# J/ B1 B8 X9 G' s
+ [8 B! s% v) d9 k/ i% N |
zan
|