在twitter上看见有人分享了一个图
热图展示不同国家历届足球世界杯的成绩,非常有意思,时间跨度是1982年到2018年,入选国家的标准是最少参加过四次世界杯,我们今天来重复一下这个图,自己这个伪球迷也来了解一下足球世界杯的相关知识。
推特上这个图还没有分享示例数据和代码,我们手动把数据整理下来,代码自己来写
部分示例数据截图
最开始整理数据是直接按照图中的图例来标注的,想了一下用数字替代可能会更快一点,数字在读入R语言后可以用代码再次替换成图例的文本
三个图的作图代码是一样的,只是需要换一下数据就可以了
第一个图
library(readxl)
library(ggplot2)
library(tidyverse)
dat01<-read_excel("data/20221122/fifaworldcup.xlsx",
sheet = "Sheet2")
dat01 %>%
pivot_longer(!country,names_to = "year") %>%
mutate(`Best Achievement`=case_when(
value == 1 ~ 'Not Present',
value == 2 ~ 'Group Stage',
value == 3 ~ 'Round of 16',
value == 4 ~ 'Quarter Finals',
value == 5 ~ 'Semi Finals',
value == 6 ~ 'Winner',
TRUE ~ value
)) -> new.dat01
new.dat01 <- new.dat01 %>%
mutate(country=factor(country,
levels = c("Germany","Spain","Italy",
"England","France",
"Belgium","Netherlands",
"Portugal","Croatia",
"Denmark","Poland","Sweden",
"Switzerland","Russia","Scotland")))
ggplot()+
geom_tile(data=new.dat01,
aes(y=year,x=country,fill=`Best Achievement`),
color="white")+
theme_classic()+
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 60,hjust=0,vjust=0.5),
legend.position = "bottom")+
guides(fill=guide_legend(title.position = "top",byrow = TRUE))+
labs(x=NULL,y=NULL)+
scale_x_discrete(position = "top")+
scale_fill_manual(values = c('Not Present'='#e5e5e5',
'Group Stage'='#440053',
'Round of 16'='#3c528b',
'Quarter Finals'='#218f8c',
'Semi Finals'='#5dc763',
'Winner'='#fde624'))+
ggtitle("Europe")+
coord_equal() -> p1
p1
第二个图
dat02<-read_excel("data/20221122/fifaworldcup.xlsx",
sheet = "Sheet3")
dat02 %>%
pivot_longer(!country,names_to = "year") %>%
mutate(`Best Achievement`=case_when(
value == 1 ~ 'Not Present',
value == 2 ~ 'Group Stage',
value == 3 ~ 'Round of 16',
value == 4 ~ 'Quarter Finals',
value == 5 ~ 'Semi Finals',
value == 6 ~ 'Winner'
)) -> new.dat02
new.dat02 <- new.dat02 %>%
mutate(country=factor(country,
levels = c("Brazi","Argentina","Mexico",
"United States","Uruguay",
"Colombia","Costa Rica",
"Paraguay","Chile")
))
ggplot()+
geom_tile(data=new.dat02,
aes(y=year,x=country,fill=`Best Achievement`),
color="white")+
theme_classic()+
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 60,hjust=0,vjust=0.5),
legend.position = "bottom")+
guides(fill=guide_legend(title.position = "top",byrow = TRUE))+
labs(x=NULL,y=NULL)+
scale_x_discrete(position = "top")+
scale_fill_manual(values = c('Not Present'='#e5e5e5',
'Group Stage'='#440053',
'Round of 16'='#3c528b',
'Quarter Finals'='#218f8c',
'Semi Finals'='#5dc763',
'Winner'='#fde624'))+
ggtitle("Americas")+
coord_equal() -> p2
p2
第三个图
dat03<-read_excel("data/20221122/fifaworldcup.xlsx",
sheet = "Sheet4")
dat03 %>%
pivot_longer(!country,names_to = "year") %>%
mutate(`Best Achievement`=case_when(
value == 1 ~ 'Not Present',
value == 2 ~ 'Group Stage',
value == 3 ~ 'Round of 16',
value == 4 ~ 'Quarter Finals',
value == 5 ~ 'Semi Finals',
value == 6 ~ 'Winner'
)) -> new.dat03
new.dat03 <- new.dat03 %>%
mutate(country=factor(country,
levels = c("South Korea","Cameroon",
"Japan","Nigeria","Saudi Arabia",
"Algeria","Iran",
"Morocco","Australia","Tunisia")
))
ggplot()+
geom_tile(data=new.dat03,
aes(y=year,x=country,fill=`Best Achievement`),
color="white")+
theme_classic()+
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 60,hjust=0,vjust=0.5),
legend.position = "bottom")+
guides(fill=guide_legend(title.position = "top",byrow = TRUE))+
labs(x=NULL,y=NULL)+
scale_x_discrete(position = "top")+
scale_fill_manual(values = c('Not Present'='#e5e5e5',
'Group Stage'='#440053',
'Round of 16'='#3c528b',
'Quarter Finals'='#218f8c',
'Semi Finals'='#5dc763',
'Winner'='#fde624'))+
ggtitle("Other")+
coord_equal() -> p3
p3
最后是拼图
library(patchwork)
pdf(file = "worldcup1982-2018.pdf",
width = 9.4,height = 4,family = "serif")
p1+p2+theme(axis.text.y = element_blank())+
p3+theme(axis.text.y = element_blank())+
plot_layout(guides="collect")+
plot_annotation(theme = theme(legend.position = "bottom"))
dev.off()
推特上的图还用点标注了每届世界杯的东道主国家,这个如何实现在单独出一期推文进行介绍
示例数据和代码可以给推文点赞,点击在看,最后留言获取
欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!