[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)]
3 {- L4 i( J, S9 `[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)]
3 d1 I [1 G( G4 M[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)]
, [" t; X* q' d5 l[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)]) _; c( D, G8 e6 C* v9 M: k* P
[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)]二、新增确诊病例变化趋势#由累计确诊病例计算新增确诊病例
( D" g" x- K3 C7 X; vinspect_lag_data<-cbind(0,inspect_data[,1
ncol(inspect_data)-1)])
, y' j1 M7 U$ W" Z- W, B T, rincrease_data<-inspect_data-inspect_lag_data1 L7 R! F+ H% Q
: J# D2 M$ I; y0 R$ I E#合并数据,new_data为新增确诊人数数据
; ]3 t3 R2 q3 w$ F; w, \new_data<-cbind(information_data,increase_data)) a0 H \0 ~2 |
6 |+ y! t2 W# f o0 C z' ~ k1. 中国新增确诊病例变化趋势
1 v# B' I5 f! b+ s& U' V#合并所有省份新增确诊人数
, R" j! z2 m% O- w7 ]china<-new_data[new_data$`Country/Region`=='China',]+ P9 p4 g$ n0 g6 Z
china_increase<-data.frame(apply(china[,-c(1:4)],2,sum))6 J, t7 P$ ^0 L& [& ]6 X
colnames(china_increase)<-'increase_patient'5 r, |% B; z' A, X
china_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")
8 ~& v p" ]! d, a
; E5 p. O/ @+ w9 l! O Nggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
6 x: V8 t! L0 c0 U! [ scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)7 T8 m4 X6 R. L% p+ c4 m* {
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+" o& b0 L/ Y# B7 K% a4 }
theme_economist()+ #使用经济学人绘图样(式ggthemes包)! M+ B- B4 |7 S
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),+ t- Z! y! U0 D8 D
axis.title.x = element_blank(),7 l8 P3 E* Z$ S/ o$ Y: _. k
axis.title.y = element_text(size=15),; d) d, V9 b! G0 [, J& y; l
axis.text.x = element_text(angle = 90,size=15),
0 D1 U( J: ?* B9 U$ e axis.text.y = element_text(size=15),1 j8 F9 Z/ G& u- v0 I5 X" S1 l
legend.title=element_blank(),
3 V; Q# R u6 V8 y9 I2 S legend.text=element_text(size=15)) ~! `" ~. h+ ^( Z) N1 i2 p8 i- b/ N
, k9 q" G# N( q. O. `
7 B J* ~7 K1 n0 {( x( T
2. 美国新增病例变化趋势
3 A/ V" p3 J' L- D! z! V5 ~us<-new_data[new_data$`Country/Region`=='United States',]
% Z/ `6 ]1 f1 Z, I( ?us_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
4 P5 X) U9 N o( W9 x6 R/ kus_increase$date<-as.Date(us_increase$date)
) N+ D7 a6 x% Q A( C' yggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
3 V5 _ ^( l2 L3 {! `. s scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天
- ]$ z4 B" b: z: c, n# _6 f. m labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+4 S3 }4 y$ z$ f$ Q. g
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
5 R; {4 A4 v* [8 T _; m theme(plot.title = element_text(face="plain",size=15,hjust=0.5),) _+ f& K$ P5 H( v. D5 f8 x
axis.title.x = element_blank(),
1 f% G& l9 ^, h# T. |' P7 o7 `$ z axis.title.y = element_text(size=15),
- w6 P, P0 t. n! \7 a6 w0 p axis.text.x = element_text(angle = 90,size=15),+ L3 A4 Y1 r% s; e' Z8 C. z2 ` z
axis.text.y = element_text(size=15),
R8 O) R( W2 I# k: i1 ` legend.title=element_blank(),& n& q7 S) Y3 q5 a- [
legend.text=element_text(size=15))
4 C% \: ]- Z- [& j1 |) B1 r
6 V8 a# F: N) _- g8 q
7 }/ X* Y% B% C# J& Y) o5 p3. 全球新增病例变化趋势8 l- n, n: \" Y4 G$ `5 B& u
total_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))
3 e/ c5 t3 W9 {6 w+ C. fcolnames(total_increase)<-'increase_patient'
" b1 R: h6 p+ f0 vtotal_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")% i: Y5 t; i& o) D
ggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
0 k* G# B* O9 c) n9 x0 L' ^+ ^ scale_x_date(date_breaks = "14 days")+
6 Z$ G V. r1 g labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
: d2 h0 `4 ^$ ] theme_economist()+
0 s0 d2 n) U3 n) F. J4 n& S+ O scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签) S u1 {$ i8 \
breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),1 I$ q% W% X5 d" k! T
labels=c("0","20万","40万","60万","80万"))+# p" l8 [8 @! A" w" `( Z# D4 U
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),. Z6 J/ _8 P( J4 C5 B; X
axis.title.x = element_blank(),
# V; p" P4 I% L axis.title.y = element_text(size=15),* Q7 q* T1 ~6 X/ I% h0 c: r1 m* C2 L
axis.text.x = element_text(angle = 90,size=15),
) y. V# g" h& n axis.text.y = element_text(size=15),' v1 S! j' m5 }5 \# q* N) K
legend.title=element_blank(),
7 f% S2 p0 O# c& E& g legend.text=element_text(size=15))
9 y8 ?* h% P: K# ?3 I1 S
' G. }7 r# L7 ]. S! X! [/ K
# g- r% l) G' s' C- F2 O$ g) { A
三、新增确诊病例全球地理分布
+ g- I2 O5 m% A) y% \1 Amapworld<-borders("world",colour = "gray50",fill="white") $ h7 H4 }9 Z1 g: a4 C5 F- [( B
ggplot()+mapworld+ylim(-60,90)+
8 ]* `/ U$ F; z geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+
/ ^" G" V" r; L; F; F: \: { scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+8 g3 M1 a- w2 N Z1 d7 I
theme_grey(base_size = 15)+
, |9 H% `6 h6 [: H7 @8 u theme(plot.title=element_text(face="plain",size=15,hjust=0.5),$ F/ z, }& L+ n$ X# [3 U8 {* @
legend.title=element_blank())
4 k4 a" L/ x7 X) g/ f. A! V4 w% u; q: u& ?
ggplot()+mapworld+ylim(-60,90)+4 U u/ i# Z2 }) W. D( L: {
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
" s, m* _+ T. I4 d scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+
8 B$ X9 N6 x: C7 z z$ I theme_grey(base_size = 15)+$ W+ g P- x1 d
theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
$ j l6 Z7 H* l% Z" f# T legend.title=element_blank())
$ y5 U8 w8 G; C8 U2 m: F: U$ S0 g


; t) ~; f; }: S' V. `' p; Z四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
6 ]. ^) j7 J" A. _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))
4 ^8 y- m5 p' [# |# |
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图
8 c2 M7 }0 y) ^; ecum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07')1 Q1 e; Y/ c3 Y' X
colnames(cum_patient_time)<-c("
rovince","Country","Lat","Long","date","increase_patient")
- U* H( A& |" O$ V; m% Xfive_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))9 _9 m" t) B; {- i5 U. b+ _
five_country$date<-as.Date(five_country$date)8 }" Q. S6 E) N9 }+ b
" C& s a2 f$ C" kggplot(five_country, 6 w C" v/ e; l2 ~3 b
aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) + 5 K* K5 x' [+ W9 o" U5 Z4 L" y
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) + * p8 q" k( k' `$ M2 L
geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+
% ?' T& }( ]; G+ F+ j scale_fill_brewer(palette='Set3')+ #使用Set3色系模板
# b+ e- G- f) M+ f- E* K7 l* x theme(legend.position="none",9 n! R. i) m2 [& {" M9 w: b
panel.background=element_rect(fill='transparent'),
; T! M1 z* e8 M" y axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),
6 a7 ?4 b4 K: T8 Q* h! f4 P6 c panel.grid =element_blank(), #删除网格线
8 ^2 W% y" O) b0 @6 C3 [0 ?3 { axis.text = element_blank(), #删除刻度标签
. R% b) w4 Y! O7 e: G5 a# T# w axis.ticks = element_blank(), #删除刻度线
& w# A* S: Z$ H) d )+# F! \( o9 S2 f; c
coord_flip()+
6 q2 {: a- ^2 k transition_manual(frames=date) + #动态呈现
$ G* P2 F, O5 o labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+
* M; m2 c) q. o9 _ theme(axis.title.x = element_text(size=15))+# q0 s! X* L' K# Q H$ V
ease_aes('linear') # _8 ~; j# Y" X4 {
: s7 b! C* s6 Fanim_save(filename = "五国累计确诊病例增长动态图.gif")' M {, v6 K; m( @
) x A9 z- b, @, m
0 M) U6 f- |8 l2 u- ?. k
2 I$ r" ~0 x, R2 ^( x, Q8 o