[R] graphics

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Aug 30 20:49:49 CEST 2005


On 8/30/05, Karsten Rincke <rincke at physik.uni-kassel.de> wrote:
> Hello,
> I guess a have a very simple problem though up to now couldn't solve it:
> I want to plot two datasets wihtin one plot like plot(x) provides it for
> one dataset(type="b" that is: points connected by lines).
> Example data 'x':
> Befragung1   Befragung2   Befragung3   Geschlecht
> 2.25   2.34   1.78   weiblich
> 1.34   3.45   2.23   maennlich
> The two rows of the example above form two datasets. Now I'm looking for
> something like plot(~x |Geschlecht, ... ): X-axis containing
> Befragung1...3, y-axis containing the values given by the matrix 'x',
> points within one dataset (row of 'x') connected by lines.
> I already tried various possibilities provided by 'lattice' or 'grid'  -
> but there seems to be a basic misunderstanding on my side so that I
> could not produce any good result.
> Would by very happy to get some hints!



R likes data vectors to be columns, not rows.  With your data, you
could try


matplot(t(mydata[, 1:3]), type = 'b')


but it would probably make more sense to have the data in a different
format to begin with.  It could either be in the `wide' format:



Befragung   weiblich  maennlich
1           2.25      1.34
2           2.34      3.45
3           1.78      2.23


or the `long' format:


Befragung   y         Geschlecht
1           2.25      weiblich
2           2.34      weiblich
3           1.78      weiblich
1           1.34      maennlich
2           3.45      maennlich
3           2.23      maennlich



With the wide format you could use matplot as before, or use xyplot
from lattice:


mydata <-
    read.table(textConnection("
Befragung   weiblich  maennlich
1           2.25      1.34
2           2.34      3.45
3           1.78      2.23"), header = TRUE)

xyplot(maennlich + weiblich ~ Befragung,
       data = mydata,
       type = 'b',
       auto.key = TRUE)


With the long format, you could similarly do:


mydata <-
    read.table(textConnection("
Befragung   y         Geschlecht
1           2.25      weiblich
2           2.34      weiblich
3           1.78      weiblich
1           1.34      maennlich
2           3.45      maennlich
3           2.23      maennlich
"), header = TRUE)

xyplot(y ~ Befragung, data = mydata, groups = Geschlecht,
       type = 'b', auto.key = TRUE)


Hope that helps,

-Deepayan




More information about the R-help mailing list