0

I have a dataframe group by treatment status and by a categorical variable df %>% group_by(treatment, categorical_var) %>% summarise(n=n()) and I am trying to get a similar bar plot as the one shown in picture using ggplot in which my y axis would be determined by my $n$ variable and my x axis would be determined by my $categorical_var$

enter image description here

As shown in the picture I am basically trying to merge two bar charts in the same plot one for the control group and the other for the treatment group. Any help on how to do this?

Here is a reproducible example


example <- tribble(
  ~treatment, ~categorical_var, ~n,
  "control",            "1",    10,
  "control",            "2",    12,
  "control",            "3",     7,
  "treatment",          "1",     14,
  "treatment",          "2",     5,
  "treatment",          "3",     11,
)


ggplot(example, aes(categorical_var, n)) + 
  geom_bar(position="dodge",stat="identity") + facet_wrap(~treatment)

And here is the putput I get, how can I change the style to get something like the picture from above?

enter image description here

neuron
  • 1,949
  • 1
  • 15
  • 30
Quinoba
  • 41
  • 8
  • To help us to help would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data, the code you tried and the packages you used? – stefan Dec 03 '21 at 07:56
  • ... this said: Using `ggplot2` this could be easily achieved via faceting. See e.g. https://stackoverflow.com/questions/13162489/arrange-barcharts-side-by-side-with-ggplot2-and-r – stefan Dec 03 '21 at 07:57
  • 1
    I added a reproducible example with my code @stefan – Quinoba Dec 03 '21 at 08:15
  • Does this answer your question? [ggplot multiple grouping bar](https://stackoverflow.com/questions/17303573/ggplot-multiple-grouping-bar) – tjebo Dec 03 '21 at 11:50
  • Related post with several options: [Axis labels on two lines with nested x variables (year below months)](https://stackoverflow.com/questions/44616530/axis-labels-on-two-lines-with-nested-x-variables-year-below-months) – Henrik Dec 04 '21 at 10:38

1 Answers1

1

Styling always involves a bit of fiddling and trial (and sometimes error (;). But generally you could probably get quite close to your desired result like so:

library(ggplot2)

ggplot(example, aes(categorical_var, n)) + 
  geom_bar(position="dodge",stat="identity") + 
  # Add some more space between groups
  scale_x_discrete(expand = expansion(add = .9)) +
  # Make axis start at zero
  scale_y_continuous(expand = expansion(mult = c(0, .05))) +
  # Put facet label to bottom 
  facet_wrap(~treatment, strip.position = "bottom") +
  theme_minimal() +
  # Styling via various theme options
  theme(panel.spacing.x = unit(0, "pt"), 
        strip.placement = "outside", 
        strip.background.x = element_blank(),
        axis.line.x = element_line(size = .1),
        panel.grid.major.y = element_line(linetype = "dotted"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank())

stefan
  • 90,330
  • 6
  • 25
  • 51