0

I am trying to show a simple histogram by cereal producer broken down by whether or not promos were used. However, my third set of columns is out of order with the other two (the "promo" and "no promo" bars are swapped). As you will see in the image, the "Post" section is flipped compared to the "Kellogg's" and "General Mills" section. I am not concerned with the asc/desc order of the bars.

Here is my code that is producing this chart:

y_values <- c(0, 100, 200, 300, 400)

total_revenue <- sales_product %>% 
  group_by(promo, producer) %>%
  summarise(revenue = sum(sell_amount)) %>% 
  ggplot(aes(x = producer, y = revenue, fill = promo)) +
  geom_col_interactive(position = "dodge", aes(tooltip = paste0('Rev: ', label_dollar()(round(revenue))))) +
  theme_classic() +
  xlab("") + 
  ylab("Total Revenue") +
  scale_y_continuous(labels = paste0("$", y_values, "K"),
                     breaks = seq(0 , 400000, by=100000)) +
  scale_fill_manual("", 
                    labels = c("Promo", "No Promo"), 
                    values = c("#0655A3", "#787878")) +
  scale_x_discrete(labels = c("General Mills", "Kellogg's", "Post")) 

y <- ggiraph(ggobj = total_revenue, height = 3/1)

y

This is a similar data set to the original though it will not create a chart identical to my example, it should be similar.:

   promo sell_amount      producer
1      0        2.50 POST
2      0        1.00 POST
3      0        1.50 POST
4      1        5.00 POST
5      1        5.00 POST
6      0        2.50 POST
7      0        0.50 POST
8      0        3.60 POST
9      0        4.00 POST
10     0        7.00 POST
11     1        2.00 KELLOGG'S
12     1        1.00 KELLOGG'S
13     1        1.50 KELLOGG'S
14     0        1.59 KELLOGG'S
15     0        6.36 KELLOGG'S
16     0       27.55 KELLOGG'S
17     1       10.60 KELLOGG'S
18     0       14.00 KELLOGG'S
19     1       15.95 KELLOGG'S
20     0        9.03 KELLOGG'S
21     1        9.00 KELLOGG'S
22     0        5.45 KELLOGG'S
23     0        2.00 KELLOGG'S
24     0        6.54 KELLOGG'S
25     0        3.27 KELLOGG'S
26     0        5.96 KELLOGG'S
27     0        3.18 KELLOGG'S
28     1        9.90 KELLOGG'S
29     0       25.07 KELLOGG'S
30     0       14.50 KELLOGG'S
31     0       10.15 KELLOGG'S
32     0       18.00 KELLOGG'S
33     0       12.00 KELLOGG'S
34     0        2.00 KELLOGG'S
35     0        5.00 KELLOGG'S
36     1        6.00 GENERAL MILLS
37     0       10.90 GENERAL MILLS
38     0        6.54 GENERAL MILLS
39     1       24.75 GENERAL MILLS
40     0        6.54 GENERAL MILLS
41     0        3.75 GENERAL MILLS
42     0       21.84 GENERAL MILLS
43     0        7.63 GENERAL MILLS
44     0        2.58 GENERAL MILLS
45     0        6.00 GENERAL MILLS
46     0        3.18 GENERAL MILLS
47     0        6.00 GENERAL MILLS
48     0       10.90 GENERAL MILLS
49     0        2.50 GENERAL MILLS
50     0        5.00 GENERAL MILLS

This is what it's currently producing:

Columns Out of Order


EDIT from Jon Spring:

Here is a minimal reprex. Note that the dodge order is inconsistent when we use ggiraph::geom_col_interactive. Normal geom_col works fine, so this might be a bug with ggiraph.

library(ggiraph)
library(ggplot2); library(scales)

plot_data <- structure(list(promo = c(0L, 0L, 0L, 1L, 1L, 1L), 
    producer = c("GENERAL MILLS", "KELLOGG'S", "POST", "GENERAL MILLS", "KELLOGG'S", "POST"), 
    revenue = c(93.36, 171.65, 22.6, 30.75, 49.95, 10)), row.names = c(NA, -6L), 
    class = c("tbl_df", "tbl", "data.frame")) 

This works as expected:

ggplot(plot_data, aes(x = producer, y = revenue, fill = promo)) +
  geom_col(position = "dodge2")

enter image description here

But this version using ggiraph::geom_col_interactive has the dodge order inconsistent between categories.

total_revenue_plot <- 
  ggplot(plot_data, aes(x = producer, y = revenue, fill = promo)) + 
  geom_col_interactive(position = "dodge2").
  
ggiraph(ggobj = total_revenue, height = 3/1)

enter image description here

qatvinc
  • 3
  • 2
  • 2
    Can you please include sample data we can use for `sales_product`? This is often easiest by including the output you get from running `dput(sales_product)` -- that will produce R code that generates an identical object. – Jon Spring Aug 12 '21 at 23:13
  • Thank you @JonSpring. My data is added in. – qatvinc Aug 12 '21 at 23:53
  • If all you want to do is change the plotting order of the bars, then this question has been asked multiple times in SO. The answer involves setting 'labels' argument in the `factor` function. If you think the association between store identities and calculated heights of the bars is wrong, then you need to state that more clearly. – IRTFM Aug 13 '21 at 00:34
  • Thank you for the suggestion @IRTFM. I am not concerned with whether my bar are plotting in an asc/desc order, if that is what you mean. I clarified the question details, but I think the issue is pretty clear in the image. – qatvinc Aug 13 '21 at 01:50
  • I can see the plot. "The issue" is what? You say `the "Post" section is flipped compared to the "Kellogg's" and "General Mills" section.` What is "flipped". Spell it out in natural language. I still suspect that the answer is to use `factor` with a levels or labels argument. And I still think this question is a duplicate of many other questions about plotting order of column arrangement or of legend ordering. – IRTFM Aug 13 '21 at 01:53
  • Move to reopen. The question is not reproducible as is, but with some effort I see the dodge order is inconsistent between categories based on the tooltip text. Not a standard "how do I order my categories" question. – Jon Spring Aug 13 '21 at 03:10
  • Thank you! I moved tool tip into the ggplot aes() and that corrected the problem. Much appreciated! – qatvinc Aug 13 '21 at 03:23
  • It did? Now I'm confused! I added to the bottom of your question with an example of a minimal reproducible example. This tries to hone in on the fewest elements of code that reproduce the issue, so that others can load it on their own computers and test attempted solutions. Your original question included a lot of pre-processing and formatting cruft that is distracting from the actual issue. – Jon Spring Aug 13 '21 at 03:29
  • Thank you, I'll keep that in mind for future requests. Not knowing for sure what was causing the issue, I wanted to provide all the information, but I understand how that could also cause a number of red herrings. – qatvinc Aug 13 '21 at 03:41

0 Answers0