- 在线时间
- 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学习( w) o' T, a) Y8 j/ l U
所需加载包
3 v: f l7 k3 i Y5 b/ e- l* zlibrary(av)
; ?* ~) W: f& }+ p5 alibrary(ggplot2)% f4 q) d! ^- d+ H$ F' y
library(gganimate)
2 L1 y# g5 H' b5 \* Plibrary(tidyverse)$ C# F4 |. ~% S# U: N5 [
library(lubridate)+ }# z8 q* L q$ S; P. \. q$ M
library(scales)5 i, k! _5 b, F" j% t5 c
library(ggrepel)
) O" x9 L* [7 k/ x( u- xlibrary(cowplot)
- {! D7 y. Z7 N% i x数据6 `% E8 w% d/ o/ R) p/ z, U
3 W; J6 {8 u7 b* W, s # W# k0 Q5 h5 k& V/ t7 y. ?1 s
ps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +. O5 E3 Q/ w& g' \
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) + _3 w; e2 s9 H. G( }% n' l
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+
. H$ _) x: q! D+ c' h) v3 ~( M #theme_bw()++ w+ b* v: [6 a4 {9 P4 w* @
#theme(legend.position="none") +; B$ o& b% g5 v, D) H; X4 f5 O
theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
) B" i: I0 b) j3 F! c4 Z; V theme(panel.background=element_rect(fill='transparent'))+
* o- a: _( w# U theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+& L+ j4 K8 F& @) v
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+
9 G- D9 }: Q! H7 M" h* \# ]0 l& { theme(panel.grid =element_blank()) + ## 删去网格线
! `( O9 o# j+ P+ P' L* b theme(axis.text = element_blank()) + ## 删去所有刻度标签
/ @2 X" [8 { @. a) z theme(axis.ticks = element_blank()) + ## 删去所有刻度线
; Q0 i5 B' q) x+ l' j, [1 B; M% Q3 Y( f# Here comes the gganimate specific bits( p* `3 r' u# T: g# @( g3 W
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
; o7 C, L% A2 o# Y+ u# r) p/ P1 _) R#annotate("text",x=0,y=40,label=C,parse=T)+- S ~7 d8 v3 }7 g/ j7 Z; J# K7 x
coord_flip()+. S0 M7 a4 [/ e' \' {. D
transition_manual(frames=as.Date(Date)) +1 j- M6 Q+ x) j% v, t# Y7 I
#ggdark::dark_theme_bw() + #设置黑色主题- C5 O5 ^& D. f) v) d: E+ t+ t
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+
; x4 j' T& P% Dease_aes('linear')
( S% l" q- S) e1 E( pps/ ?2 G/ i0 |' W3 H
结果展示% U& F0 \# q5 ?+ h1 i# l8 B9 _7 O2 d
8 i0 m" P4 K4 W& B: s
5 f: H) j3 ]! N5 w5 E4 x7 n$ T7 j
视频格式转化,加载BGM
9 J4 P2 p+ a8 n# h3 N+ C; S#df <- animate(ps, renderer = av_renderer('animation.mp4'), X% D) t; h+ C) r3 \
# width = 1280, height = 720, res = 100, fps = 10)#视频制作8 |3 n. ]7 p) d x
# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
8 v/ {0 [7 p5 ~/ A0 J" ]1 Q全国新冠状肺炎26天增长状况
& W) \& }/ n2 n7 X3 }
' I# v/ m9 W' F. i3 h. \ Upc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+) ]1 ~" m& R. @9 j) j
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +. `( d- s, A5 C8 t* A1 T* f3 Q
geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+
$ d4 S4 d# f& D# \ theme(legend.position="none") +
1 T4 `2 z- ]( E* o0 J6 ?+ H+ _ theme(panel.background=element_rect(fill='transparent'))+
3 q3 l; `+ u+ u5 K7 l( J theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+
0 j8 R$ n$ p$ t8 C8 g theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+2 N: h& ?" h' h, H
theme(panel.grid =element_blank()) + ## 删去网格线
" ?- T) t% A2 m theme(axis.text = element_blank()) + ## 删去所有刻度标签
9 U5 X# q2 n! n' l theme(axis.ticks = element_blank()) + ## 删去所有刻度线
: F& a$ @2 C% r0 Y- Q0 o" Z #scale_x_continuous(limits = c(0,6))+3 y- |9 P% J# q- [2 c* W, u) H
# Here comes the gganimate specific bits: g* l6 s+ A7 U( _+ o* O
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
- P* U8 Y y3 B) o5 i; H6 Y#annotate("text",x=0,y=40,label=C,parse=T)+
$ E2 {! L4 ^9 U. U # coord_flip()+
; [4 N4 f% J) D; F transition_manual(frames=as.Date(Date)) +6 p! O# ]& j9 I6 u
# ggdark::dark_theme_bw() + #设置黑色主题
: Q4 j' ~2 y( }8 O2 i labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
6 m4 q1 _6 b- P6 p1 T- k" | ease_aes('linear')
7 _% l3 |. k8 H. I0 L' ~- |pc
$ o8 S* J* s- S8 Y$ V: D![]()
5 `, S' z9 ?2 B5 ]9 l* @8 `, E& M" t, I, H0 _8 K5 y
动态图合并
! m j$ o8 ~& z6 d! ^( Tlibrary(magick)& S2 G" G0 r. w" X" [; ?
ps_gif <- animate(ps,,width = 720, height = 480) W! O9 ?5 f6 T5 o8 K
pc_gif <- animate(pc, width = 360, height = 480)
; }' Q- W! @$ U7 p6 J% }6 l. i* {ps_gifs <- image_read(ps_gif)' u1 _7 I, g, ]1 X% U0 C
pc_gifs <- image_read(pc_gif)8 i& f- ~( K4 }# a# ^; L7 S
new_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))
) S9 o; p4 e- R, b+ Cfor(i in 2:length(pc_gifs)){
! Q2 j6 K# E! h& d9 u4 E combined <- image_append(c(pc_gifs, ps_gifs))% @$ P7 X% H4 T, n
new_gif <- c(new_gif, combined)
9 z# h, R! Y( `3 F0 ` w}
4 K* P, {2 y, g" znew_gif1 \2 p. y* ~+ r+ J! p8 n* h
结果展示$ x, A+ U& N0 A0 P4 r/ w
/ x1 F2 {9 n8 M; ] - f$ H/ e# M9 o4 }. G# g( H( E2 X
" ^8 b0 t+ d0 L3 x: G4 {
|
zan
|