- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40031 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12720
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
可视化实例基于R语言的全球疫情可视化, y; W+ }4 R0 I+ l4 \* S% r
目录' x8 v6 B6 R3 k8 p' T
一、数据介绍及预处理 [7 g4 ]5 J a4 H' ~5 f
二、新增确诊病例变化趋势" |0 o& d+ M5 t9 E2 _: w( I( J- s' X O
三、新增确诊病例全球地理分布4 _) x4 J4 E7 [. Z, S, X
四、累计确诊病例动态变化图
; A, M. x0 W& \) Q一、数据介绍及预处理8 U' u0 R' h! l- [" y
1. 基本字段介绍
6 d4 U( `3 m& w8 ]3 Z( ~ B
+ V6 @* e& ?6 d! d! n" d9 F5 W3 C字段名 含义
+ \" p: b) r* F, t6 l. BProvince/State 省/州; S6 j8 J4 F- A3 Z% `0 y- T
Country/Region 国家/地区
- H) }/ }* B% s4 F8 l6 h- H7 w/ R& WLat 纬度
- F' Q' H4 o& V* x# A$ n4 q. {/ b* jLong 经度
, B# x9 c5 }" T) y1/22/20-12/7/20 每日累计确诊病例/ U, C4 [8 Y) @# r' P6 E
0 @$ [* n: p, R+ b' C# l# \ ) p3 Z9 M4 ~$ o0 |
! C; u/ _; X0 x+ i* a% n4 b5 S5 Y
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)]
8 C I) ^3 }% F4 p' t( z5 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)]2 B3 f. G' Y. ?4 I0 e) F' F3 N7 B
[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)] a1 ]! Y" N! B, B
[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)]
% j; ]: V7 H* ]( K: P9 ]5 f3 f: E[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! G* p: t9 ]: I; F6 [3 j" cinspect_lag_data<-cbind(0,inspect_data[,1 ncol(inspect_data)-1)])
- X, S+ S9 z) Zincrease_data<-inspect_data-inspect_lag_data
7 P& t% f% h/ x* p% {0 Z
. v+ E! l5 _- J& P2 E#合并数据,new_data为新增确诊人数数据6 ] T# I! C/ z5 p
new_data<-cbind(information_data,increase_data)
1 S n( _, ?# S9 `! C* y8 g0 e( V
2 P& Z* \* k, T; _1. 中国新增确诊病例变化趋势( L* Q+ X. r( W# s2 s6 }- E
#合并所有省份新增确诊人数6 u) N5 [7 b" J' l; t
china<-new_data[new_data$`Country/Region`=='China',]( n( l b- G, ^! w3 H
china_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
/ g4 K/ V- U$ o, K* _& Acolnames(china_increase)<-'increase_patient'
+ X: Q+ \/ n0 O+ v7 U9 ochina_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")
- B4 d! C; B' {( `. W% V% K7 g/ I$ H
ggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+$ [4 Q, o/ z" A/ y
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)- f" l6 t& h" d0 n- y1 r/ y
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+
: v3 P2 h5 K, T# f# n' u" T theme_economist()+ #使用经济学人绘图样(式ggthemes包)
: F$ N- K) A _ theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
2 }/ I- M8 V) B7 s axis.title.x = element_blank(),/ A" _5 M+ k3 s# T5 A
axis.title.y = element_text(size=15),# `0 `8 Z9 ~/ `+ W7 r) L6 ?
axis.text.x = element_text(angle = 90,size=15),
$ n- x% }3 ]- g i, c, H! C4 m! ] axis.text.y = element_text(size=15),
% k# T9 D" H- l' v, Q- i: m legend.title=element_blank(),# }3 |! i u3 Z4 a+ h' p* l! P' ` E- h
legend.text=element_text(size=15))) ^6 X* A6 h V) e
5 Q2 T. L9 d# l3 K/ s: { ![]()
) M! f O5 ]1 @" {. i- g) x( i2. 美国新增病例变化趋势
. x3 S+ D- F1 }/ N/ o' G" yus<-new_data[new_data$`Country/Region`=='United States',]( J$ R) |7 R' ^- k
us_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07'): Q) k' S' y7 u9 \# Z
us_increase$date<-as.Date(us_increase$date)
% h# @7 l# s$ l' e H$ c5 d' l- mggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
8 n; w* u5 d$ S. I6 K scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天
4 p2 x0 u$ x" [ labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+
7 c. Q& M, X* {2 D% K$ I+ F theme_economist()+ #使用经济学人绘图样(式ggthemes包)
5 L2 W$ V+ `5 k- Y- [ theme(plot.title = element_text(face="plain",size=15,hjust=0.5),0 R$ V( D# Q7 @/ L0 K4 ^$ @
axis.title.x = element_blank(), ^7 Y) C. Z0 k3 T$ w2 @- a
axis.title.y = element_text(size=15),- H5 B1 J4 B0 P# u4 t$ o
axis.text.x = element_text(angle = 90,size=15),/ z* |9 U' [8 }
axis.text.y = element_text(size=15),
, v$ L: w% M! N' W ^ legend.title=element_blank(),
" ?1 c/ x* Y% }: C% _4 Q8 j legend.text=element_text(size=15))
0 i. X# ~& y! X- U( q# O- S3 Z3 ~. s: w ]$ W+ [* c$ x7 C% c( `
4 l% ^% ?1 _0 c0 K
3. 全球新增病例变化趋势
. y" N1 R: b4 r' o) V C" Y. N9 g; Xtotal_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))7 [( l& z. N* y$ y; j& M+ ?3 H: z
colnames(total_increase)<-'increase_patient'! G8 W, g% a* S6 N* V
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")' `* s. i" r" d% ?
ggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
3 {0 L' z1 A: Z3 \# o# j6 Q scale_x_date(date_breaks = "14 days")+ A) L8 o9 B+ q! Y& O
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
; T2 v% r' c: } J theme_economist()+
0 d2 n/ o# o, t, ~0 o scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签
z, Z& P \$ S7 M4 Y0 e7 A' k breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),
: W' d8 v6 h: Z7 ] l! k/ i7 ? labels=c("0","20万","40万","60万","80万"))+
4 {2 R% k: f4 P- O7 o3 q9 K* b theme(plot.title = element_text(face="plain",size=15,hjust=0.5),* ^) a" q) m- _! ^
axis.title.x = element_blank(),
9 a4 c2 g6 o6 L b axis.title.y = element_text(size=15),: }8 G& W2 G4 g' p! r7 z" |1 D
axis.text.x = element_text(angle = 90,size=15),+ \+ F8 E# ^$ i& ]7 m
axis.text.y = element_text(size=15),% ^9 A) y, v' s5 g
legend.title=element_blank(),3 N! I4 r, X+ R; J; o
legend.text=element_text(size=15))
3 c9 c4 p4 x3 Q* U3 T- w# `7 n, k" u h* p' X& g
![]()
8 \! p6 G: t4 w5 h* _4 S) D三、新增确诊病例全球地理分布
: N* s* L0 N9 i" R1 j2 w! |* \& Y" amapworld<-borders("world",colour = "gray50",fill="white")
/ ?# D2 C: T; S1 Q$ x8 o( N; \" N. Kggplot()+mapworld+ylim(-60,90)++ A+ ~# [" D: k$ N/ i6 _9 R
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+
; l* i V" |7 w7 ]3 c" R scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+4 D3 @/ m/ b) S1 \( Z7 t
theme_grey(base_size = 15)+4 h1 ]; w( [) W" u2 M: h+ q1 U7 h* X
theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
) Z0 z7 z1 ~0 B+ t8 `2 G) v( T* F legend.title=element_blank())
8 V: j$ N' f o/ R1 }* p& q
2 L- s/ E8 w6 u1 g7 M& Yggplot()+mapworld+ylim(-60,90)++ _( V$ |/ t" _/ j5 G
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
0 v6 _8 X1 s6 l4 a/ [ scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+
, n' J) i* b( P8 H0 D% o4 w& S) L# M0 J theme_grey(base_size = 15)+
: U8 X% N* e) u M, h3 e# P theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
% x5 v3 ]% V! U2 ^ legend.title=element_blank())
' m! f* L- @8 d) W: q5 r8 v' T# ~
![]() ![]()
1 \& k2 A; t8 r( x四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
$ Q g( v, }( R7 _4 \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)) 3 F9 U& ~/ R9 `7 q+ S+ \4 U
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图5 g8 e! R: T( L T
cum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
+ ]6 _$ b0 |& N# qcolnames(cum_patient_time)<-c(" rovince","Country","Lat","Long","date","increase_patient")8 U1 E. p6 S7 q7 F' M
five_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy")), P( C6 y0 @) ?+ T4 x& l! s
five_country$date<-as.Date(five_country$date)) r; ^& x/ w; a$ W! Y$ r- ?9 s* T
( A Z! W3 x- |3 n4 I0 Mggplot(five_country, & m; H, K( m5 ]* s7 k0 Z7 o3 b
aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) + 6 K# f: P6 y, `0 V0 e v' k
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
* p# x* r$ c% W- k geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+ * X, N6 O3 x+ k8 B/ J6 u' J
scale_fill_brewer(palette='Set3')+ #使用Set3色系模板
# I! {6 Q3 @+ x7 f theme(legend.position="none",
9 L2 B1 ?- U+ X p( E/ y9 G( G panel.background=element_rect(fill='transparent'),
# `* x, F7 V! S! X2 N ]4 E axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),
' R8 _6 S9 I' o5 O2 ]1 `4 G panel.grid =element_blank(), #删除网格线
@ n8 ~1 E' I4 f axis.text = element_blank(), #删除刻度标签. k8 x( i! a' }. f7 W2 ]$ A
axis.ticks = element_blank(), #删除刻度线8 Z* c8 d+ }5 F& a/ q- B' J! }( H$ \
)+
( v1 b% {, l% Z2 [" u coord_flip()+
! Q5 z; ~' @- e2 y. a. l9 J transition_manual(frames=date) + #动态呈现/ E8 P' H7 U, n2 ~( ^
labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+
+ q" m) Q' L7 ~+ r5 T theme(axis.title.x = element_text(size=15))+
; m6 }6 a3 d* @' Y7 U9 G ease_aes('linear') 7 W; T# H7 M4 e. k, t9 k
* L9 D$ a* `+ y$ H; j$ m( _anim_save(filename = "五国累计确诊病例增长动态图.gif")/ S: Q K: s+ g1 s2 } _; O& K0 f
% Y) v: F" E0 w! s M+ F
![]()
, e9 |/ }9 M. `' Y( ~, C# a
% P& Q9 L1 ^0 x/ y5 m. B5 k $ u: |5 |* ^# f; B& G
! y2 M! ~1 Y" E* r" w3 T% p
|
zan
|