1
votes

I am having a problem with plotting error bars for a dataset.

Here follows some code, i hope you can help me out, because i have researched the problem quite thoroughly, but i have not yet been able to figure out why it doesn't work. I am not a very experience programmer or R user, but i would like to think that also not a beginner.

> fruit_params
Fruits variable N     value        sd         se        ci
Apple  January 3  319.4667 289.32861 167.043950 718.73211
Apple  Febuary 3  373.8000 251.00398 144.917218 623.52846
Apple    March 3  217.8000  13.03994   7.528612  32.39300
Apple    April 3  424.6333  39.11948  22.585639  97.17816
Apple      May 3 1160.6667  40.27820  23.254629 100.05659
Apple     June 3 1510.3333 269.31828 155.490979 669.02368
Orange  January 3  241.1667  65.83877  38.012030 163.55257
Orange  Febuary 3  317.4667 204.09195 117.832541 506.99251
Orange    March 3  224.4667  23.13144  13.354941  57.46167
Orange    April 3  329.3333  18.11307  10.457586  44.99536
Orange      May 3 1279.6667 129.46943  74.749210 321.61989
Orange     June 3 1167.6667  66.16142  38.198313 164.35408

This is my data frame. I want to plot barplots, and add error bars according to standard error of the mean (se column).

library(ggplot2)
ggplot(data = fruit_params, aes(x = variable, y = value, fill = Fruits)) +
geom_bar(position = "dodge", stat="identity") +
geom_errorbar(aes(ymin = value, ymax=value+se, width=.2, position = position_dodge(0.9)))

Running this script produces the following error message:

Advarsel: Ignoring unknown aesthetics: position Don't know how to automatically pick scale for object of type PositionDodge/Position/ggproto. Defaulting to continuous. Fejl: Aesthetics must be either length 1 or the same as the data (12): ymin, ymax, width, position, x, y, fill

If you comment out the position = position_dodge(0.9) from the aes() of the geom_errorbar, you can plot the graph, but the error bars will be offset.

What is the problem? Thank you for your time :-)

1
Please use dput() to provide your datatobiasegli_te

1 Answers

1
votes

You have width and position inside aes(...). Put those two statements outside of aes(...) and it works:

ggplot(data = fruit_params, aes(x = variable, y = value, fill = Fruits)) +
    geom_bar(position = "dodge", stat = "identity") +
    geom_errorbar(aes(ymin = value - se, ymax=value + se), width = .2, position = position_dodge(0.9))

enter image description here