Home>
, current situation

I want to put SEM error bar in the following beesarm plot with ggplot2.
I can add an error bar for sd, but I don't know how to output sem using stat_summary ().

The data looks like this, with fA on the x-axis and grouped by fB.

No fA fB score
1 x O 0.837216991
1 y O 0.025204773
1 z O 0.00754073
2 x O 0.007453169
2 y O 0.885924943
2 z O 0.308625439
3 x O 0.037804973
3 y O 0.021440267
3 z O 0.172021095
1 x P 0.398543642
1 y P 0.670812843
1 z P 0.961756129
2 x P 0.001630415
2 y P 0.310609978
2 z P 0.182114473
3 x P 0.049011967
3 y P 0.762299464
3 z P 0.968341695

What I want to know briefly
  • How can I find n for each data with stat_summary ()/errorbar?(Because if n is known, SEM can be obtained by sd/sqrt (n).)
    I wish I could do this, but if I can't

  • If I store the calculated sem in another data frame and use it for a graph, do I have to overlap it with layer ()?
    Since the data structure is not the average value of each group but the table that describes the data for each sample, sem is calculated separately and added to the column. .. .. That is difficult, should I make another table? I also thought. In that case, is it necessary to stack with layer ()?


The code is as follows:

avrplot =
  ggplot (data = data, aes (x = fA, y = score, fill = fB, color = fB)) +
  geom_quasirandom (dodge.width = 0.7) +
  theme_classic ()


Is it possible to add + stat_summary () or + geom_errorbar here?
I would like to know. Thank you

* Please forgive me for pointing out what about error bars in sem in the first place. I would like to do it with sd if possible, but due to various circumstances. .. ..

  • Answer # 1

    sem<-function (x) sd (x)/sqrt (length (x))
    avrplot =
      ggplot (data = data, aes (x = interaction (factor (fB), factor (fA)), y = score, fill = fB, color = fB)) +
      geom_quasirandom (dodge.width = 0.7) +
      stat_summary (fun = "mean", fun.min = function (x) mean (x) --sem (x), fun.max = function (x) mean (x) + sem (x)) +
      theme_classic ()