[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)]- l( O7 v1 Q/ k; w9 E3 {$ ?
[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)]
- l$ ], g: q& I7 Q4 A[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)]
" {* O: Q" f0 P3 q. k) s- `+ J[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)], i* G3 N- e" N0 s
[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)]二、新增确诊病例变化趋势#由累计确诊病例计算新增确诊病例# w- Q9 V& f4 `6 R
inspect_lag_data<-cbind(0,inspect_data[,1
ncol(inspect_data)-1)])
) e. g: T8 }# _' |1 {3 Aincrease_data<-inspect_data-inspect_lag_data6 B" c, B% D; { K
+ {- x; U9 e8 Z& Y. n U3 U( w#合并数据,new_data为新增确诊人数数据
@' c+ V7 z+ ynew_data<-cbind(information_data,increase_data). y7 j: M3 `+ W# U# G8 g; O+ b
& y7 a6 P% K- g9 A0 n* z& y: y
1. 中国新增确诊病例变化趋势, I: N/ F. \$ o" d, p
#合并所有省份新增确诊人数7 O% s8 P+ d0 e$ b6 d {, u3 F: Y6 u
china<-new_data[new_data$`Country/Region`=='China',]' v2 h+ \( M) J9 v
china_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
# W9 a& o, Y8 ` t9 D. P5 T: Vcolnames(china_increase)<-'increase_patient'
( Z9 f0 S; _% R8 a7 A+ Cchina_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")
- s n; u/ ]( X0 n, {
- Z* O3 @0 Q/ Z( J! o+ Y1 C2 jggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+) T* p- v- e0 h% A' _
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)
0 _( \. C# L8 k( |. N, a: ` labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+9 ~/ e0 l( h3 R: K3 C5 q9 z" Z2 g' c
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
$ g; F* l" s6 a" B+ ~ theme(plot.title = element_text(face="plain",size=15,hjust=0.5),- `1 L1 u4 K. `2 j* o
axis.title.x = element_blank(),
2 y, N4 J1 @" n8 \ axis.title.y = element_text(size=15),
0 b3 R9 a( ]6 L% B axis.text.x = element_text(angle = 90,size=15),6 C& n: i3 y% X, N7 z ~
axis.text.y = element_text(size=15),
3 r k' `' z- g* d legend.title=element_blank(),
|3 p. p& _6 F/ I c legend.text=element_text(size=15)): m& q3 Z Y+ X+ N5 I. b
, A# q( r) m# T, h+ R" M

( o2 @: Q3 \' G( D3 j2 n, V2. 美国新增病例变化趋势 |$ B5 W1 v3 {
us<-new_data[new_data$`Country/Region`=='United States',]" x5 W6 {$ m( m3 v6 ]
us_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
- d A5 f U% N E/ ~$ }' Eus_increase$date<-as.Date(us_increase$date)+ V- N _* X. M4 ~ J
ggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+- |: z! g6 K8 y3 e0 L7 l% A
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天7 Z8 F4 e0 s, G0 e$ c V
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+( L8 @$ O3 t5 q% Q" X) n; [
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
' Y- _# A+ _3 P6 W0 V4 J theme(plot.title = element_text(face="plain",size=15,hjust=0.5),$ S; r" F/ _1 H1 ?! x
axis.title.x = element_blank(),
. R r- P# q1 m" Z axis.title.y = element_text(size=15),
1 l: a" p! o6 U' Q axis.text.x = element_text(angle = 90,size=15),
) z, {" T; r8 U1 l) C+ m5 b axis.text.y = element_text(size=15),; |. Q, r; R4 z; x/ i
legend.title=element_blank(),
! p) c7 @# i: O1 o+ N- w+ m legend.text=element_text(size=15))
1 o% `# I. a9 r) a6 o" n; f; @ S
7 {, }0 b5 V/ K, d; d7 c) M/ K2 M
+ I/ g* B f* C
3. 全球新增病例变化趋势
3 J7 l; t' Y6 l8 J1 Ntotal_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum)): H/ D. u0 T$ {3 E; o6 B5 l0 y x
colnames(total_increase)<-'increase_patient'! M. w2 I) {/ T$ B
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")+ T0 o0 ~; t% M2 `) z. c: x- Y$ _" h
ggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
5 [3 O/ [6 c# t4 e# O/ ? scale_x_date(date_breaks = "14 days")+3 U3 q4 \' J7 G2 M$ G
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
7 b/ J' _ G" Y( T% _" m theme_economist()+0 m* v4 a/ V- i. q
scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签% E8 b3 |" u8 y& _* b% ?
breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),
1 V! R, x+ }# j( _9 @" g3 m labels=c("0","20万","40万","60万","80万"))+% m3 }7 P, i4 J% G0 @% ?3 P
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),' ?' p1 r" O* [# Z
axis.title.x = element_blank(),
' O! o% \. o. y' X$ ]5 U+ V axis.title.y = element_text(size=15),5 Q0 u( k$ [5 c& i; l/ j
axis.text.x = element_text(angle = 90,size=15),% D" a9 G% ]& M" g9 ?6 w4 N, k* w
axis.text.y = element_text(size=15),' L& F: ?0 m4 @; E, E0 ^2 K) Y
legend.title=element_blank(),
7 M, Z t/ i4 k/ ^ legend.text=element_text(size=15))
0 C+ b5 ~! |. w3 }% P( X# @- j- \; Y/ p) t
0 ]1 d+ c; h2 M
三、新增确诊病例全球地理分布& H2 u4 X3 A# f; P6 M2 c
mapworld<-borders("world",colour = "gray50",fill="white")
6 ]; u! Y& B0 O( oggplot()+mapworld+ylim(-60,90)+) h2 n4 O2 U2 K
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+5 b+ `, N' O$ ]8 a c' W
scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+
) P; S3 M. f2 _, Q' ? theme_grey(base_size = 15)+
/ j" Y$ m$ v+ C theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
x: J+ Q4 q- B3 X7 @ legend.title=element_blank())& h+ @4 s/ L% V% R9 X# ]
1 [+ }& g6 X4 x) q8 ^9 x$ \5 t
ggplot()+mapworld+ylim(-60,90)+$ Y* L* ^7 N+ |) `
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
- b* [ p* ?( e/ A% v! ~# g scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+! l8 K# \& `6 C k- L$ [3 H9 ?
theme_grey(base_size = 15)+
6 K( x+ X) }' _9 J( F; h theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
6 E6 c0 @' x) \% D; Y' G- { legend.title=element_blank())
C4 x3 ]. e' s$ f; F+ O9 K# d& m) M- o9 U7 w8 b4 M; L3 a

0 j ~- g8 F7 n# `" @ r0 L
四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
: d- l [/ l% n" L; c+ h
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))
( b9 S5 @' Y$ H! E
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图. h+ O& M! P& W
cum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
+ m$ v% g$ C( Icolnames(cum_patient_time)<-c("
rovince","Country","Lat","Long","date","increase_patient")5 p& S" ]* Y# ~6 s! N" |4 @( R
five_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))! q' ?& `) w* |: H0 g+ z. S9 p
five_country$date<-as.Date(five_country$date)1 |9 D5 w* m6 @) K
! Y$ I" Q- I2 \/ s
ggplot(five_country,
5 |8 P; ~! \2 L& r aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) +
2 W6 N; @0 r5 s# b3 r2 I T [ geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
& C$ ~2 J$ j. X geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+
7 N9 ~+ |# d, p/ |; [7 G% ~ scale_fill_brewer(palette='Set3')+ #使用Set3色系模板) K4 x) v/ G& B- t7 `+ H5 v
theme(legend.position="none",! L9 N2 A/ _8 ?4 s
panel.background=element_rect(fill='transparent'),
. ~3 W% r! f$ }4 L( a axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),
0 N! y; [ \8 g7 {$ }1 a- n panel.grid =element_blank(), #删除网格线
8 s% f' j/ Y V( `7 X) v' [& k( j( M axis.text = element_blank(), #删除刻度标签) ~+ w' I" d2 o. I( n; P' H
axis.ticks = element_blank(), #删除刻度线
7 U. u6 M/ n6 |* ]4 J )++ X9 Z& V+ [* e! L8 s! f
coord_flip()+
f) v T% O- D6 Q. W; h transition_manual(frames=date) + #动态呈现
' w6 F% X! r4 }3 o7 z labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+
P: f3 d- W$ u6 K. W1 G3 y0 w theme(axis.title.x = element_text(size=15))+
! x+ t9 g6 B% o6 z0 _! E5 v( N ease_aes('linear')
3 [% T5 e( \" t! c% z& A' L2 o( i( v! b- m1 w
anim_save(filename = "五国累计确诊病例增长动态图.gif")/ W. _' @) X* i; g
3 }2 v# \5 J- |( }" D3 B
( q5 v" {% R; d& X9 I' f
; \5 }$ V0 Q1 H