0

Hi Stack Overflow Community! I am attempting to reorder the items in the attached legend to a specific order that I would like to input. The order is: Distal, rTub, Mid, Prox.

I have tried the order function and scale_y_discrete(). scale_y_discrete() didn't work because the y axis is graphing a count frequency, and the data is stored as the text seen in the legend. The code I'm using to output the graph is below

p <- Scaphoid_dataF %>%
  ggplot( aes(x=Age, fill = FxLoc, order = )) +
  geom_histogram() +
  labs(y = "Count of Fractures", x = "Patient Age", title = "Frequency of Fractures vs Age")
p + theme_bw() + scale_fill_grey()

Graph output

enter image description here

mnist
  • 6,571
  • 1
  • 18
  • 41
  • Set the appropriate factor levels? E.g. https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph?rq=1. We can't reproduce your plot. – Axeman May 04 '20 at 05:29
  • Does this answer your question? [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – KoenV May 04 '20 at 05:42

1 Answers1

3

Set the factor orders:

Scaphoid_dataF$Age <- factor(Scaphoid_dataF$Age,
                        levels = c("Distal", "rTub", "Mid", "Prox"),
                        ordered = TRUE)
linog
  • 5,786
  • 3
  • 14
  • 28