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:
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")
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)