CeC
CeC

Reputation: 85

How to make separate mean, errorbar on top of a scatter plot and how to make a horizontal mean line?

I want to make a position_dodged scatter plot with means represented as horizontal lines (with error bars).

I'm having trouble position_dodging the mean and error bars using stat_summary() and making the mean appear as a horizontal line.

I want to show that the mean is different between the sexes. I know each group has different means but I don't know how to visualize it.

download.file(url="https://ndownloader.figshare.com/files/2292169",
              destfile = "~/portal_data_joined.csv")

surveys <- read.csv

surveys_cln <- surveys %>% filter(sex=="F"|sex=="M")

ggplot(data = surveys_cln, mapping = aes(x=species_id, y=weight, color=sex))+
  geom_jitter(alpha=.6, position = position_dodge(.5))+
  stat_summary(fun.data = mean_sdl, fun.args = list(mult=1), 
               geom="errorbar", color="red", width=.5, position=position_dodge2(width=.9))
  stat_summary(fun.y=mean, geom="errorbar", color="red", width = .75, linetype = "dashed", position=position_dodge(9))

I only have one mean+errorbar per condition, not for both groups of a condition. If I could show the mean+errorbar for both sexes separately, that'd be great.

Here is image

link to the resulting image

Hi! Thank you for your help. Here's an updated code:

  ggplot(data = surveys_cln, mapping = aes(x=species_id, y=weight, fill=as.factor(sex), shape=as.factor(sex)))+

  geom_jitter(alpha=.6, position = position_dodge(.5))+
  stat_summary(aes(group = sex), fun.data = mean_sdl, fun.args = list(mult=1), 
  geom="errorbar", width=.5, position=position_dodge2(width=.9))+
  stat_summary(fun.y=mean, geom="errorbar", width = .75, linetype = "dotted", position=position_dodge(9))+

  scale_fill_manual(values=c("#006D2C","#DEEBF7"))+
  #scale_fill_brewer(palette="Paired")+

  theme_bw()+
  #theme(text=element_text(size=30))+
  theme(#legend.position = "none",
    plot.title = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.title.x = element_text(),
    axis.text.x = element_text(),
    axis.ticks = element_blank(),
    axis.text.y = element_text(size=rel(.7)))

Could you help me with the colors? I want F=#000000 and M=#73000a.

Also, is there anyway to visualize the mean as a horizontal bar in between the errorbars?

Here's 2nd try

link to the resulting image

Thanks again!

Upvotes: 0

Views: 325

Answers (2)

Dan Chaltiel
Dan Chaltiel

Reputation: 8484

While @abichat's answer is totally correct, you could also just remove the color="red" argument, which causes ggplot not to group your data.

This way, your plot will show the errorbars with the good color (maybe increase their thickness to improve the readability).

Upvotes: 1

abichat
abichat

Reputation: 2416

You need to add aes(group = sex) in your geom_*().

ggplot(data = surveys_cln,
       mapping = aes(x = species_id, y = weight, color = sex)) +
  geom_jitter(alpha = .6, position = position_dodge(.5)) +
  stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), 
               aes(group = sex), 
               geom = "errorbar", color = "red", width = .5, 
               position = position_dodge2(width = .9))

enter image description here

Upvotes: 1

Related Questions