大数跨境
0
0

ggplot2绘制120年间奥运会各项目参赛情况

ggplot2绘制120年间奥运会各项目参赛情况 R语言数据分析指南
2021-08-03
1
导读:本节继续使用 TidyTuesday 第31周的项目数据集来绘制1920-2016120年间奥运会各项目的参

本节继续使用 TidyTuesday 第31周的项目数据集来绘制1920-2016120年间奥运会各项目的参赛情况,后台回复20210803获取本文数据及代码;喜欢的小伙伴欢迎扫描文末二维码加入我的交流群更多精彩内容为您而准备

加载R包

library(tidyverse)
library(ggthemes)
library(wesanderson)

加载数据

olympics <- read_csv("olympics.csv")

数据清洗

sport_countries <- olympics %>% 
  filter(season == "Summer") %>% 
  group_by(year, sport) %>% 
  summarise(countries_unique = list(unique(noc))) %>% 
  rowwise() %>% 
  mutate(countries_count = length(countries_unique)) %>% 
  ungroup()

fct_sport <- sport_countries %>% 
  count(sport, sort = TRUE) %>% 
  mutate(sport = fct_reorder(sport, n)) %>% 
  pull(sport)

数据可视化

sport_countries %>% 
  filter(year != 1906) %>% 
  mutate(sport = factor(sport, levels = levels(fct_sport))) %>%
  ggplot(aes(year, sport, fill = countries_count)) +
  geom_tile(size = 0.75, color = "#F0F0F0") +
  annotate("rect", xmin = 1914,xmax = 1918.25,
           ymin = -Inf, ymax = Inf, fill = "#D2D2D2", alpha = 0.6) +
  annotate("text", x = 1916.125, y = 26.5,
           label = "WORLD WAR I", angle = 90, color = "grey40",
           fontface = "bold", size = 6) +
  annotate("rect", xmin = 1938, xmax = 1946.25, ymin = -Inf,
           ymax = Inf, fill = "#D2D2D2", alpha = 0.6) +
  annotate("text", x = 1942.125, y = 26.5, label = "WORLD WAR II",
           angle = 90, color = "grey40",fontface = "bold", size = 6) +
  scale_x_continuous(breaks = seq(1896, 2016, 8),
expand = expansion(mult = 0.02)) +
  scale_fill_gradientn(colours = wes_palette("Zissou1",
n = 50, type = "continuous")) +
  theme_fivethirtyeight(base_size = 16, base_family = "Poppins") +
  theme(
    plot.title.position = "plot",
    plot.caption.position = "plot",
    legend.position = "top",
    legend.justification = c(0.35, 0.8),
    legend.key.width = unit(20, "mm"),
    legend.key.height = unit(4, "mm"),
    panel.grid.minor.x = element_line(color = "#D2D2D2"))


关注下方公众号,获取更多优质资源,如需要加入微信交流群,请在菜单栏处添加作者微信,即可邀您进群

【声明】内容源于网络
0
0
R语言数据分析指南
R语言重症爱好者,喜欢绘制各种精美的图表,喜欢的小伙伴可以关注我,跟我一起学习
内容 1180
粉丝 0
R语言数据分析指南 R语言重症爱好者,喜欢绘制各种精美的图表,喜欢的小伙伴可以关注我,跟我一起学习
总阅读222
粉丝0
内容1.2k