Error bar and points not aligned in bar plot

Hi! So I want to make a bar plot showing several groups of data at the same time. So I have two groups of mice (ctrl and treatment), each one with its own error bar (which I can't make centered...). Also, each point is a single mouse, some of them are males and females, so that's why I have 2 colors in it. But the points have the same problem as the error bar, they are not centered. Can someone help? I imagine it's not hard but I've started this week with R studio. Thanks :slight_smile:

ggplot(data = CHE3w_hemograms, mapping = aes(x = time_point,
y = WBC,
fill = treatment)) +
stat_summary(fun.data = mean_se, geom = "col", color ="black", position = position_dodge2(0.2)) +
stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2, position = position_dodge2(0.2)) +
geom_jitter(size = 1, position = position_dodge2(0.3), aes(colour = sex)) +
scale_color_manual(values = c("#7D0006", "#004480")) +
scale_fill_manual(values = c("#C5BEC0", "#CE1812")) +
theme_classic() +
facet_wrap(~genotype)

This is more of an iterative trying than a fully justified answer:

First, use position_dodge() rather than position_dodge2(). Second, in that case a width of 1 works better than 0.2 (which would mean having the bars intersecting, since the width of the dodge becomes smaller than the width of the bar). Third, you have a small problem of the sex influencing the dodge; that can be arranged by specifying explicitly the grouping in the common aes (and adding the fill and color in the separate ones). Fourth, in geom_jitter() specifying a position via position_dodge() will actually eliminate all jitter. You need position_jitterdodge(). So something like that seems to give you the right output:

library(tidyverse)

# create fake data
CHE3w_hemograms <- expand_grid(time_point = c("24H", "1W"),
                               treatment = c("Ctrl","Urethane"),
                               sex = c("FEMALE","MALE"),
                               genotype = c("WT","KO")) |>
  add_column(WBC = map(runif(16, min=1, max = 10),
                       ~ rnorm(10, .x, 1) |> abs())) |>
  unnest(WBC)



ggplot(data = CHE3w_hemograms, mapping = aes(x = time_point,
                                             y = WBC,
                                             group = treatment)) +
  stat_summary(aes(fill = treatment),
               fun.data = mean_se, geom = "col", color ="black", position = position_dodge(1)) +
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2, position = position_dodge(1)) +
  geom_jitter(aes(colour = sex),
              position = position_jitterdodge(dodge.width = 1, jitter.width = .1),
              size = 1) +
  scale_color_manual(values = c("#7D0006", "#004480")) +
  scale_fill_manual(values = c("#C5BEC0", "#CE1812")) +
  theme_classic() +
  facet_wrap(~genotype)

Created on 2022-10-15 by the reprex package (v2.0.1)

Note that for the example, I had to create fake data, where there the mean is random for each condition, so the male and females can easily have very different means and look weird.

I imagine it's not hard but I've started this week with R studio.

I love ggplot, but to be honest that particular aspect of position_dodge() is something I never fully understood and I always have to try until it fits (often, I need position_dodge(0.8) but no idea why). So, I would say you are starting with one of the harder cases!

That's amazing!!! It's working now. Thank you so much!!! :star_struck: :star_struck:

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.