可视化实例基于R语言的全球疫情可视化
可视化实例基于R语言的全球疫情可视化目录
一、数据介绍及预处理
二、新增确诊病例变化趋势
三、新增确诊病例全球地理分布
四、累计确诊病例动态变化图
一、数据介绍及预处理
1. 基本字段介绍
字段名 含义
Province/State 省/州
Country/Region 国家/地区
Lat 纬度
Long 经度
1/22/20-12/7/20 每日累计确诊病例
https://img-blog.csdnimg.cn/20201230102502362.JPG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70#pic_center
2. 数据预处理
[*]整理某些国家的名称,如Korea, South改为 Korea
[*]将日期列字段修改为相应的日期格式
[*]#加载本次可视化所需包library(readr) library(sp) #地图可视化library(maps) #地图可视化library(forcats)library(dplyr)library(ggplot2)library(reshape2) library(ggthemes) #ggplot绘图样式包library(tidyr)library(gganimate) #动态图
#一、国家名词整理data<-read_csv('confirmed.csv')data$`Country/Region`='United States'data$`Country/Region`='Korea'
information_data<-data[,1:4] #取出国家信息相关数据inspect_data<-data[,-c(1:4)] #取出确诊人数相关数据
#二、日期转换datetime<-colnames(inspect_data)pastetime<-function(x){ date<-paste0(x,'20') return(date)}datetime1<-as.Date(sapply(datetime,pastetime),format='%m/%d/%Y')colnames(inspect_data)<-datetime1
#合并数据,data为累计确诊人数数据(预处理后)data<-cbind(information_data,inspect_data)二、新增确诊病例变化趋势#由累计确诊病例计算新增确诊病例
inspect_lag_data<-cbind(0,inspect_data[,1:(ncol(inspect_data)-1)])
increase_data<-inspect_data-inspect_lag_data
#合并数据,new_data为新增确诊人数数据
new_data<-cbind(information_data,increase_data)
1. 中国新增确诊病例变化趋势
#合并所有省份新增确诊人数
china<-new_data
china_increase<-data.frame(apply(china[,-c(1:4)],2,sum))
colnames(china_increase)<-'increase_patient'
china_increase$date<-as.Date(rownames(china_increase),format="%Y-%m-%d")
ggplot(china_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天(注意:此时的date列必须为日期格式!)
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日中国新增确诊人数变化趋势图')+
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
axis.title.x = element_blank(),
axis.title.y = element_text(size=15),
axis.text.x = element_text(angle = 90,size=15),
axis.text.y = element_text(size=15),
legend.title=element_blank(),
legend.text=element_text(size=15))
https://img-blog.csdnimg.cn/20201230110050745.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70#pic_center
2. 美国新增病例变化趋势
us<-new_data
us_increase<-gather(us,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
us_increase$date<-as.Date(us_increase$date)
ggplot(us_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
scale_x_date(date_breaks = "14 days")+ #设置横轴日期间隔为14天
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日美国新增确诊人数变化趋势图')+
theme_economist()+ #使用经济学人绘图样(式ggthemes包)
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
axis.title.x = element_blank(),
axis.title.y = element_text(size=15),
axis.text.x = element_text(angle = 90,size=15),
axis.text.y = element_text(size=15),
legend.title=element_blank(),
legend.text=element_text(size=15))
https://img-blog.csdnimg.cn/20201230110555816.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70#pic_center
3. 全球新增病例变化趋势
total_increase<-data.frame(apply(new_data[,-c(1:4)],2,sum))
colnames(total_increase)<-'increase_patient'
total_increase$date<-as.Date(rownames(total_increase),format="%Y-%m-%d")
ggplot(total_increase,aes(x=date,y=increase_patient,color='新增确诊人数'))+geom_line(size=1)+
scale_x_date(date_breaks = "14 days")+
labs(x='日期',y='新增确诊人数',title='2020年1月22日-2020年12月7日全球新增确诊人数变化趋势图')+
theme_economist()+
scale_y_continuous(limits=c(0,8*10^5), #考虑数字过大,以文本形式标注y轴标签
breaks=c(0,2*10^5,4*10^5,6*10^5,8*10^5),
labels=c("0","20万","40万","60万","80万"))+
theme(plot.title = element_text(face="plain",size=15,hjust=0.5),
axis.title.x = element_blank(),
axis.title.y = element_text(size=15),
axis.text.x = element_text(angle = 90,size=15),
axis.text.y = element_text(size=15),
legend.title=element_blank(),
legend.text=element_text(size=15))
https://img-blog.csdnimg.cn/20201230110840159.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70#pic_center
三、新增确诊病例全球地理分布
mapworld<-borders("world",colour = "gray50",fill="white")
ggplot()+mapworld+ylim(-60,90)+
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-01-22`),color="darkorange")+
scale_size(range=c(2,9))+labs(title="2020年1月22日全球新增确诊人数分布")+
theme_grey(base_size = 15)+
theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
legend.title=element_blank())
ggplot()+mapworld+ylim(-60,90)+
geom_point(aes(x=new_data$Long,y=new_data$Lat,size=new_data$`2020-11-22`),color="darkorange")+
scale_size(range=c(2,9))+labs(title="2020年11月22日全球新增确诊人数分布")+
theme_grey(base_size = 15)+
theme(plot.title=element_text(face="plain",size=15,hjust=0.5),
legend.title=element_blank())
https://img-blog.csdnimg.cn/20201230111745832.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70https://img-blog.csdnimg.cn/2020123011182045.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70
四、累计确诊病例动态变化图1. 至12月7日全球累计病例确诊人数前十国家
cum_patient<-datacum_patient<-cum_patientcolnames(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))https://img-blog.csdnimg.cn/20201230112356748.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5lNDg2OQ==,size_16,color_FFFFFF,t_70#pic_center
2. 五国(India、Brazil、Russia、Spain、Italy)累计确诊病例动态变化图
cum_patient_time<-gather(data,key="date",value="increase_patient",'2020-01-22':'2020-12-07')
colnames(cum_patient_time)<-c("Province","Country","Lat","Long","date","increase_patient")
five_country<-subset(cum_patient_time,Country %in% c("India","Brazil","Russia","Spain","Italy"))
five_country$date<-as.Date(five_country$date)
ggplot(five_country,
aes(x=reorder(Country,increase_patient),y=increase_patient, fill=Country,frame=date)) +
geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +
geom_text(aes(label=paste0(increase_patient)),col="black",hjust=-0.2)+
scale_fill_brewer(palette='Set3')+ #使用Set3色系模板
theme(legend.position="none",
panel.background=element_rect(fill='transparent'),
axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1),
panel.grid =element_blank(), #删除网格线
axis.text = element_blank(), #删除刻度标签
axis.ticks = element_blank(), #删除刻度线
)+
coord_flip()+
transition_manual(frames=date) + #动态呈现
labs(title = paste('日期:', '{current_frame}'),x = '', y ='五国累计确诊病例增长')+
theme(axis.title.x = element_text(size=15))+
ease_aes('linear')
anim_save(filename = "五国累计确诊病例增长动态图.gif")
https://img-blog.csdnimg.cn/2020123011315173.gif#pic_center
页:
[1]