3

I want to compare data in a facet-alike manner using a bar plot, but missing data causes the bar "widths" to be different:

enter image description here

How can I

  1. ensure the same bar size ("width") and
  2. [nice to have] shrink the panel size of each facet to the really required size?

PS: I do not want to show empty data as "zero-bar" to waste no space.

library(ggplot2)
data <- data.frame(product = c("Fish", "Chips", "Baguette"),
                   store = c("London", "London", "Paris"),
                   customer.satisfaction = c(0.9, 0.95, 0.8),
                   stringsAsFactors = TRUE)
data

ggplot(data, aes(x = product, y = customer.satisfaction)) +
geom_bar(stat = "identity", width = 0.9) +
coord_flip() +
facet_wrap( ~ store, ncol = 1, scales = "free_y")
R Yoda
  • 8,358
  • 2
  • 50
  • 87

1 Answers1

6

Perhaps an approach using facet grid would be satisfactory:

ggplot(df, aes(x = product, y = customer.satisfaction)) +
  geom_bar(stat = "identity", width = 0.9) +
  coord_flip() +
  facet_grid(store ~., scales = "free", space = "free")

enter image description here

missuse
  • 19,056
  • 3
  • 25
  • 47
  • Excellent result! My only point is the facet labels should be displayed on the top of the plot what `facet_grid` not supports IMHO (as `facet_wrap` does with the `strip position` argument). Any idea? – R Yoda Feb 24 '18 at 11:09
  • @R Yoda I'm not aware of this functionality in ggplot2. The desired behavior can be achieved by manipulating the grobs as explained [here](https://stackoverflow.com/questions/31572239/set-space-in-facet-wrap-like-in-facet-grid) – missuse Feb 24 '18 at 11:21