- 在线时间
- 514 小时
- 最后登录
- 2023-12-1
- 注册时间
- 2018-7-17
- 听众数
- 15
- 收听数
- 0
- 能力
- 0 分
- 体力
- 40200 点
- 威望
- 0 点
- 阅读权限
- 255
- 积分
- 12771
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1419
- 主题
- 1178
- 精华
- 0
- 分享
- 0
- 好友
- 15
TA的每日心情 | 开心 2023-7-31 10:17 |
|---|
签到天数: 198 天 [LV.7]常住居民III
- 自我介绍
- 数学中国浅夏
 |
可视化实例基于R语言的全球疫情可视化" Y* ?, Q" s% y2 i9 ?7 I
目录
) I! t! o: |" j4 S一、数据介绍及预处理2 K. b$ m6 \( o$ H1 V3 c' i
二、新增确诊病例变化趋势9 P: o @5 K i5 a. R
三、新增确诊病例全球地理分布/ J% Z, E$ z) F: F! @ O$ c: c+ ?- H
四、累计确诊病例动态变化图
1 k6 r' n+ H* z一、数据介绍及预处理 l- Q/ Q2 W* B; A% [
1. 基本字段介绍" k8 N6 K! F. m' Z9 A" l
. d, f4 b1 d, H/ _字段名 含义
0 ^- ^+ E/ V, X: R1 pProvince/State 省/州
J1 i ?- v7 v/ @: ^$ ] `9 _! fCountry/Region 国家/地区( K& O; F8 S6 s
Lat 纬度8 N0 x( `; T) y5 W$ s @$ [
Long 经度1 j8 d: Z. V5 H% e( p4 Z4 \5 Q
1/22/20-12/7/20 每日累计确诊病例0 Y* [: \+ o4 M
6 {/ G) H4 c1 R( m0 p8 H5 C3 v: {![]()
" {6 z/ z4 ~( h5 U0 f9 {; q6 b4 s% y( k" x9 i- {0 u/ \/ D2 n
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)]0 t: |1 P3 H: N: t) }" C
[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)]* S, P7 \0 t8 q
[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)]
0 w ?- E5 C2 X, Y; a1 O' f[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)]
$ \- E6 c& i8 b1 C- m) T[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)]二、新增确诊病例变化趋势#由累计确诊病例计算新增确诊病例1 ?, _8 y' _3 j( ?/ a, B- d
inspect_lag_data<-cbind(0,inspect_data[,1 ncol(inspect_data)-1)])( w3 F4 ~" n% F! A2 s" ^. p
increase_data<-inspect_data-inspect_lag_data1 i4 X6 t* Q& x( G0 F* R3 {
" F9 V6 u7 w& T) I9 o0 j. D9 j
#合并数据,new_data为新增确诊人数数据
; Z9 e. I P- ` a8 u2 Qnew_data<-cbind(information_data,increase_data)8 d! Q3 @( Z ^# a- t! R
}* d I, [/ y# d
1. 中国新增确诊病例变化趋势
. V# t6 o( u' p- T. C) L#合并所有省份新增确诊人数
9 @ o. \! P( |8 V8 Hchina<-new_data[new_data$`Country/Region`=='China',]
|' L& x6 E) l }: [& C+ _china_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
" p3 P6 b( W8 T9 @, wcolnames(china_increase)<-'increase_patient'
6 Z4 e$ p( L1 Bchina_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")) ]) H$ B2 B4 V
; w \7 m9 U. F
ggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+4 y& m6 d% i/ j$ n) J
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)- f4 _: y1 e8 L8 U
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+; V0 B& \1 z `6 B& i, H
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
H! @' k) [/ P# d7 x/ E( ~ theme(plot.title = element_text(face="plain",size=15,hjust=0.5),) E" x! @4 n& y2 q1 [
axis.title.x = element_blank(),; t0 K3 c. m) V2 {2 Z7 i, ~
axis.title.y = element_text(size=15),
! R0 D2 X2 T. K( g axis.text.x = element_text(angle = 90,size=15)," q, m5 z: N6 T& Z- t5 \
axis.text.y = element_text(size=15),
9 _2 ^. F7 U/ X" [9 T legend.title=element_blank(),- F2 T& r) w' g
legend.text=element_text(size=15)), Z/ Z4 R$ i+ s6 }* g9 o
. B: L" V( w' F9 ^0 i3 x* y ![]()
2 p$ m/ G! H/ ?; u2. 美国新增病例变化趋势8 A1 Q6 H/ }6 {0 ]
us<-new_data[new_data$`Country/Region`=='United States',]
" F$ X C: p5 {/ [* _2 J2 j5 Bus_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07'): L4 g6 U' T9 f- t3 r$ m
us_increase$date<-as.Date(us_increase$date)7 ^# f! q# H r; W: k
ggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
9 @: |! e3 g t, l3 d scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天
" E: q/ u* N R/ s labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+
- S k$ I5 K1 `5 r6 l X* J9 ?" W theme_economist()+ #使用经济学人绘图样(式ggthemes包)
3 i8 A8 D' }) s theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
( q2 |1 l/ f! I0 f axis.title.x = element_blank(),
1 C% @$ ~2 c. [9 P8 r; o n3 k' [ axis.title.y = element_text(size=15),; z& n0 n. \" x9 L+ S l2 E
axis.text.x = element_text(angle = 90,size=15),& _) O* V9 I; B7 E
axis.text.y = element_text(size=15),: M* B1 p0 ^( x8 H. r. h# h
legend.title=element_blank(),( \% O1 e: u; F3 T4 x
legend.text=element_text(size=15)), m" z2 a9 r4 s
; n+ e+ a0 f% T " n2 s8 j, f7 t+ s5 m
3. 全球新增病例变化趋势
' X" w3 n! B5 }total_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))" x8 V9 H# ?; Q) V+ x
colnames(total_increase)<-'increase_patient') e( w( f% |( p/ ]$ ?% H
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d"), {6 B0 S- S( w- p7 T
ggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
7 p+ f7 Y' u# E! ^+ ]) A9 e7 t& D scale_x_date(date_breaks = "14 days")+
: P g) Z; [7 `! j' l( B labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+$ i0 J" j) ] ]8 W0 R( h+ r. M/ O
theme_economist()+8 p1 c0 v8 t" O! z* m
scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签
# g4 I% i2 C) ]0 M* m breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),& m' f5 `& N- i ?
labels=c("0","20万","40万","60万","80万"))+3 ^5 A- j2 o n/ D7 G
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),4 \6 \! U" U) a/ [; v6 J
axis.title.x = element_blank(),
2 q! ^, W9 r A8 G$ }, x0 m7 Q0 D4 d axis.title.y = element_text(size=15),3 C' M! Z7 Y9 @ N) u4 _
axis.text.x = element_text(angle = 90,size=15),' p* Y+ E( y4 a) h! y
axis.text.y = element_text(size=15),
% Y) w. B5 h) _7 z" p. I) Z. Z0 v- J: @ legend.title=element_blank(),7 V* ^) x9 {+ a- L7 x% @
legend.text=element_text(size=15))3 k( k2 k$ ?+ X3 C, g
# m+ R% t0 ]( A( u
![]()
- R: x: w3 ^# a. ^! h( H三、新增确诊病例全球地理分布
3 c* _+ B& g. N9 j! R! `mapworld<-borders("world",colour = "gray50",fill="white") 8 G9 A) u& H% o5 p0 o* r6 O3 S
ggplot()+mapworld+ylim(-60,90)+' B" e5 m+ m* @5 s: R: h) [6 K
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+0 E6 i5 B; ~9 t8 c7 [
scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+
' y. b) n3 i& p- g, |, [$ P theme_grey(base_size = 15)+
+ Y8 I8 _# d) T" Y: n9 d0 t7 C" ?8 W theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
1 ^" ^* U( O! y# s$ H7 V legend.title=element_blank())
+ v8 {8 t3 V# c7 I
& ~) V! d' u2 {/ C3 R: l& Zggplot()+mapworld+ylim(-60,90)+
/ d |$ B4 |, C7 }6 Q) H; H0 Y+ t geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
8 R& p7 f& J$ \* ^; Y scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+) O7 `- ^6 z7 O7 k+ h' Q8 ]
theme_grey(base_size = 15)+
. b0 W/ S: t6 X1 V$ R/ ] g5 G* X0 q theme(plot.title=element_text(face="plain",size=15,hjust=0.5),3 z |/ ]3 ~- y
legend.title=element_blank())4 G2 t3 G# J" y5 U5 x. u/ b
9 w$ Z6 D% W( {7 b& R4 T![]() ) V- {1 `/ O* x7 E; ~! N" e/ C9 b
四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家 # N0 f* k2 g) f1 h; s
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)) ![]()
8 G/ Y9 x$ M- Z- w, k+ [2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图
Z2 I; V) ?" X- [cum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07'); T9 o4 ~, \3 L7 Y9 D R
colnames(cum_patient_time)<-c(" rovince","Country","Lat","Long","date","increase_patient")+ @2 b# c0 ^! }
five_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))
) e& m& K0 j/ r) |five_country$date<-as.Date(five_country$date)
$ _- k( o+ u3 ?0 @/ D
2 _7 Z6 |% Z) [: q. _5 W' z" Vggplot(five_country, & W8 ]* [3 H- ^% J
aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) + 6 ]/ G2 e- |" |. N" x" M6 W% D& a
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) + 8 Q* P3 p4 S3 T. d# [7 x2 Z b: t
geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+ 0 e6 R% X% W/ z
scale_fill_brewer(palette='Set3')+ #使用Set3色系模板$ }% A0 g4 Y% M, l3 y* p
theme(legend.position="none",+ f! k( k; k7 L& c7 H
panel.background=element_rect(fill='transparent'),+ S& F3 |2 `7 l
axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),
- L! y* ]! M4 c" K y: N& s panel.grid =element_blank(), #删除网格线" G" K3 w' E' Z0 j$ H
axis.text = element_blank(), #删除刻度标签) Y! m: j9 e$ T, @' o7 i
axis.ticks = element_blank(), #删除刻度线/ ^4 h; Y6 r. Z2 x) p' I/ v2 D# t5 C' [
)+" n5 U7 I# F4 k4 t& y4 [% n
coord_flip()+
9 H1 u$ s5 W* o% {8 L/ i' b transition_manual(frames=date) + #动态呈现
8 E4 A# s& t y( |& Y labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+ 6 K5 G$ {! x; Y& M Z) q
theme(axis.title.x = element_text(size=15))+: t P; g" O6 c& ^, }- R* \
ease_aes('linear') 4 I6 k8 j( ~1 r/ U: E
/ f P0 i" d8 m q. ?7 panim_save(filename = "五国累计确诊病例增长动态图.gif")2 z1 |2 v4 ~: P! P0 _
3 R: N$ b8 ^. e7 P1 |5 ^1 Q * _8 ^" x. S3 d
4 Y+ s! a; `' n 8 u5 H. U0 ` N$ n" I* z$ v
3 O7 _3 q( I9 w6 ]( [ |
zan
|