2

I'd like to use R to plot runtime results for function f. At each datapoint on the x-axis, I run the simulation 10 times, I have 3 separate lines. At each datapoint, I'd like to also include the range with vertical lines illustrating the highest and lowest runtimes, while the data line goes through the point for the average value of each set of 10 results.

To make myself clearer, I have mocked up a plot that I am trying to describe: An example of desired plot with min/max/mean

I am not familiar with R, and so I am not sure how best I should save my dataset. This is what my data might look like for the solid line at the bottom:

------------------------------------------
|       | Runs for solid line            |
------------------------------------------
| Input | 1   |  2  |  3   |  4   |  5   |
------------------------------------------
| 2000  | 0.6 | 0.7 | 0.75 | 0.51 | 0.6  |
| 4000  | 0.9 | 1.0 | 1.1  | 0.8  | 0.7  |
| 6000  | 1.4 | 1.3 | 1.5  | 1.43 | 1.56 |
------------------------------------------

I can imagine two more tables: one for the dotted line, and one for the irregular dotted line. For the table above, I'd like a vertical line at 2000 indicating max=0.75, min=0.6, and the solid line to go through the mean value of 0.632 . The same for the regular and irregular dotted lines.

Rob Stewart
  • 1,812
  • 1
  • 12
  • 25
  • Have a look at the function `raw.means.plot` in package `plotrix`. Instead of the lines it prints the raw data (so you get the minimum and maximum). The data needs to be in the long format (i.e., one observation per row). With [sample data](http://stackoverflow.com/q/5963269) you will get much better answers. – Henrik Jul 04 '13 at 16:28
  • You can also use `boxplot` – nico Jul 04 '13 at 16:33
  • I downvoted for lack of effort. – Roland Jul 04 '13 at 16:37
  • @Roland answered a similar question which gives me more or less what I am looking for: http://stackoverflow.com/questions/13032777/scatter-plot-with-error-bars . The link in the comments on that Q is also relevant: http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/ – Rob Stewart Jul 04 '13 at 17:15

1 Answers1

3

I recommend reading an introduction to R.

Your data:

DF <- read.table(text="Input  1     2    3     4     5   
 2000   0.6  0.7  0.75  0.51  0.6  
 4000   0.9  1.0  1.1   0.8   0.7  
 6000   1.4  1.3  1.5   1.43  1.56",header=TRUE)

Let's reshape it to a more sensible format (long format):

library(reshape2)
DF <- melt(DF,id="Input")

Add an id for the line:

DF$group <- "a"

A second line:

DF2 <- DF
DF2$value <- DF2$value+1
DF2$group <- "b"

Put all data in one data.frame:

DF <- rbind(DF,DF2)

Now we can plot. There are numerous possibilities for plotting. Here is one that includes calculating the summary stats you want to plot:

library(ggplot2)

ggplot(DF,aes(x=Input,y=value,group=group)) + 
  stat_summary(fun.y="mean",geom="line",aes(linetype=group)) +
  stat_summary(fun.ymin="min",fun.ymax="max",geom="errorbar")

enter image description here

Roland
  • 127,288
  • 10
  • 191
  • 288