- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40219 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12777
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
可视化实例基于R语言的全球疫情可视化1 u$ ^( i" f1 M9 ?% K
目录
. h0 n7 [$ t/ R一、数据介绍及预处理
1 U4 ^9 @- v7 x) E4 |二、新增确诊病例变化趋势
4 Y& N- `$ u+ p% f F三、新增确诊病例全球地理分布
6 R+ g, J$ w# K1 g, d, c/ Q四、累计确诊病例动态变化图- E1 l& O9 {2 M! ?
一、数据介绍及预处理8 T, G* e- w2 Z3 c) l! I; u; O$ _
1. 基本字段介绍2 J2 @" P' q. C7 l) S' j# i- Z
$ n3 ~5 f3 D: c
字段名 含义
. q; l# D0 Q( O- m5 {; OProvince/State 省/州5 d `2 j0 p/ O$ T8 R- d
Country/Region 国家/地区2 V. B' g( v6 ?& g" H, T
Lat 纬度7 a9 ]' ^4 L+ ^4 K
Long 经度+ h: c$ y! u- a8 V0 M6 o0 `0 n
1/22/20-12/7/20 每日累计确诊病例
1 X6 o- k+ G e. ~4 i( p8 d- c
5 R, _( ~' A2 D![]()
. ?+ K. m& O2 h. w
3 `" p8 b/ a/ z* H; U2. 数据预处理 - 整理某些国家的名称,如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)]
+ V% X* {9 a2 `8 R: v* c# A A. ][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)]0 u6 u$ C" s3 M2 o8 \
[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)]
+ V& B' N3 e8 a; v/ g# 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)]
5 f* v& H# {1 f1 Q$ B: v* M+ z) d[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. k3 J2 w' H+ |
inspect_lag_data<-cbind(0,inspect_data[,1 ncol(inspect_data)-1)])
- W5 x+ A. J* ?increase_data<-inspect_data-inspect_lag_data
4 W) s" M: }0 t. I6 ^2 g% m
5 Q8 J/ S$ J& t6 G. k( e7 j#合并数据,new_data为新增确诊人数数据: T- j; }9 T, n; t7 d
new_data<-cbind(information_data,increase_data)! S' a$ _8 {& w8 z
0 `$ j: r1 v2 w( W
1. 中国新增确诊病例变化趋势
( C% s. ~, | y i9 h' {#合并所有省份新增确诊人数" D% ]# [4 M1 I
china<-new_data[new_data$`Country/Region`=='China',]9 Q6 V# Z- O2 H J6 \& s' w
china_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
6 y- O2 e; P; {/ ucolnames(china_increase)<-'increase_patient'
3 _8 r2 t. \9 L7 ochina_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")& l8 t" ^7 I8 H. p- t5 W# @! ^
) k, e! h) Z0 O- M# w. W/ ~. @! gggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
7 i/ \: R& r8 w8 u' N \% X scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)
) H* E' Q: W: M* m+ }; T5 H labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+
: t, X$ Z; ?! [6 N* C theme_economist()+ #使用经济学人绘图样(式ggthemes包)
9 b. y% T3 w3 }2 F& [ theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
7 A; I0 U4 [9 J: G5 | axis.title.x = element_blank(),
' G. l5 f4 ?$ d- e axis.title.y = element_text(size=15),; r$ K5 A" P. T! [; h1 @
axis.text.x = element_text(angle = 90,size=15),
! P8 K( B) a) O9 T1 b& o axis.text.y = element_text(size=15),
1 v& ]# G$ v. H+ f* B! f legend.title=element_blank(),/ L- g) @& V; ]2 A6 @: d3 S
legend.text=element_text(size=15))8 I6 q* U5 ] V' G+ Y
# }4 N: i3 k9 m5 G2 `
![]()
0 c) E4 Y& Y$ C# R' R! c2 Y2. 美国新增病例变化趋势
6 N# v$ y/ P4 j% x& _% Kus<-new_data[new_data$`Country/Region`=='United States',]
1 n5 w1 T& w- I, J% zus_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
& _2 H, D3 x0 Aus_increase$date<-as.Date(us_increase$date)% F7 [2 Y8 {' U. g4 r" [
ggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+7 F0 d8 ]% Z# i$ A3 y
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天
2 T. |6 n5 L8 k' D+ e* ^! Y# D labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+
5 u+ o# N0 t$ s theme_economist()+ #使用经济学人绘图样(式ggthemes包)9 B z4 q0 `+ ^2 Y
theme(plot.title = element_text(face="plain",size=15,hjust=0.5)," ~! l# k) V& Z; ?# h4 L8 ?* O
axis.title.x = element_blank(),
5 w% ~, ^; E" S3 H axis.title.y = element_text(size=15),
7 W- D3 M R# r0 x, o axis.text.x = element_text(angle = 90,size=15),
6 s! F6 H& E- T" J6 N% j axis.text.y = element_text(size=15),
6 _) Y( U& V5 m% m legend.title=element_blank(),
, o+ r1 j C' U$ b. x; h1 w; J legend.text=element_text(size=15))5 P) z8 X* U) i6 t( a) [3 S1 q
7 U. U& z& ^; d5 L4 m, e
![]()
! X+ M$ @% [+ i0 R7 o3. 全球新增病例变化趋势0 W9 j3 T0 O2 [4 o6 K- b' @+ h
total_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))7 I: v, `, l4 R3 J y8 ]
colnames(total_increase)<-'increase_patient'" R. g3 f |- Q
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")
9 f) u9 H" j5 Dggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+6 Q) D* Q; p, ]3 E8 I: s+ E2 U; U! N
scale_x_date(date_breaks = "14 days")+
' @4 n( z8 l2 Q( n labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
8 ~8 D5 z! \0 m" ~9 h I9 c theme_economist()+7 h. s6 U D) F) q+ l0 @4 a
scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签* k: t3 |: Z, p4 d0 |2 E. i0 b
breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),
g8 x; g& L7 Q8 h; k9 K: ^ labels=c("0","20万","40万","60万","80万"))+
4 N3 d. |/ N! b theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
. x. ^9 m! Y, m axis.title.x = element_blank(),5 j+ \9 ?- N9 x U/ @
axis.title.y = element_text(size=15),# O- E5 o1 g; k
axis.text.x = element_text(angle = 90,size=15),
" K+ @7 ], p9 }) I axis.text.y = element_text(size=15),2 K3 w) z' U7 m
legend.title=element_blank(),
' l& D$ l' M; W- A+ D8 P legend.text=element_text(size=15))
8 d0 p, q% O8 j/ V0 S2 n1 l$ q% l. \1 j$ l1 o, S, L6 F8 L6 U
- k4 R1 a+ J4 p' u( E8 y
三、新增确诊病例全球地理分布' e) h1 D( c2 S9 x
mapworld<-borders("world",colour = "gray50",fill="white")
; \! f3 c8 m' a: q' _% [& ^+ Yggplot()+mapworld+ylim(-60,90)+: o4 B0 m2 R& a3 b( _
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+
z$ K1 O% P: N" f2 C7 z9 l1 d scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+3 C3 c/ N& F- }, s% p3 ~- y
theme_grey(base_size = 15)+
- c' _5 G( Z( g% G; J- M) b theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
- Y! ]6 \ F* f# V8 Y& l; X! ~ legend.title=element_blank())
! f* r4 t1 G+ l! ?' B# o9 N1 t! ?5 Z) _
ggplot()+mapworld+ylim(-60,90)+3 m' Y) L! y2 D+ c+ I4 o! R+ Z& r
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
1 q9 ^* g5 [% M scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+
4 w* C: `4 U& Q7 ?" |/ [' W theme_grey(base_size = 15)+
6 y* q" p* r. x/ `% E7 J+ e- f theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
: e7 g! M* P& T8 B7 M legend.title=element_blank())" H) p5 F% C& O: o$ x& }
) U! V/ |$ N( }1 j: t6 }![]() ![]()
* Q/ Y& y9 Q5 @3 {$ E四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
9 B8 Z9 M- M3 G' B& L) 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)) ) i# M2 w, e" I/ t
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图
- i. K9 T- s Q- P# `9 }3 ocum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07'): \* j q. V2 z* `- C2 n( K
colnames(cum_patient_time)<-c(" rovince","Country","Lat","Long","date","increase_patient")3 s0 Z7 c0 u2 X0 j* Y. u& l
five_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))
- B8 z: ~( ~" H# N, ^# Qfive_country$date<-as.Date(five_country$date)
5 \1 ^6 [; v+ I4 ?' f
+ v- z1 o$ X2 H9 t9 g/ C2 {8 z8 Hggplot(five_country, # w9 g# m4 l9 n% |5 H* f' u
aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) +
4 i4 F3 }2 K( U% |/ `3 l geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
4 _& y. e) ]' V! \. x2 e+ y7 K2 E geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+
8 h) E+ L* q8 X! z# O. n scale_fill_brewer(palette='Set3')+ #使用Set3色系模板
9 c8 G6 Y1 T+ C Z5 q i theme(legend.position="none",
7 d3 |, C+ m; ~! ^& X panel.background=element_rect(fill='transparent')," A" {' H% b8 }
axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),2 d3 ?( H* j2 ^# a3 f+ O! A6 a
panel.grid =element_blank(), #删除网格线
3 x: ~8 u; W. ^ axis.text = element_blank(), #删除刻度标签
0 d3 t$ v, i) N axis.ticks = element_blank(), #删除刻度线
2 i a* f9 G# l' `: c8 T5 }) A5 i )+7 \3 z0 P7 W; _
coord_flip()+
! J2 }# L$ \+ t7 A. |% | transition_manual(frames=date) + #动态呈现3 z: O" `/ l& w' G
labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+ 1 ~7 X+ b# B$ }0 u+ _
theme(axis.title.x = element_text(size=15))+
2 s4 \) M$ Y: x# c! S ease_aes('linear')
1 o3 y/ E4 ]* }3 o
3 }- ^; t/ k1 v G* t9 wanim_save(filename = "五国累计确诊病例增长动态图.gif")* E; q9 W4 m) X- N
D9 |& F" s6 V" r
![]()
$ L3 E/ L+ }- c6 j3 ]0 _& R1 H4 Y/ Z5 E
9 j( _+ `" b; U* c5 p/ Y( O5 I6 v6 j
- X4 b2 W9 `% [' v
|
zan
|