0

How to customize my error in the way that they are located at the middle of my bar instead of the awkward alignment as shown in attached image? I've tried to google search but couldn't find any solution.

My data frame:

data.frame(Exp1_fv)

growth_condition time  N        fv          sd          se         ci
Normal              0.0 3 0.8066667 0.005773503 0.003333333 0.01434218
Normal              0.5 3 0.8033333 0.011547005 0.006666667 0.02868435
Normal              8.0 3 0.8100000 0.010000000 0.005773503 0.02484138
Normal              24.0 3 0.8166667 0.005773503 0.003333333 0.01434218
Normal              48.0 3 0.8100000 0.010000000 0.005773503 0.02484138
High light+Chilled  0.0 3 0.8133333 0.005773503 0.003333333 0.01434218
High light+Chilled  0.5 3 0.6766667 0.068068593 0.039299420 0.16909176
High light+Chilled  8.0 3 0.4433333 0.095043850 0.054873592 0.23610201
High light+Chilled  24.0 3 0.3900000 0.045825757 0.026457513 0.11383749
High light+Chilled  48.0 3 0.6966667 0.035118846 0.020275875 0.08724005

My scripts:

Exp1_fv <- summarySE(data = Exp1, measurevar = "fv", groupvars = c("growth_condition", "time"))

ggplot(data = Exp1_fv, mapping = aes(x = factor(time), y = fv)) +
  geom_bar(stat = "identity", aes(fill = growth_condition), position = "dodge") +

  labs(title = "Fv/Fm", x = "Time (hours)",  y = "Fv/Fm", fill= "Growth condition") +
  ylim(0,1.0) +
  geom_errorbar(aes(ymin = fv - se, ymax = fv + se), width = 0.1, position = position_dodge(0.1))

enter image description here

wychin
  • 7
  • 4
  • `Error: object 'FV' not found` – r2evans May 17 '22 at 17:06
  • Hi there was some mistake in my script just now, I've just corrected it. Though I still couldn't rectify the error bars (attached image). – wychin May 17 '22 at 23:58
  • That doesn't help those of us who don't have your data or access to your R console: `could not find function "summarySE"` and again `object 'Exp1' not found`. – r2evans May 18 '22 at 02:12
  • You've provided a start for good reproducible code, but you are using at least one non-base package, and no data. The former can be resolved fairly easily, just list where you get `summarySE`. For data, you can either provide sufficient sample data (e.g., `dput(head(.,20))` or `data.frame(..)` or `read.table(..)`, see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info) or you can adapt your process to a public dataset (`mtcars`, `iris`, `ggplot2::diamonds`, many many more exist). – r2evans May 18 '22 at 02:14
  • Since your question is about plotting and not using `summarySE`, you might be able to share the contents of `Exp1_fv` only, bypassing the other-package issue. – r2evans May 18 '22 at 02:23
  • Hi, I've included the data frame. Thanks. – wychin May 18 '22 at 09:39

1 Answers1

0

errorbars do not have a width. As per position_dodge() help, when you want to dodge two different geoms with variable width (in your case geom_bar and geom_errorbar), you need to explicitly provide width argument for position_dodge(). In your case the solution should be:

Exp1_fv<-summarySE(data=Exp1, measurevar="fv", groupvars=c("growth_condition","time"))

ggplot(
  data=Exp1_fv,
  mapping = aes(x = factor(time), y = fv, fill = growth_condition)
)+
  geom_bar(stat = "identity", position = position_dodge())+
  labs(title="Fv/Fm", x= "Time (hours)", y="Fv/Fm", fill= "Growth condition")+
  ylim(0,1.0)+
  geom_errorbar(
    aes(ymin=fv-se, ymax=fv+se),
    width=.2,
    position = position_dodge(width = 0.9)
  )

Since the data is not provided, it is difficult to test and verify if this would solve your problem.

Output:

  • Thanks for the reply! However, I just tried the "geom_errorbar" command above and it doesn't change my error bars' orientation. Also I just included the data in my question. – wychin May 18 '22 at 09:42
  • It's because `fill = growth_condition` was provided in the `geom_bar` layer which is not accessible to `geom_errorbar`. I have moved it to the main layer `ggplot(mapping = aes(...)` and it works. I have edited the code above. – Lakhansing Pardeshi May 18 '22 at 10:37