1

I would like to design a horizontal ggplot barchart with 3 groups on the left axis (country, retailer and category). Following inspiration from R ggplot // Multiple Grouping in X-axis I can design it on the x axis, however, when I then apply coord_flip the country and retailer groups remain on the x axis. How can the also be placed to the left please?

I have attempted with both coord_flip (Attempt 1) and swapping x and y in the initial call to ggplot (Attempt 2) without success. Ultimately this doesn't require to be in a ggplot, but I do require to house it in a shiny app.

Thanks in advance.

library(ggplot2)
library(magrittr)
set.seed(1234)

countries  <- c('AU', 'NZ', 'UK', 'SA')
retailer   <- c(paste0('rtlr', 1:6))
categories <- c(paste0('cat', 1:4))

df <- tibble(
  countries    = sample(countries,  50, replace = T)
  , categories = sample(categories, 50, replace = T)
  , retailer   = sample(retailer,   50, replace = T)
  , n          = sample(1:200   ,   50, replace = T)
)

# Attempt 1

df %>%
  ggplot(aes(x = categories, y = n)) +
  geom_col(show.legend = F) +
  facet_grid(
    ~ countries + retailer
    , scales = 'free'
    , switch = 'x'
    , labeller = label_both
    ) +
  coord_flip()

 # Attempt 2
df %>%
  ggplot(aes(x = n, y = categories)) +
  geom_col(show.legend = F) +
  facet_grid(
    ~ countries + retailer
    , scales = 'free'
    , switch = 'x'
    , labeller = label_both
    )
CallumH
  • 751
  • 1
  • 7
  • 22
  • Can you provide any sample plot for your desired output? Even something similar with different variable names will do. – Shibaprasadb Oct 21 '21 at 12:08
  • another option for any horizontal plot which you may want to look into is the `ggstance` package. It has a few horizontal geoms that make it possible to NOT use coord_flip which comes in handy if you want to do other stuff with your graphs – tjebo Oct 21 '21 at 12:16
  • 1
    I'll definitely look at ggstance. Super thnx – CallumH Oct 21 '21 at 12:23

1 Answers1

0

not 100% sure if this is what you're looking to do, but you can try using the rows and cols arguments instead of the formula notation.

For example

df %>%
  ggplot(aes(x = categories, y = n)) +
  geom_col(show.legend = F) +
  facet_grid(
    rows = vars(countries, retailer) 
    , scales = 'free'
    , switch = 'x'
    , labeller = label_both
    ) +
  coord_flip()

(I don't have access to my computer and running R online, thus struggling to embed the resulting figure, apologies)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • P.S. I find your use of commas at the beginning of the line instead of the end rather confusing! But I guess that's a style question – tjebo Oct 21 '21 at 12:08
  • 1
    Thanks for the solution and apologies for the style confusion, I find it easier on my eyes and for commenting out during development. This answer is close to what I am looking for... the categories come before the country and retailer this way but it's probably got me on my way. I am also having some unusual limitations on my computer today so I'll accept soon. – CallumH Oct 21 '21 at 12:22