R语言ggplot2做表格,很好看

简介: R语言ggplot2做表格,很好看

代码来自于链接

https://github.com/tashapiro/tanya-data-viz/blob/main/nfl-table/nfl-tables.R

结果

image.png

代码

#install.packages("nflfastR")
library(nflfastR)
library(tidyverse)
#install.packages("ggimage")
library(ggimage)
library(RColorBrewer)
#install.packages("ggtext")
library(ggtext)
library(sysfonts)
library(showtext)

#import font from sysfonts
sysfonts::font_add_google("chivo","chivo")
showtext_auto()


#load data
nfl_stats <- load_player_stats()
nfl_qb <- nfl_stats|>filter(position=="QB" & week==1)
nfl_team<- teams_colors_logos

#reshape data
nfl_plot_data<-nfl_qb|>
  filter(passing_yards>70)|>
  select(headshot_url, player_name, player_display_name, recent_team, completions, attempts, passing_yards, passing_tds, interceptions, sacks, rushing_yards, rushing_tds)|>
  inner_join(teams_colors_logos|>select(team_abbr, team_nick, team_division), by=c("recent_team"="team_abbr"))|>
  separate(team_division, into=c("conference","division"), sep=" ")|>
  mutate(comp_perc = completions/attempts)|>
  arrange(conference, division, -comp_perc, -passing_yards)|>
  group_by(conference, division)|>
  mutate(rank = row_number(), 
         pos = -rank,
         yd_color = case_when(passing_yards>330 | passing_yards<125 ~ "white", TRUE ~"black"))

#create new dataframe with positions for line divisions to create table
nfl_plot_lines<-data.frame(lines = seq(0.5,-4.5))

#table headers
headers<-data.frame(name = c("Player","Team","YDS","CMP %","TD"),
                    pos = c(0, 1.5, 2.8,3.5,4.1),
                    hjust= c(0,0,0.5,0.5,0.5))

#create title with css/html formatting, this will be applied with ggtext::element_textbox_simple in ggplot
title<-"<span style='font-size:18pt;'>**NFL QB Passing Stats**<br></span><span style='font-size:14pt'>Data from nflfastR for Week 1 of 2022 Season. Metrics related to passing performance.<br></span>"


#plot
ggplot(nfl_plot_data)+
  geom_image(mapping=aes(y=pos, image=headshot_url, x=0), size=0.1, asp=1.5)+
  geom_rect(mapping=aes(xmin=2.5, xmax=3.1, ymin=pos-0.5, ymax=pos+0.5, fill=passing_yards), show.legend = FALSE)+
  scale_fill_gradientn(colours=brewer.pal(7,"PuOr"))+
  geom_text(data=headers|>filter(hjust==0), mapping=aes(label=name, x=pos, y=-0.1), hjust=0, fontface="bold")+
  geom_text(data=headers|>filter(hjust==0.5), mapping=aes(label=name, x=pos, y=-0.1), fontface="bold")+
  geom_text(mapping=aes(y=pos, x=0.3, label=player_name), hjust=0)+
  geom_text(mapping=aes(y=pos, x=1.5, label=team_nick), hjust=0)+
  geom_text(mapping=aes(y=pos, x=2.8, label=passing_yards, color=yd_color))+
  scale_color_identity()+
  geom_text(mapping=aes(y=pos+0.1, x=3.5, label=round(comp_perc*100,1)), size=3.2)+
  geom_text(mapping=aes(y=pos-0.2, x=3.5, label=paste0(completions,"/",attempts)), size=2, color="grey30")+
  geom_text(mapping=aes(y=pos, x=4.1, label=passing_tds))+
  geom_hline(data=nfl_plot_lines, mapping=aes(yintercept=lines), color="grey50", size=0.3)+
  labs(title=title, caption="Data: nflfastR | Graphic: @tanya_shapiro")+
  scale_x_continuous(limits=c(0,4.1))+
  scale_y_continuous(limits=c(-5,0.2), expand=c(0,0))+
  facet_grid(conference~toupper(division))+
  theme_minimal()+
  theme(panel.grid = element_blank(),
        plot.title = element_textbox_simple(halign=0.5),
        strip.background.x = element_rect(fill="black"),
        strip.text.x=element_text(face="bold", size=9, color="white"),
        strip.text.y=element_text(face="bold",size=9),
        plot.margin = margin(t=20, b=20, r=10, l=10),
        axis.text = element_blank(),
        axis.title=element_blank())

这里有设置字体的步骤可以省略

他这里的管道符用到的是 |> ,这个应该是R4.1以上才可以用,如果R版本比较低可以用%>%

相关文章
|
1月前
|
数据可视化 数据挖掘 定位技术
R语言读取Excel表格数据并绘制多系列柱状图、条形图
R语言读取Excel表格数据并绘制多系列柱状图、条形图
|
1月前
|
存储 数据可视化 数据挖掘
R语言可视化:ggplot2冲积/桑基图sankey分析大学录取情况、泰坦尼克幸存者数据
R语言可视化:ggplot2冲积/桑基图sankey分析大学录取情况、泰坦尼克幸存者数据
|
1月前
r语言ggplot2误差棒图快速指南
r语言ggplot2误差棒图快速指南
|
1月前
ggplot2如何在R语言中绘制表格
ggplot2如何在R语言中绘制表格
|
1月前
|
数据可视化
R语言ggplot2 对Facebook用户数据可视化分析
R语言ggplot2 对Facebook用户数据可视化分析
|
10月前
|
存储 Go
速绘丨GO富集气泡图绘制方法,利用R语言ggplot2包快速绘制,完整脚本可重复绘图
速绘丨GO富集气泡图绘制方法,利用R语言ggplot2包快速绘制,完整脚本可重复绘图
|
10月前
|
Linux Python
彩蛋丨利用R语言脚本实现批量合并Excel表格,再也不用手动点来点去了!
彩蛋丨利用R语言脚本实现批量合并Excel表格,再也不用手动点来点去了!
|
10月前
|
数据采集 机器学习/深度学习 SQL
绝不可错过!R语言与ggplot2实现SCI论文数据分析神器
绝不可错过!R语言与ggplot2实现SCI论文数据分析神器
168 0
|
数据可视化 数据挖掘 定位技术
跟着Nature Communications学作图:R语言ggplot2画世界地图并用md语法添加文字标签
跟着Nature Communications学作图:R语言ggplot2画世界地图并用md语法添加文字标签
|
数据可视化 数据挖掘 Python
跟着NatureMetabolism学作图:R语言ggplot2转录组差异表达火山图
跟着NatureMetabolism学作图:R语言ggplot2转录组差异表达火山图

热门文章

最新文章