0

The current graph I have

I am trying to get a scatter plot in R. I have two factors (group and sex), and I want to show the sex in colors separately in the same graph (together with its confidence interval error bars. (Now all my data seems together without considering sex)

Here is my R code:

ggplot(data, aes(x=reorder(group_general, weight, color = sex, 
                 FUN=mean), y=weight)) + 
  geom_jitter(colour="lightblue", alpha=1, width=0.1) +
  geom_point(stat="summary", fun.y="mean") + 
  geom_errorbar(stat="summary", fun.data="mean_se", fun.args =   
                list(mult = 1.96), width=0) +
  labs(x="Group", y="weight(mean + 95%CI)") +
  theme_bw() +
  theme(axis.text.x=element_text(angle=45, hjust=1)) 

Any recommendations?

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
mervet
  • 11
  • 1
  • 1
    could we please have a [mcve]? – Ben Bolker Jun 25 '23 at 18:08
  • I am trying the find out the effect of some groups and sex on the weight. In the scatter plot I want to show the data with the confidence intervals but with different colors for sex. I am attaching my current plot. As you can see all the data is gathered together. However, I want to show them seperately for each group I have. – mervet Jun 25 '23 at 18:16
  • 1
    To make color depend on sex you need to map it within `aes` like `geom_something(aes(color = sex), ...)` *and not override it* by specifying a constant color value like in `geom_jitter(..., colour="lightblue")` afterwards. – I_O Jun 25 '23 at 18:16
  • 1
    A plot helps, but it is not a reproducible example. See e.g. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ben Bolker Jun 25 '23 at 18:21
  • 1
    It looks like this may be a typo. what happens if you change your mapping to `aes(x=reorder(group_general, weight, FUN=mean), y=shoot, color = sex))` (i.e. put `color = sex` outside the `reorder()` call?) – Ben Bolker Jun 25 '23 at 18:24
  • It worked! Thanks Ben – mervet Jun 25 '23 at 18:27

0 Answers0