Change the order of fill on geom_col for position = "dodge"

I have the following chart that I've created with a geom_col with position = "dodge":

What I'm trying to get is to have the "Garbage Time (Positive)" to appear the furthest left, "Positive" to be next, so on and so forth. I got the legend into the order that I want, but can't figure out a way to have that order reflected on the chart itself. For reference, here's the existing code for the chart:

ggplot() +
  geom_col(data = all_stats_total, aes(x = rusher_position, y = fpts_pp, fill = game_script),
           position = "dodge") +
  scale_fill_discrete(limits = c("positive_garbage", "positive", "neutral", "negative", "negative_garbage"), labels = c("Garbage Time (Positive)", "Positive", "Neutral",  "Negative", "Garbage Time (Negative)")) +
  labs(y = "Fantasy Points Per Play",
       title = "Passing-related positions (QB, WR, TE) perform more favorably in\npositive game scripts than negative game sripts. The impact on running\nbacks, by game script, is negligible.",
       caption = "Figure: @SamHoppen | Data: @nflfastR",
       fill = "Game Script")+
  theme(axis.title.x = element_blank())

Any suggestions on how to make this happen?

Edit: Here's a sample of the data.

rusher_position	game_script	        fpts_pp
QB	            negative	        0.2725492
QB	            neutral 	        0.3949173
QB	            positive	        0.6983354
QB              negative_garbage    0.2456543
QB              positive_garbage    0.9324257
RB	            negative	        0.6726941
RB	            neutral 	        0.6541099
RB	            positive	        0.8742239
RB              negative_garbage    0.2456543
RB              positive_garbage    0.9324254

Is game_script a factor? What order are the levels?

Just posted an edit with sample data included.

the sample doesn't answer my question...

Sorry, game_script is not a factor, it is a character. The order that they're appearing on the chart seems to be alphabetical based on the character name, but the order I want them in is custom. Hope that helps!

Thats great, so I believe that if you made them factors and specified the levels with your desired order, they would plot that way rather than the default alphabetic that chars would be

1 Like

Perfect! That did the trick! Thanks so much!

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