作为全球最受欢迎的体育运动,自然会吸引全世界无数球迷的目光。本文将对世界杯历史数据进行可视化分析。数据集是FIFA官方数据整理的基础数据表,本文数据集来源于天池。本文将对表WorldCupsSummary(世界杯成绩信息表)进行数据可视化。该数据集包含了1930-2018年共21届世界杯赛事汇总信息,涉及的信息可见“变量介绍”部分。
一 变量介绍
Year:举办年份
HostCountry:举办国家
Winner、Second、Third、Fourth:获奖名次。
GoalsScored:总进球数
QualifiedTeams:总参赛队伍数
MatchesPlayed:总比赛场数
Attendance:现场观众总数
HostContinent:举办国所在洲
WinnerContinent:冠军国家所在洲
二 分析问题
举办世界杯次数最多的国家
举办世界杯次数最多的洲
计算各举办国在所属洲的占比
夺冠次数最多的国家及其所在洲分布
世界杯半决赛结果
经常打入半决赛、决赛的队伍,及其夺冠概率
东道主进入半决赛、决赛、夺冠的概率
历届世界杯总进球数变化趋势
历届世界杯现场观看人数变化趋势、年平均现场观看人数
历届世界杯总参赛队伍变化趋势
历届世界杯总比赛场数变化趋势
三 数据分析
1 导入数据
WorldCupsSummary <- read.csv("WorldCupsSummary.csv",header=TRUE)
2 数据预处理
# 将Germany FR替换为Germany
WorldCupsSummary$Winner[WorldCupsSummary$Winner=="Germany FR"] <- "Germany"
WorldCupsSummary$Second[WorldCupsSummary$Second=="Germany FR"] <- "Germany"
WorldCupsSummary$Third[WorldCupsSummary$Third=="Germany FR"] <- "Germany"
WorldCupsSummary$Fourth[WorldCupsSummary$Fourth=="Germany FR"] <- "Germany"
3 数据分析
3.1 举办世界杯次数最多的国家
f_hostcountry <- data.frame(table(WorldCupsSummary$HostCountry),stringsAsFactors = F)
names(f_hostcountry)[1] <- "HostCountry"
f_hostcountry <- f_hostcountry[order(f_hostcountry$Freq),]
b1 <- WorldCupsSummary[,c(1,2)]
b1 <- merge(f_hostcountry,b1)
b2 <- b1[b1$Freq != 1,]
b3 <- data.frame(HostCountry=character(),stringsAsFactors = F) #创建空白数据框
for (i in unique(b2$HostCountry)) {
k4 <- b2[b2$HostCountry==i,c(1,2,3)]
k4$Year <- paste0(k4$Year[1],"& ",k4$Year[2])
b3 <- rbind(b3,k4[duplicated(k4),])
}
f_hostcountry <- rbind(b1[b1$Freq==1,],b3)
ggplot(f_hostcountry,aes(x=reorder(HostCountry,Freq),y=Freq,fill=HostCountry))+
geom_bar(stat="identity")+
scale_y_continuous(breaks=c(1,2))+
labs(title="举办世界杯次数最多的国家",x="举办国家",y="举办次数")+
theme(panel.grid.major.y=element_blank(),
panel.grid.minor=element_blank(),
plot.title=element_text(hjust=0.5,size=16,face="bold"))+
geom_text(aes(x=HostCountry,y=0.05,label=paste("举办时间:",Year,"年",seq=""),hjust=0))+ #hjust=0左对齐
coord_flip() #交换坐标轴
3.2 举办世界杯次数最多的洲
f_hostContinent <- data.frame(table(WorldCupsSummary$HostContinent))
names(f_hostContinent)[1] <- "HostContinent"
f_hostContinent <- mutate(f_hostContinent,Percentage=round(Freq/sum(Freq)*100,2))
layout(matrix(1:2,1,2,byrow=T),widths=c(1,1.5))
bar2 <- barplot(f_hostContinent$Freq,
col=brewer.pal(4,"Set3"),axes=F,cex.main=1.5,cex.names=0.55,
main="四大洲举办世界杯次数",xlab="四大洲",ylab="举办世界杯次数")
axis(side=2,at=seq(0,13,1),labels=seq(0,13,1),las=1)
axis(side=1,at=seq(0.7,4.3,1.2