- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 39388 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12512
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1388
- 主题
- 1158
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
|
R语言gganimate学习* e* N3 u9 k( {
所需加载包2 t1 G2 d, @( D c
library(av)
% }" ~+ Z' y* q# w$ flibrary(ggplot2)3 ` n4 o D! L
library(gganimate)
8 P3 A. l6 b5 l; H5 e9 Blibrary(tidyverse)
; ]4 F" f/ D' I- x! J$ ~library(lubridate)
+ H0 c: r, {- \; X. Plibrary(scales)
5 _& v; v! q7 t6 p4 u! d; k$ E, \! Dlibrary(ggrepel)& @/ A0 w9 g' W4 l' |% ~
library(cowplot), w1 D& b* Q; f R7 C$ U# W" O6 |
数据3 a5 ?% I$ B5 j1 |4 e( H
; _( L. O# T+ `
5 O! @9 {6 ^, r% `& l2 t, rps = ggplot(mydatan, aes(x=reorder(省份, 累计确诊),y=累计确诊, fill=省份,frame=Date)) +
& h4 g( W" U: O9 w3 {# L geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +9 j" p& u3 s6 p$ C
geom_text(aes(label=paste0(累计确诊)),col="black",hjust=-0.2)+& n0 Q& v3 |$ N0 { l' l8 S: @. w9 X
#theme_bw()++ {* _) s N5 V+ w$ A4 R0 }6 [
#theme(legend.position="none") +
5 Q" D1 d" I* ]7 H7 w0 u theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +
& `0 _8 P* H# `7 D theme(panel.background=element_rect(fill='transparent'))+
( M( d: q" Y3 H3 t. S' ]/ {9 ?3 i theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+5 Y( q; C3 u! w+ v5 h
theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+- S7 s3 x9 A( e: ^; d8 V2 O" ?
theme(panel.grid =element_blank()) + ## 删去网格线% ~- Z+ N9 D; n
theme(axis.text = element_blank()) + ## 删去所有刻度标签
4 c& Q" {1 T- f) N8 B; [8 \" F# x9 l$ x theme(axis.ticks = element_blank()) + ## 删去所有刻度线1 O& y# V% ^0 q" Q
# Here comes the gganimate specific bits
- F4 N, l6 g! x( u+ O) ^9 {/ T' I9 @#labs(title = '日期:', x = '省份', y = '累计确诊病例') +
) K& Z5 r, @/ X#annotate("text",x=0,y=40,label=C,parse=T)+
# [, f, ^+ {9 h0 H' scoord_flip()+
D. G! t7 Y: H# N; Ltransition_manual(frames=as.Date(Date)) +
( }2 k, `9 [+ T$ @% J5 Z5 g0 Q#ggdark::dark_theme_bw() + #设置黑色主题, j2 g' D6 l" T8 V: e: [$ f* Z
labs(title = paste('日期:', '{current_frame}'),x = '', y ='各省累计确诊病例增长(除湖北省外)')+( w4 Q& D) N" ]8 i d+ S& H
ease_aes('linear')3 r Z4 ]; i3 J5 b2 p
ps
1 w. O: \4 T+ A% d" U3 ]! D6 J结果展示
- \. w+ S1 D! M. W# l4 a, ^1 n( g5 `* b3 ?. S4 ?
/ X' M9 S# G4 [6 ], k
视频格式转化,加载BGM* R0 l& u$ [& V. K
#df <- animate(ps, renderer = av_renderer('animation.mp4'), 6 h- O7 e! |$ z5 f& z) A# F* \! _
# width = 1280, height = 720, res = 100, fps = 10)#视频制作
8 W8 U _# w R6 s/ Q# av_encode_video(df, 'output.mp4', framerate = 2,audio ="N.mp3")
& I( t3 I8 k# }全国新冠状肺炎26天增长状况
! j, V3 `% D$ o6 o& f9 D1 s$ e$ x- N/ L& c0 ]4 g8 ]* S- f- H
pc<-ggplot(data=CNdata_s,aes(x=variable,y=value,fill=variable,frame=Date))+7 n, Z6 @' `1 ~6 Q! r
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE,width=0.7) +
# Q$ P% n" p- I& M0 z! D geom_text(aes(label=paste0(value)),col="black",hjust=-0.2)+0 ]1 K1 R. h( b
theme(legend.position="none") +
`1 u S! p6 V: ~- P; F* ?: t theme(panel.background=element_rect(fill='transparent'))+: w+ M3 D& W. B3 k% Q4 {# M
theme(axis.text.x=element_text(angle=0,colour="black",size=15,hjust=1))+9 ?& b5 C- v2 h! `
theme(axis.text.y=element_text(angle=0,colour="white",size=2,hjust=1))+
7 m) d5 q, U9 I0 f% V& z0 u theme(panel.grid =element_blank()) + ## 删去网格线. P6 B! q- T! y. }0 L+ P( q
theme(axis.text = element_blank()) + ## 删去所有刻度标签. ]/ }$ K- ]0 B8 ~4 g+ V, M
theme(axis.ticks = element_blank()) + ## 删去所有刻度线% t* S! e- B9 R; I
#scale_x_continuous(limits = c(0,6))+1 x1 S; o$ z2 ~# W* B& V4 \
# Here comes the gganimate specific bits9 Y0 z# i3 T& m# U) |
#labs(title = '日期:', x = '省份', y = '累计确诊病例') +$ S7 i0 R: n1 A, l/ ~' p
#annotate("text",x=0,y=40,label=C,parse=T)+9 K+ D* @. ~, _- T4 D" r: V& ], D
# coord_flip()+# V- j- |2 c9 [% r. r7 `4 t
transition_manual(frames=as.Date(Date)) +5 `. x- c4 N: F+ ]! `
# ggdark::dark_theme_bw() + #设置黑色主题
8 Z) r! |, f8 j5 s8 h8 I$ m labs(title = paste('日期:', '{current_frame}'),x = '全国新冠状肺炎增长', y ='')+
* m+ L3 Z2 ]9 T* w. z, [, C ease_aes('linear')
- l6 r7 o2 I8 G' |' |4 \) W/ ppc$ `; ?3 u8 _3 j2 t9 z
8 H2 o4 t( p3 |! s5 e; D6 @* ]; R. ^7 J8 K7 U6 s; ?
动态图合并9 V5 n" d9 ~$ {1 p2 z1 J
library(magick)4 a' ?; b1 K( i/ d/ K+ i8 B
ps_gif <- animate(ps,,width = 720, height = 480)
9 n+ Q4 i5 I" }! R; C5 jpc_gif <- animate(pc, width = 360, height = 480)- a- w1 P* Q, J3 K
ps_gifs <- image_read(ps_gif)
2 m" K" O, t/ ^, a" |pc_gifs <- image_read(pc_gif)
, H& \ X. y F8 Pnew_gif <- image_append(c(pc_gifs[1], ps_gifs[1]))6 L. _7 q1 L, {4 [) h* C
for(i in 2:length(pc_gifs)){1 m4 t: H) p4 g# i4 S
combined <- image_append(c(pc_gifs, ps_gifs))8 ]" c C) l$ a) @
new_gif <- c(new_gif, combined)
7 C) A, @) g) \6 W. _}
% n4 U( F% Q6 _# pnew_gif3 D. J% O" D1 } o
结果展示& o* H) {# s4 Y& K
+ P: N& R1 P: F7 Y, Q3 |. a s B0 }; H
5 R, t5 @+ B: @/ o
$ b6 K1 K/ y5 b! U0 Y
|
zan
|