Facet grid in ggplot2 leaves blanks between columns

Viewed 35

I am creating a plot in R with ggplot that utilizes a facet_grid structure. But for some reason, when I make the plot it shows these gaps in between the columns for each individual player:

enter image description here

I want it so that the top-left "grid" shows just the 8 bars for ARI/% Team Receiving and for the top-right grid to show the 3 bars for KC/% Team Receiving, so on and so forth. For reference, here is the code for the dataframe as well as the plot itself:

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"))


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

When I run the code, I also get this error, which I believe to be related to the reorder_within() part of the plot.

enter image description here

Any suggestions on how to remedy this?

1 Answers

I am not sure if it is possible when using ep_type ~ posteam. What you could do is use ep_type~. which will remove the empty bars but only not shows the headers of "posteam" so you could add a legend for that like this:

library(ggplot2)
library(tidytext)
ggplot()+
  geom_col(data = test,
           aes(x = pct_team_ep, y = reorder_within(ep_type, player_name, pct_team_ep), fill = posteam),
           show.legend = TRUE, position = "identity")+
  facet_grid(ep_type ~ ., scales = "free", space = "free", drop = TRUE)+
  theme(axis.text.y = element_blank()) 

Created on 2022-08-16 by the reprex package (v2.0.1)

Related