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.