Error with ordering on facet_wrap geom_col

I'm using a facet_wrap combined with a geom_col, but the ordering isn't how I'd like it to appear - below is the graph as it looks now and you can clearly see the ordering in the top section is not correct:

Unfortunately, I've tried several things to fix this, but can't figure it out. Here's the code I'm using for the table as well as the data:


library(tidyverse)
library(dplyr)
library(ggimage)
library(ggrepel)
library(nflfastR)
library(magick)
library(cowplot)
library(showtext)
library(jsonlite)
library(curl)
library(rsvg)
library(ggsci)
library(ggridges)
library(ggthemes)
library(shadowtext)


font_add_google("Encode Sans Condensed", "encode", regular.wt = 400, bold.wt = 600)
font_add_google("Inconsolata", "incon")
showtext_auto()

ggplot(data = arizona,aes(x = pct_team_ep, y = reorder(player_name, rank), fill = Team))+
  geom_col(show.legend = FALSE, position = "identity")+
  geom_shadowtext(aes(x = pct_team_ep + 0.02, color = Team, label = player_name), typeface = "bold", hjust = 0, bg.color = 'white', family = "incon", size = 3.5, na.rm = T, bg.r = 0.2, show.legend = FALSE) +
  scale_x_continuous(labels = scales::percent_format(accuracy=1),
                     expand = expansion(mult = 0), limits = c(0,1),
                     name = "",
                     breaks = scales::pretty_breaks(5)) +
  scale_fill_manual(values = NFL_pri) +
  scale_color_manual(values = NFL_sec) +
  facet_wrap( ~ ep_type, ncol = 1, scales = "free") +
  theme_bw()+
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.ticks.x = element_line(size = 0.5),
    strip.background = element_rect(
      fill= "white", size=1, linetype="solid"),
    strip.text.x = element_text(
      size = 12, color = arizona$team_color, family = "encode", face = "bold"
    ))
Team player_name team_color ep_type pct_team_ep rank
TB B. Gabbert #d50a0a % Team Rushing Expected Fantasy Points 0.05803571 4
TB C. Godwin #d50a0a % Team Receiving Expected Fantasy Points 0.22020287 14
TB J. Watson #d50a0a % Team Receiving Expected Fantasy Points 0.05592323 3
TB K. Vaughn #d50a0a % Team Receiving Expected Fantasy Points 0.05365854 2
TB K. Vaughn #d50a0a % Team Rushing Expected Fantasy Points 0.11612903 9
TB L. Fournette #d50a0a % Team Receiving Expected Fantasy Points 0.07182683 6
TB L. Fournette #d50a0a % Team Rushing Expected Fantasy Points 0.31750339 15
TB L. McCoy #d50a0a % Team Receiving Expected Fantasy Points 0.06256446 5
TB M. Evans #d50a0a % Team Receiving Expected Fantasy Points 0.16322217 13
TB O. Howard #d50a0a % Team Receiving Expected Fantasy Points 0.12724307 11
TB R. Gronkowski #d50a0a % Team Receiving Expected Fantasy Points 0.15147957 12
TB R. Jones #d50a0a % Team Receiving Expected Fantasy Points 0.09816815 7
TB R. Jones #d50a0a % Team Rushing Expected Fantasy Points 0.57880435 16
TB S. Miller #d50a0a % Team Receiving Expected Fantasy Points 0.11648661 10
TB T. Brady #d50a0a % Team Rushing Expected Fantasy Points 0.11413043 8
TB T. Johnson #d50a0a % Team Receiving Expected Fantasy Points 0.05351488 1

Any suggestions on what to do? Let me know and thanks in advance!

you can try factor reorder from forcats
when you share data you might want to share it in a format that can be copied out for others to test. usually using dput(data) / dput(arizona) in your case.

library(forcats)

arizona %>%
  mutate(player_name = fct_reorder(player_name, rank)) %>%
  ggplot(...)

You can use reorder_within() from the tidytext package

library(ggplot2)
library(tidytext)

arizona <- data.frame(
  stringsAsFactors = FALSE,
              Team = c("B.","C.","J.","K.","K.",
                       "L.","L.","L.","M.","O.","R.","R.","R.","S.","T.",
                       "T."),
       player_name = c("Gabbert","Godwin","Watson",
                       "Vaughn","Vaughn","Fournette","Fournette","McCoy",
                       "Evans","Howard","Gronkowski","Jones","Jones","Miller",
                       "Brady","Johnson"),
        team_color = c("#d50a0a","#d50a0a","#d50a0a",
                       "#d50a0a","#d50a0a","#d50a0a","#d50a0a","#d50a0a",
                       "#d50a0a","#d50a0a","#d50a0a","#d50a0a","#d50a0a",
                       "#d50a0a","#d50a0a","#d50a0a"),
           ep_type = c("% Team Rushing Expected Fantasy Points","% 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 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 Rushing Expected Fantasy Points",
                       "% Team Receiving Expected Fantasy Points","% Team Rushing Expected Fantasy Points",
                       "% Team Receiving Expected Fantasy Points"),
       pct_team_ep = c(0.05803571,0.22020287,
                       0.05592323,0.05365854,0.11612903,0.07182683,0.31750339,
                       0.06256446,0.16322217,0.12724307,0.15147957,0.09816815,
                       0.57880435,0.11648661,0.11413043,0.05351488),
              rank = c(4, 14, 3, 2, 9, 6, 15, 5, 13, 11, 12, 7, 16, 10, 8, 1)
)

ggplot(data = arizona, aes(x = pct_team_ep,
                           y = reorder_within(player_name, rank, ep_type),
                           fill = Team)) +
    geom_col(show.legend = FALSE) +
    scale_x_continuous(labels = scales::percent_format(accuracy=1),
                       expand = expansion(mult = 0), limits = c(0,1),
                       name = "",
                       breaks = scales::pretty_breaks(5)) +
    scale_y_reordered() +
    facet_wrap( ~ ep_type, ncol = 1, scales = "free")

Created on 2020-10-29 by the reprex package (v0.3.0.9001)

Note: Next time please post a proper REPRoducible EXample (reprex) illustrating your issue as explained on this link.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thanks, I'll be sure to do that in the future! Unfortunately, the fct_reorder didn't work for me. Any other solutions you can think of?