-1

I have a faceted plot wherein I'd like to have the Y-axis labels and the associated values appear in descending order of values (and thereby changing the order of the labels) for each facet. What I have is this, but the order of the labels (and the corresponding values) is the same for each facet.

ggplot(rf,
       aes(x = revenues, 
           y = reorder(AgencyName, revenues))) +
  geom_point(stat = "identity", 
           aes(color = AgencyName),
           show.legend = FALSE) +
  
  xlab(NULL) + 
  ylab(NULL) + 
  scale_x_continuous(label = scales::comma) +
  facet_wrap(~year, ncol = 3, scales = "free_y") + 
  theme_minimal()

Can someone point me to the solution?

Charles Knell
  • 117
  • 1
  • 9
  • 2
    It's most helpful if you can provide some of your data via the output of `dput()`. You can provide just some of the data by first using `head`. So, `dput(head(rf, 20))` – AndrewGB May 06 '22 at 22:08
  • Sorry, the data is proprietary. As much as I'd like to, I can't. Maybe I'll take an hour or so to dummy a data set and post that, if you think that will enable you to give an answer. The revenue rank per customer appears correctly for the first facet, but when the relative order shifts from facet to facet when the revenue changes, the order of the customer names stays the same and doesn't adjust to reflect the changed relative values. – Charles Knell May 07 '22 at 01:35
  • Yes, generally providing some data will lead to quicker and better answers. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example You could even just take a small subset of your data and just alter it so that it isn’t the original data. – AndrewGB May 07 '22 at 01:39

1 Answers1

2

The functions reorder_within and scale_*_reordered from the tidytext package might come in handy.

reorder_within recodes the values into a factor with strings in the form of "VARIABLE___WITHIN". This factor is ordered by the values in each group of WITHIN. scale_*_reordered removes the "___WITHIN" suffix when plotting the axis labels. Add scales = "free_y" in facet_wrap to make it work as expected.

Here is an example with generated data:

library(tidyverse)

# Generate data
df <- expand.grid(
  year = 2019:2021,
  group = paste("Group", toupper(letters[1:8]))
)
set.seed(123)
df$value <- rnorm(nrow(df), mean = 10, sd = 2)

df %>% 
  mutate(group = tidytext::reorder_within(group, value, within = year)) %>% 
  ggplot(aes(value, group)) +
  geom_point() +
  tidytext::scale_y_reordered() +
  facet_wrap(vars(year), scales = "free_y")
ansgar
  • 96
  • 3
  • 1
    This is great! Thanks. I have one last issue to deal with. Adapting your code, I have the ordering I wanted, but when I add the "color = group" parameter to the aes() function in either the ggplot() function or the geom_point() function, the colors for each group are not consistent from facet to facet. Any thoughts? – Charles Knell May 07 '22 at 22:03
  • Since the underlying values of the factor created with `reorder_within` are different, the default coloring will lead to different colors for the "same" values. Instead of overwritting the original values from `group` (as I did in my example), you could generate a new column (e.g. `group2`) and then use `ggplot(aes(value, group2)` and `geom_point(aes(color = group))` in the plot. – ansgar May 08 '22 at 09:08
  • Thanks again. You have saved me possibly days of Googling and experimenting. – Charles Knell May 08 '22 at 16:21