You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2中facet_grid分栏间出现空白的解决问询

问题解决方法

核心问题分析

你的代码存在两个主要问题:

  1. Y轴映射错误:将y映射到了分面变量ep_type,导致同一分面内所有条形堆叠在同一Y位置,出现重叠和空白。
  2. reorder_within使用不当:函数参数错误,且未配合scale_y_reordered()使用,引发报错。

修正方案(使用forcats手动排序)

通过预处理数据,在每个ep_type和posteam分组内对球员名称按pct_team_ep排序,再绘制图表:

library(ggplot2)
library(dplyr)
library(forcats)

test <- structure(list(posteam = c("ARI", "ARI", "ARI", "ARI", "ARI", 
"KC", "KC", "ARI", "KC", "ARI", "ARI", "ARI", "ARI", "KC", "ARI", 
"KC", "KC", "ARI"), player_name = c("A. Green", "A. Wesley", 
"C. Edmonds", "C. Edmonds", "C. Kirk", "C. Edwards-Helaire", 
"D. Williams", "D. Hopkins", "D. Gore", "E. Benjamin", "J. Conner", 
"K. Murray", "M. Williams", "M. Hardman", "R. Moore", "T. Kelce", 
"T. Hill", "Z. Ertz"), ep_type = c("% Team Receiving Expected Fantasy Points", 
"% Team Receiving Expected Fantasy Points", "% Team Receiving Expected Fantasy Points", 
"% Team Rushing Expected Fantasy Points", "% Team Receiving Expected Fantasy Points", 
"% Team Rushing Expected Fantasy Points", "% Team Rushing Expected Fantasy Points", 
"% Team Receiving Expected Fantasy Points", "% Team Rushing Expected Fantasy Points", 
"% Team Rushing Expected Fantasy Points", "% Team Rushing Expected Fantasy Points", 
"% Team Rushing Expected Fantasy Points", "% Team Receiving Expected Fantasy Points", 
"% Team Receiving Expected Fantasy Points", "% Team Receiving Expected Fantasy Points", 
"% Team Receiving Expected Fantasy Points", "% Team Receiving Expected Fantasy Points", 
"% Team Receiving Expected Fantasy Points"), pct_team_ep = c(0.182936486668621, 
0.1274987856876, 0.110899393183845, 0.364654483810259, 0.185969658295152, 
0.484933781664591, 0.436542306199132, 0.232476349208523, 0.263790577219152, 
0.108924993390151, 0.552044927156214, 0.14942611850254, 0.102823738401866, 
0.130902414601717, 0.123696799895897, 0.214270739393065, 0.259129290426641, 
0.219092993255652)), row.names = c(NA, -18L), class = c("tbl_df", 
"tbl", "data.frame"))

# 按分组对球员名称排序
test_ordered <- test %>%
  group_by(ep_type, posteam) %>%
  mutate(player_ordered = fct_reorder(player_name, pct_team_ep)) %>%
  ungroup()

# 绘制图表
ggplot(test_ordered, aes(x = pct_team_ep, y = player_ordered, fill = posteam)) +
  geom_col(show.legend = FALSE, position = "identity") +
  facet_grid(ep_type ~ posteam, scales = "free_y") +
  theme(axis.text.y = element_blank())

关键修正点说明

  • Y轴映射修正:将y改为排序后的球员名称player_ordered,确保每个条形占据独立Y位置。
  • 分组排序:使用fct_reorder在每个分面分组内按数值排序,实现条形从低到高排列。
  • 自由Y轴刻度:scales = "free_y"让每个分面仅显示对应分组的球员,消除空白区域。

备选方案(使用tidytext的reorder_within)

若坚持使用reorder_within,需确保参数正确并配合scale_y_reordered():

library(ggplot2)
library(tidytext)

ggplot(test, aes(x = pct_team_ep, y = reorder_within(player_name, pct_team_ep, interaction(ep_type, posteam)), fill = posteam)) +
  geom_col(show.legend = FALSE, position = "identity") +
  facet_grid(ep_type ~ posteam, scales = "free_y") +
  scale_y_reordered() +
  theme(axis.text.y = element_blank())

内容的提问来源于stack exchange,提问作者Sam Hoppen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 20:27:30