-1

I am trying to create a means & confidence interval plot for my data set. But I need to group it by two different factors. I have four treatments (Control, Virgin, Low EE2, High EE2) and I took measurements for the total distance moved across the 4 treatments on two separate days (D14 & D21). I would also like the order to be as stated above for each factor. Any help would be appreciated, I'm very new to using R!

Here is the current code I have for my ggplot...

f <- factor(c("Control", "Virgin", "Low EE2", "High EE2"), levels=c("D14", "D21"))

ggplot(Working_Exp_1_Data_summary, aes(x=Juvenile.Treatment, y=Distance.moved, color=Juvenile.Treatment, fct_relevel(f))) +
  geom_errorbar(aes(ymin=Distance.moved-ci, ymax=Distance.moved+ci), width=0.2) +
  geom_line() +
  geom_point()

And my current graph...

plot

  • 2
    Welcome! If you add data to your question to [make your example reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), you're more likely to get an answer! – alistaire Apr 29 '23 at 23:22

1 Answers1

0

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)) 

enter image description here

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)) 

enter image description here

colebrookson
  • 831
  • 7
  • 18