1

I have data which represent the results of a conducted experiment that I want to plot using R. I am retentively new to R and my information are limited. My data are stored in pairs (mean and standard deviation) for each of the experimented methods using different number of examples. For example:

 Method A Method B Method C Mean StDv Mean StDv Mean StDv 1 54.113, 3.469, 51.039, 0.774, 96.257, 1.861 2 55.432, 3.78, 51.921, 1.109, 90.705, 1.284 3 57.047, 3.673, 52.397, 1.054, 90.616, 1.122 4 58.338, 3.919, 53.152, 1.348, 91.024, 0.811 

Where 'Mean' and "StDv' is the mean and standard deviation respectively. Moreover, the first column (1, 2, 3, and 4) represents the size of the experimented data. In other words, when the size of the data was 2, the first method scored 55.432 ± 3.78 and the second scored 51.921 ± 1.109 and so on.

The plot that I am after is having the values of the data size (the first column) as the labels of the x-axes, whilst the value of the y-axes are clearly represents the performance (between 0-100) that is the average (or the mean in the table) scored value. Moreover, I want to add the standard deviation to the plot as an error bar. I don't mind if the plot is a bar- or line-chart either will do.

1 Answer 1

1

Concerning your specific problem:

You will want to format your data as a data.frame with columns size, mean, stdv and method (melt from reshape2 should help you for this). Then you can run code like this below:

p <- ggplot(data, aes(x=size, y=mean, fill=method)) p <- p + geom_bar(position=position_dodge(), stat="identity") p <- p + geom_errorbar(aes(ymin=mean-stdv , ymax=mean+stdv), width=.2, position=position_dodge(.9)) print(p) 

For options and details, these links should help you:

http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/#bar-graphs

Placement of error bars in barplot using ggplot2

Sign up to request clarification or add additional context in comments.

5 Comments

I think you should make this into a comment. Answers should provide some more details, possibly with relevant code, so that the answer stands on its own. The linked page can go offline unnoticed, thus rendering the answer useless.
Thank you guys, but I'm still not sure how to use the mean columns to plot a single bar per method for each data-size point. The following command plots the values of the first method including the standard deviation as error-bar and smooth. qplot(size,M1_M, data=ds)+geom_smooth(aes(ymin=M1_M-M1_S, ymax=M1_M+M1_S), stat="identity") +geom_errorbar(aes(size, ymin=M1_M-M1_S, ymax=M1_M+M1_S), width=0.1)
Did you see my new post? Please clarify if you have problems reshaping the data (i.e. into long format with four columns size,mean,stdv,method) or if you have problems running the plot code.
Thanks CMicheal, actually I don't know how to use this "reshape2" which worth to read about in the future. Also, the solution you have proposed I knew but I wish there is another way around before changing the shape of the data. Thanks again for your valuable work suggestion.
Generally speaking, I would say that with ggplot2 it will be difficult to plot without adapting your data format. If you want to keep your data structure, you may want to look into lattice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.