[color=rgba(0, 0, 0, 0.749019607843137)]#加载本次可视化所需包[color=rgba(0, 0, 0, 0.749019607843137)]library(readr) [color=rgba(0, 0, 0, 0.749019607843137)]library(sp) #地图可视化[color=rgba(0, 0, 0, 0.749019607843137)]library(maps) #地图可视化[color=rgba(0, 0, 0, 0.749019607843137)]library(forcats)[color=rgba(0, 0, 0, 0.749019607843137)]library(dplyr)[color=rgba(0, 0, 0, 0.749019607843137)]library(ggplot2)[color=rgba(0, 0, 0, 0.749019607843137)]library(reshape2) [color=rgba(0, 0, 0, 0.749019607843137)]library(ggthemes) #ggplot绘图样式包[color=rgba(0, 0, 0, 0.749019607843137)]library(tidyr)[color=rgba(0, 0, 0, 0.749019607843137)]library(gganimate) #动态图[color=rgba(0, 0, 0, 0.749019607843137)]
8 \ V! |) b* q5 J/ v[color=rgba(0, 0, 0, 0.749019607843137)]#一、国家名词整理[color=rgba(0, 0, 0, 0.749019607843137)]data<-read_csv('confirmed.csv')[color=rgba(0, 0, 0, 0.749019607843137)]data[data$`Country/Region`=='US',]$`Country/Region`='United States'[color=rgba(0, 0, 0, 0.749019607843137)]data[data$`Country/Region`=='Korea, South',]$`Country/Region`='Korea'[color=rgba(0, 0, 0, 0.749019607843137)]! w2 g9 Q/ J; t) k Z8 l
[color=rgba(0, 0, 0, 0.749019607843137)]information_data<-data[,1:4] #取出国家信息相关数据[color=rgba(0, 0, 0, 0.749019607843137)]inspect_data<-data[,-c(1:4)] #取出确诊人数相关数据[color=rgba(0, 0, 0, 0.749019607843137)]* h: a' G# L9 v& Q, E. e9 o+ R
[color=rgba(0, 0, 0, 0.749019607843137)]#二、日期转换[color=rgba(0, 0, 0, 0.749019607843137)]datetime<-colnames(inspect_data)[color=rgba(0, 0, 0, 0.749019607843137)]pastetime<-function(x){[color=rgba(0, 0, 0, 0.749019607843137)] date<-paste0(x,'20')[color=rgba(0, 0, 0, 0.749019607843137)] return(date)[color=rgba(0, 0, 0, 0.749019607843137)]}[color=rgba(0, 0, 0, 0.749019607843137)]datetime1<-as.Date(sapply(datetime,pastetime),format='%m/%d/%Y')[color=rgba(0, 0, 0, 0.749019607843137)]colnames(inspect_data)<-datetime1[color=rgba(0, 0, 0, 0.749019607843137)]
; w& i4 H# z! o# b[color=rgba(0, 0, 0, 0.749019607843137)]#合并数据,data为累计确诊人数数据(预处理后)[color=rgba(0, 0, 0, 0.749019607843137)]data<-cbind(information_data,inspect_data)[color=rgba(0, 0, 0, 0.749019607843137)]二、新增确诊病例变化趋势#由累计确诊病例计算新增确诊病例
3 Z5 e3 [) u; d- {inspect_lag_data<-cbind(0,inspect_data[,1
ncol(inspect_data)-1)])
) o2 w" h7 ^8 ^increase_data<-inspect_data-inspect_lag_data
8 V" t% r5 f0 ]/ b- h. Q" d* A/ W% v9 t. l" B9 F% h4 p! {( d
#合并数据,new_data为新增确诊人数数据
% T: K' Y+ F* s. g. Anew_data<-cbind(information_data,increase_data)
- j+ ]9 P: E( W+ v* j7 |: _) L; I
; y' I3 Z6 I+ _( s0 K3 N+ a L1. 中国新增确诊病例变化趋势
, J9 l8 P( ~* a) }8 ]0 K% O#合并所有省份新增确诊人数7 [) \* z+ a) B
china<-new_data[new_data$`Country/Region`=='China',]
+ {) Z" n# u( G. T& Rchina_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
# v; W1 Q" {- `* Q6 Q& D( y: R" Ycolnames(china_increase)<-'increase_patient'
5 e+ y1 ^' A3 l Gchina_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")
! p E4 @. @6 |
, q4 b5 ^0 o J; Pggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
: t# a) P1 ^3 \8 J" F scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)
, O: N2 M0 R% j& F/ P1 P labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+
' F4 }: p0 I( h6 i6 J4 i6 O( | theme_economist()+ #使用经济学人绘图样(式ggthemes包)# S5 l$ g5 z% ?+ z
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),* v7 z! u* r) N) M
axis.title.x = element_blank(),; Y0 l/ N4 U/ H8 {& L
axis.title.y = element_text(size=15),
: {( P, \/ z4 P- r, }6 ` axis.text.x = element_text(angle = 90,size=15),+ D2 G _: N$ ?1 w( c8 O
axis.text.y = element_text(size=15),
9 \5 u! T4 N* M" N: R3 J! Y3 s legend.title=element_blank(),3 e% a- Q8 Y0 q( K3 v# `
legend.text=element_text(size=15))+ D2 E3 O6 t7 ?
" a, V# B! i# a* l 
' l @! b# q8 }; K# I2. 美国新增病例变化趋势3 j, m) j: _3 U
us<-new_data[new_data$`Country/Region`=='United States',]
: `# y4 _! _9 {6 |" Q+ Qus_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
3 g% o6 K% P5 L u) Aus_increase$date<-as.Date(us_increase$date)
, q0 L( c- I* R ^9 Y( ~: X0 m' V4 {ggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
+ q6 ]' d0 _- M. [! y, t, e) Y9 ] scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天8 X$ x1 e4 N7 P V0 G, O0 p/ e% v
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+2 S( a5 m7 `6 J$ W/ v4 }
theme_economist()+ #使用经济学人绘图样(式ggthemes包)& `8 @, h3 `0 m9 K& {. o& Z9 w7 L
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
8 q0 M6 s3 L/ t axis.title.x = element_blank(),
7 l K* x) I/ s" e axis.title.y = element_text(size=15),3 a$ g9 S0 W; j7 N; a
axis.text.x = element_text(angle = 90,size=15),4 K [ L7 ?. R! Y2 I
axis.text.y = element_text(size=15),3 M7 y% t! k& p6 {7 h
legend.title=element_blank(),- ~9 B f2 v9 f+ I! @) d" }2 X; h- q
legend.text=element_text(size=15))7 K$ S, [ X$ p, z4 F
& _3 C3 @& B w4 _9 n

9 S4 U0 F, k( _! \2 V3. 全球新增病例变化趋势
3 B5 O( P6 Q9 e( h6 K5 ~total_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))* b5 T+ S4 F0 B
colnames(total_increase)<-'increase_patient'8 w8 y9 l1 z8 |2 {- @
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")( ] v2 a5 V0 g3 E
ggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+/ S4 W0 r: `* m+ u. o& ]
scale_x_date(date_breaks = "14 days")+
- Y( o- t; ` X4 A3 c- O labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
$ \) }( q ]8 p( E# p& K4 [6 _2 q theme_economist()+
( ~# D) G! I; I& {( R* Y+ c scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签( O+ n. e1 M/ o( j
breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),
9 e* F, |, h4 F- Z# a- G4 f3 h1 b labels=c("0","20万","40万","60万","80万"))+) ~ e- ?4 o2 N% S0 F0 R
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
6 E7 M: K! u& g- R! K* c7 b axis.title.x = element_blank(),
y! x/ `; a' j4 K axis.title.y = element_text(size=15),
. h% o5 F- W2 g1 ` axis.text.x = element_text(angle = 90,size=15),$ c S% p9 }0 f* z# m) S
axis.text.y = element_text(size=15),
8 d$ Z( h9 {: k/ _, b8 T4 M( t9 } legend.title=element_blank(),
* E) o8 p& a' {' P* D legend.text=element_text(size=15))
2 h% G& J) J' K( O6 C: f1 m8 U0 e0 _2 C* y
! D1 `- o0 F$ I
三、新增确诊病例全球地理分布
D" M! g4 O' c. g/ S& P, {mapworld<-borders("world",colour = "gray50",fill="white") 6 q8 X& q6 W9 D
ggplot()+mapworld+ylim(-60,90)+
; ?& h- O4 H) x8 l p1 d; l3 i7 [: o geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+
) L3 s" B$ ?) }4 G1 j scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+3 f, E- g5 ?9 C0 {
theme_grey(base_size = 15)+/ E7 p1 @" b) O% z* d
theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
8 v* A& c [; ]5 @/ s legend.title=element_blank())
) X4 O- y4 ]! e( `
5 U+ ^4 f4 a$ R" c5 w6 tggplot()+mapworld+ylim(-60,90)++ ^: T+ v- |9 [) \' \) u
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
5 J- d6 J4 O+ k scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+
* T- a6 k2 j ]* l, f' V theme_grey(base_size = 15)+% c# w8 c7 j% a' M
theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
1 l" J' t( L$ S! h1 \ legend.title=element_blank())7 i2 w' Y5 B7 H
9 K5 ~0 t1 f3 S6 h, |

* i7 f6 B7 g; C& n3 p7 B; s7 W四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
0 N L7 E& O; X* i% y
cum_patient<-data[c("Country/Region","2020-12-07")]
cum_patient<-cum_patient[order(cum_patient$`2020-12-07`,decreasing = TRUE),][1:10,]
colnames(cum_patient)<-c("country","count")
cum_patient<-mutate(cum_patient,country = fct_reorder(country, count))
cum_patient$labels<-paste0(as.character(round(cum_patient$count/10^4,0)),"万")
ggplot(cum_patient,aes(x=country,y=count))+
geom_bar(stat = "identity", width = 0.75,fill="#f68060")+
coord_flip()+ #横向
xlab("")+
geom_text(aes(label = labels, vjust = 0.5, hjust = -0.15))+
labs(title='至2020年12月7日累计确诊病例前十的国家')+
theme(plot.title = element_text(face="plain",size=15,hjust=0.5))+
scale_y_continuous(limits=c(0, 1.8*10^7))
- q" n* B h0 A) n( o) o5 o
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图% y3 y) U7 c3 X$ x! q, @' L5 d: R
cum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07')7 z: d0 Y0 s, Z
colnames(cum_patient_time)<-c("
rovince","Country","Lat","Long","date","increase_patient")
* q I6 r6 p" p- d+ Wfive_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))6 o. a0 E8 d$ W4 J# G5 o
five_country$date<-as.Date(five_country$date)& V6 r, [. I5 Y* p/ |7 t2 b* p6 g
% z) N! K3 \7 D% t' e% V. `ggplot(five_country,
: H" x/ a2 V6 W5 a: H aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) +
& c' G& l: H6 R/ P' c) d geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) + ; y9 E: t; [: a, {5 k4 }
geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+
* H$ }5 n# O* S1 |; u& L scale_fill_brewer(palette='Set3')+ #使用Set3色系模板
1 e3 r. N" P9 x/ W( O& y theme(legend.position="none",: t9 K+ @6 N% E% e" R
panel.background=element_rect(fill='transparent'),
7 J7 G5 s, p8 N) C axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1)," ^$ Y6 [- [1 Z
panel.grid =element_blank(), #删除网格线
# l+ M* X' }: h( g5 w: q' j axis.text = element_blank(), #删除刻度标签
0 i/ q+ b- m$ W1 n& L. \ axis.ticks = element_blank(), #删除刻度线
) j2 j) T! G v0 Y' }( a# f )+
) g, _2 G; i+ f1 g# x coord_flip()+ ( T, p- M7 A; v- r+ A
transition_manual(frames=date) + #动态呈现
f+ @! N, K* S7 u2 S labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+
5 D* M8 Y- n! @0 v/ F theme(axis.title.x = element_text(size=15))+
) j/ u7 X! F! W1 R; |) \' H; y6 z+ i ease_aes('linear')
. w) r8 U+ X0 h/ q8 @- b% _: Z! g) O
8 ]* Z+ P0 W7 q; Panim_save(filename = "五国累计确诊病例增长动态图.gif")
8 Q) A6 u: m8 [* B4 ?* @7 e/ A: ]+ g( a% x* Q1 D

% [: C8 o! l0 c1 l' s" D; _2 l2 n1 |3 b& g! L