0

I have data in the following format and want to create a grouped bar plot using facet_grid() in ggplot2.

plot.df = data.frame(m   = factor(rep(1:3, 9)),
                     s   = factor(rep(rep(1:3, each=3),3)),
                     var = factor(rep(1:3, each = 9)),
                     val = rnorm(27))


ggplot(plot.df, aes(x=var, y=val, fill=m)) +
  facet_grid(.~s,scales="free") +
  geom_bar(position="dodge", stat="identity") +
  coord_flip() 

facet plot 1

Now one of the var factors has a different scale than the others. Let's say we need the different scale for all cases where var == 3. We can split it from the rest of the facets like this:

plot.df$select = c(rep(0,18),rep(1,9))

ggplot(plot.df, aes(x=var, y=val, fill=m)) +
  facet_grid(select~s,scales="free") +
  geom_bar(position="dodge", stat="identity") +
  coord_flip() 

facet plot 2

Is it possible to get a separate axis for val for these two facet groups? Or is it in general considered bad practice and I should split the plot in two separate plots in the first place?

yrx1702
  • 1,619
  • 15
  • 27
  • You can plot them separately then merge them together. [Example](https://stackoverflow.com/questions/7993722/creating-arbitrary-panes-in-ggplot2) – Tung May 05 '20 at 14:24
  • Similar to plotting separately then combining, I would recommend `ggarrange` [see information here](http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/). You would need to create the two plots--"upper" and "lower" one--then use `ggarrange(...)` to organize. I would have tried, but I would need to update my R version and can't do that now :/ – chemdork123 May 05 '20 at 17:50
  • Alright, I will stay with two separate plots then. Thanks! – yrx1702 May 05 '20 at 17:55

0 Answers0