As @alistaire mentioned, it's always easier to help when your example is reproducible, but I've tried here.
Your data (approximately):
df <- data.frame(
estimate = c(3200, 3400, 2780, 3790, 3100, 3750, 2900, 4900),
treatment = c("control", "control", "high ee2", "high ee2", "low ee2",
"low ee2", "virgin", "virgin"),
day = rep(c("d14", "d21"), 4)
)
df$ci_up <- df$estimate + 500
df$ci_lo <- df$estimate - 500
Your first challenge of plotting the x-axis in a particular order is one of ordering a factor, which has answers here and other places. But to do so, use factor()
:
df$treatment = factor(df$treatment, levels = c("control", "virgin",
"low ee2", "high ee2"))
Now, you plot your data:
ggplot(df) +
geom_point(aes(x = treatment, y = estimate, colour = treatment)) +
geom_errorbar(aes(x = treatment, ymin = ci_lo, ymax = ci_up,
colour = treatment), width = 0.2)
But I think you also wanted to order based on the day? Guessing that D14 is first and D21 is second chronologically, we can order them in such a fashion:
df$day = factor(df$day , levels = c("d14", "d21"))
And then plot:
ggplot(df) +
geom_point(aes(x = treatment, y = estimate, colour = treatment,
shape = day),
position = position_dodge(width = 0.5)) +
geom_errorbar(aes(x = treatment, ymin = ci_lo, ymax = ci_up,
colour = treatment, group = day), width = 0.2,
position = position_dodge(width = 0.5))

We don't have to change the shape for the day, but it could be useful. To keep the errorbars and the points aligned with each other and not stacked on the day groups, we added group = day
to the errorbar call, and for both, used position_dodge()
. If you wanted to remove the different shapes, to keep the points/errorbars from stacking on each other for the same treatment, we would add group = day
to geom_point()
as well:
ggplot(df) +
geom_point(aes(x = treatment, y = estimate, colour = treatment,
group = day),
position = position_dodge(width = 0.5)) +
geom_errorbar(aes(x = treatment, ymin = ci_lo, ymax = ci_up,
colour = treatment, group = day), width = 0.2,
position = position_dodge(width = 0.5))
