- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40293 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12799
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
可视化实例基于R语言的全球疫情可视化
' z1 X6 i2 Z0 H- H4 ~- ]目录
* G0 y$ V/ J) P一、数据介绍及预处理, X: h8 ?8 f9 f5 A* p5 v1 Z1 X5 R
二、新增确诊病例变化趋势1 b3 r0 |9 i) V. _- r" {
三、新增确诊病例全球地理分布
; Z, Z6 F' b- c) j; e' ~! j+ _! L. [四、累计确诊病例动态变化图5 z3 \" r3 K8 V
一、数据介绍及预处理0 {- h/ i+ g; B3 ]% R1 |. o/ |8 q! R$ Q
1. 基本字段介绍
2 T% h+ o6 S7 Q- _; Q+ ]
7 o" s# w8 N: d" ?1 Q" `2 e字段名 含义# E5 l. I B7 f* X% g
Province/State 省/州
9 \# S' T+ N h/ F$ dCountry/Region 国家/地区
& i0 H8 m0 w+ }- H! l8 JLat 纬度
& b1 G5 B5 I; w5 O2 h4 g; H0 WLong 经度
( j: h/ P& c0 f$ G1/22/20-12/7/20 每日累计确诊病例) z% G5 F4 n+ M% T
* b$ T8 Y6 D3 @8 |9 Y
* ^( r% k: y* k5 U
5 e2 S4 R; D% X) h0 {+ b6 x" P
2. 数据预处理 - 整理某些国家的名称,如Korea, South改为 Korea
- 将日期列字段修改为相应的日期格式
- [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)]
4 w3 w5 Z+ m8 y+ J- G[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)]* U( c) P+ _/ o3 U
[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)]$ R8 E y z2 H7 G. H; l- a2 X8 t
[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)]5 q ^! {+ i5 l, F: G
[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)]二、新增确诊病例变化趋势#由累计确诊病例计算新增确诊病例
' r* q0 Z+ l. P9 v8 z( H5 Cinspect_lag_data<-cbind(0,inspect_data[,1 ncol(inspect_data)-1)])
! }- }2 G2 w% h8 z0 H6 V! Uincrease_data<-inspect_data-inspect_lag_data
2 w' n+ V% e: l t/ H9 G+ v5 }: s. E2 ?1 N: V2 }, K
#合并数据,new_data为新增确诊人数数据8 n1 Y: ]2 i, p* ?) q% m1 s
new_data<-cbind(information_data,increase_data)! j4 `; t5 l& {0 @1 R. C1 [
# e" r% }6 O! _1. 中国新增确诊病例变化趋势3 `9 M+ R8 {: z9 p
#合并所有省份新增确诊人数
5 I7 W ?5 {3 e+ @china<-new_data[new_data$`Country/Region`=='China',]
( ?7 s) c* q4 F! h+ V" U4 Hchina_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
5 K6 V+ r6 @: A# I7 b7 _colnames(china_increase)<-'increase_patient'
" K+ g! i- Q) Z1 gchina_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")
$ ~" a0 d3 z0 X. n2 }3 j
1 m- f/ o5 A- z) ?3 Iggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+- [- {$ X6 U; N: Q! Z+ h* J
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)
C5 w7 O& b/ e2 [ labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+* I$ v3 V7 d3 c
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
' n! Y* J5 \2 g+ s( L- E theme(plot.title = element_text(face="plain",size=15,hjust=0.5),4 {. u; {1 l, |# N7 s. N( s
axis.title.x = element_blank(),
' D& z e- ^% v z; B0 y axis.title.y = element_text(size=15),
B& ^; J& q2 O1 D: g) g5 l6 K axis.text.x = element_text(angle = 90,size=15),# x3 d4 X: c$ ~
axis.text.y = element_text(size=15),* n1 f: s! Z4 e9 n2 g9 l3 r. k
legend.title=element_blank(),
2 \9 d8 g J* P5 L7 b) J legend.text=element_text(size=15))
5 Q2 ?9 E* }- |* p3 O6 \
2 Y/ P4 u& i$ R) m ![]()
* F: P1 `% g! y" P2. 美国新增病例变化趋势: v7 R" d/ B3 ?+ ^) ?
us<-new_data[new_data$`Country/Region`=='United States',]
! Y& E* I' ~% [: O9 ]1 g6 c1 q; ?us_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
- S4 ], C% z& ?: n4 z4 \# I/ l9 bus_increase$date<-as.Date(us_increase$date)8 ^" f0 X0 y \6 ?! z/ V% T7 j: ~
ggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
+ R: W! U' n0 i; n& u; d/ m" S scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天
/ S5 s2 y0 h/ J) W5 y( t0 ` labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+
; e0 t5 d& F1 f O8 o( c theme_economist()+ #使用经济学人绘图样(式ggthemes包)
3 z8 I) B8 ? R' X! p theme(plot.title = element_text(face="plain",size=15,hjust=0.5),/ U- B+ k8 X4 H5 k1 E: W1 C
axis.title.x = element_blank(),
- ]8 a& X, V* e$ R. U+ q axis.title.y = element_text(size=15)," I8 Q1 @$ ~: u, Y& z
axis.text.x = element_text(angle = 90,size=15),
8 @" J9 [7 X% Z B) T+ ] axis.text.y = element_text(size=15),$ |2 u; e: e$ Y) B) O g
legend.title=element_blank(),( t V9 A! G6 B2 F! B. J z+ K
legend.text=element_text(size=15))
( x; z$ e$ B- |+ _9 i
& w) i0 d7 O! D" n0 R" z& h$ {![]()
. O3 v1 |8 y# S# D3. 全球新增病例变化趋势
3 z7 Y+ Y' M& Gtotal_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))# I, l5 l# y# j, }5 X( s; Z
colnames(total_increase)<-'increase_patient'& ?) V6 h" t" i: K
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")
/ v# |; E5 [' _9 i4 G8 T9 qggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
3 X% W6 C' p' ^7 z7 Q' y% v i. G4 m scale_x_date(date_breaks = "14 days")+3 e) D; v- m: G- s, `
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
% d: m" q+ u: d2 b- a theme_economist()+
1 E, Y4 s3 d8 o4 D5 i2 w7 G3 j scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签
9 j) {; Y: R! g7 @ breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),- z7 O7 Y; c8 j0 y% }
labels=c("0","20万","40万","60万","80万"))+
) y+ P, S. i! R# P2 V7 f2 m% A) C theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
( b: o; _, r T6 f" m axis.title.x = element_blank(),% Y/ h1 d; _, m% Z
axis.title.y = element_text(size=15)," }9 M" T" U0 V
axis.text.x = element_text(angle = 90,size=15),
' Y0 H6 P# ^" O6 ]! b3 C9 g, f axis.text.y = element_text(size=15),
+ \2 l8 _( z% ^% L legend.title=element_blank(),
7 S1 K! B( D% B: G! ?5 e2 ` legend.text=element_text(size=15))+ H" h* o( {9 E- E9 |
9 W7 R4 L- y2 {! s/ ~# i6 N
![]()
. X8 v2 Q; [, U3 t7 g5 T三、新增确诊病例全球地理分布! _! v( _2 ^5 P" V$ i% k
mapworld<-borders("world",colour = "gray50",fill="white") & w; I4 Y, X6 L
ggplot()+mapworld+ylim(-60,90)+
0 Z8 Z: F9 J( m' a- [& o geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+
! r7 L0 R: q" [: R2 j2 U scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+
% F3 U+ q: M; @' I, l# e9 L l theme_grey(base_size = 15)+
$ P5 Y/ [! l# Q' Y8 K theme(plot.title=element_text(face="plain",size=15,hjust=0.5),2 w2 P1 C9 Q; a' R/ l
legend.title=element_blank())/ p2 L5 S: y& p: c
1 L1 N* c2 O8 [5 e# O. o- \
ggplot()+mapworld+ylim(-60,90)+
9 ?/ ]! |% e5 I" T, \0 {* @ geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
8 w- @+ h$ }* f3 T/ m. A scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+5 P# W* t0 n* [2 Z
theme_grey(base_size = 15)+
% F3 h, G' u" A* } theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
" K7 O2 p- w$ ]' Z7 D legend.title=element_blank())5 A! r3 z( ]% u: l- f" a3 F0 c
* U5 i8 `* ^9 ?![]() ![]()
; Q) h0 Q: U. C) | I; g四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
3 A) c/ Y$ N+ T2 Rcum_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)) 6 V# A% r1 }2 |# A
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图
/ _! U0 h, b/ Jcum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07')7 w4 a8 ?5 V$ i) P/ g
colnames(cum_patient_time)<-c(" rovince","Country","Lat","Long","date","increase_patient")
6 P; B6 l5 i) d8 a) }five_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))
6 l6 s4 m' A- N. Y, j% Y0 }( Zfive_country$date<-as.Date(five_country$date)
$ B6 o) N! D4 \) H% b' U5 K6 P/ ^* |& c7 t
ggplot(five_country, 0 r9 Y/ l' r, a
aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) +
& }, ]2 E9 b1 O4 F X: X- _ geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) + % \, X. }* d" n( F, H; A
geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+
. t% d/ {& S" n- j scale_fill_brewer(palette='Set3')+ #使用Set3色系模板 I: Y! j2 k2 [+ B# R
theme(legend.position="none",2 `: J$ o. u s
panel.background=element_rect(fill='transparent'),: r. n& P2 L: N
axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),) `& o7 W4 E* m4 s2 L
panel.grid =element_blank(), #删除网格线
9 G Y6 ~: J6 `, J% n2 H axis.text = element_blank(), #删除刻度标签
9 b; {. i% F& ?: V1 n& r5 D: w. O axis.ticks = element_blank(), #删除刻度线! K" K; Z) u2 S# d
)+
5 `" j9 P' q% y& r' f! U6 Y coord_flip()+
- V5 N' ^0 h4 {4 z, }* _1 w transition_manual(frames=date) + #动态呈现9 W& [3 Q$ }; X/ l3 C
labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+
( C0 q: \" Y* v% u% U theme(axis.title.x = element_text(size=15))+9 S- g2 w* a/ H: i. e! k
ease_aes('linear') - \: D$ S3 ]9 O! O- C
( n' @/ Z' Y% J1 g3 y* ?7 f) K! o8 Danim_save(filename = "五国累计确诊病例增长动态图.gif")
8 |# e) N+ b" q4 a7 x/ p ]- U, k- L2 M' a, v; o+ n! [
; M& B" {& m' }
. h2 ^" G1 S& m" n* M5 u4 d
& j! e' l1 f! j1 S ?6 L, k3 e
. R' t* r- K( j6 q6 e |
zan
|