Skip to content

Plotting averages of y per x

3 messages · Gregor Gorjanc, Kjetil Halvorsen, Uwe Ligges

#
Hello!

I often plot average of y instead of all y values, since one can easily 
see the trend if there is to many points and/or x might be like 1, 2, 3, 
4, 5, ... and you might get a cloud (which can also be informative) 
and/or columns of points.

Anyway, learning with R i get stucked. I have the data in table 
burnins1, with the following names:

names(burnins1)
[1] "Model"       "Replication" "h2"          "burnin"

and some data
...
...
380    New          80  2     23
381    New          81  5     38
382    New          82 10     31
383    New          83 15     98
384    New          84 20     32
385    New          85 40     30
386    New          86  1     43
387    New          87  2     53
388    New          88  5     36
389    New          89 10     51
390    New          90 15     19
...
...

So I want to plot mean of variable burnin by variables model and h2. I 
compute means with

tmp <- as.data.frame(t(tapply(burnin, list(Model, h2), mean)))

and table tmp looks like this

      Old   New
1  31.00 29.36
2  30.30 28.34
5  32.92 30.66
10 39.00 37.54
15 40.66 34.07
20 39.29 35.94
40 28.63 28.51

Now I want to launch something like
plot(tmp[, 0], Old)
points(tmp[, 0], New, pch = 20)

and it gives me the error

Error in pairs.default(x, ...) : non-numeric argument to pairs

I guess that I am having problem with tmp[, 0]. If I use

plot(Old)
points(New, pch = 20)

I get the plot, but abscisa is numbered from 1 to 7 not 1, 2, 5, 10, 15, 
20 and 40. Does anyone have any sugestions?

I would also like to produce table like this, so plotting would be 
easier, since there would be no need to use points() and/or lines() 
commands to add another group.

   Model h2 mean
1  Old  1  31.00
2  Old  2  30.30
3  Old  5  32.92
4  Old  10 39.00
5  Old  15 40.66
6  Old  20 39.29
7  Old  40 28.63
8  Old  1  29.36
9  Old  2  28.34
10 Old  5  30.66
11 Old  10 37.54
12 Old  15 34.07
13 Old  20 35.94
14 Old  40 28.51
#
Gregor GORJANC wrote:

            
There is no column 0. R starts counting at 1. Try something like
plot(as.numeric(rownames(tmp)), tmp[,"Old"])
(not tested)

Kjetil

  
    
#
Gregor GORJANC wrote:
Yes, R starts indexing with 1, but not with 0.
Omit the x axis at first (e.g. using argument xaxt="n") and add it with 
axis(2, ...) and correct labels afterwards.

Uwe Ligges