image.png熱圖展示不同國家歷屆足球世界杯的成績(jī),非常有意思,時(shí)間跨度是1982年到2018年,入選國家的標(biāo)準(zhǔn)是最少參加過四次世界杯,我們今天來重復(fù)一下這個(gè)圖,自己這個(gè)偽球迷也來了解一下足球世界杯的相關(guān)知識(shí)。
推特上這個(gè)圖還沒有分享示例數(shù)據(jù)和代碼,我們手動(dòng)把數(shù)據(jù)整理下來,代碼自己來寫
部分示例數(shù)據(jù)截圖
image.png最開始整理數(shù)據(jù)是直接按照?qǐng)D中的圖例文字來標(biāo)注的,想了一下用數(shù)字替代可能會(huì)更快一點(diǎn),數(shù)字在讀入R語言后可以用代碼再次替換成圖例的文本
三個(gè)圖的作圖代碼是一樣的,只是需要換一下數(shù)據(jù)就可以了
第一個(gè)圖
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
image.png第二個(gè)圖
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
image.png第三個(gè)圖
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
image.png最后是拼圖
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()
image.png推特上的圖還用點(diǎn)標(biāo)注了每屆世界杯的東道主國家,這個(gè)如何實(shí)現(xiàn)在單獨(dú)出一期推文進(jìn)行介紹
示例數(shù)據(jù)和代碼可以給推文點(diǎn)贊,點(diǎn)擊在看,最后留言獲取
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!