Skip to content

Program advice

3 messages · John Logsdon, Peter Dalgaard, Douglas Bates

#
Hi

Starting to use R as a serious tool, I have come across a programming
problem that I can't see the answer too yet.  Can someone advise me plese.

The problem is that I want to plot a series of lines which represent short
term growths.  All the data is in a single vector and I can indicate
the index via a second vector.  In GLIM, if the second vector is a factor,
a single $GRA Size Year Item instruction will do it with a factor

Example data:
Item    Size  Year
1       0     1980
1       10    1981
1       14    1982
1       20    1983
1       25    1984
1       30    1985
2       0     1980
2       5     1981
2       6     1982
2       8     1984
3       0     1984
3       2     1985
4       0     1980
4       20    1981
4       30    1982
4       30    1984
4       35    1985

This should produce 4 jagged lines on a plot, some shorter than others.

So far, I can see no simple equivalent of the $GRA instruction with the
factors but presumably the trick is to fill up a matrix with the data in a
rather ugly loop storing the row number and incrementing on a change in
Item.  Ensure that the matrix is NA-filled first and then use matplot to
do the graph.

So (a) is there an alternative and (b) is there a single assignment that
will copy the Size vector into a matrix of some sort?  The only way I can
see to do the latter is equally messy!

Sorry to bother - I have an antipathy to ugly coding where something
elegant will do!  Perhaps a coding therapist is in order?

\John

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
John Logsdon <j.logsdon at lancaster.ac.uk> writes:
Hmm... Best I could think of till now was:

 dfr<-read.table("/tmp/data",header=T)
 evalq(plot(Year,Size,type='n'),dfr)
 invisible(lapply(split(dfr,dfr$Item), 
	function(d)evalq(lines(Year,Size,lty=Item),d)))
#
Peter> John Logsdon <j.logsdon at lancaster.ac.uk> writes:
  >> Example data: Item Size Year 1 0 1980 1 10 1981 1 14 1982 1 20
  >> 1983 1 25 1984 1 30 1985 2 0 1980 2 5 1981 2 6 1982 2 8 1984 3 0
  >> 1984 3 2 1985 4 0 1980 4 20 1981 4 30 1982 4 30 1984 4 35 1985
  >> 
  >> This should produce 4 jagged lines on a plot, some shorter than
  >> others.
  >> 
  >> So far, I can see no simple equivalent of the $GRA instruction
  >> with the factors but presumably the trick is to fill up a matrix
  >> with the data in a rather ugly loop storing the row number and
  >> incrementing on a change in Item.  Ensure that the matrix is
  >> NA-filled first and then use matplot to do the graph.

  Peter> Hmm... Best I could think of till now was:

  Peter>  dfr<-read.table("/tmp/data",header=T)
  Peter> evalq(plot(Year,Size,type='n'),dfr)
  Peter> invisible(lapply(split(dfr,dfr$Item),
  Peter> function(d)evalq(lines(Year,Size,lty=Item),d)))

A variant on this is to use the gapply function from the lme library.
This function applies a function to the subgroups of a data frame that
contains a grouping variable.

R> library( lme )
R> ydat <- read.table( "y.dat", header = TRUE )
R> plot( value ~ year, data = ydat, type = "n" )
R> gapply( ydat, function(x) lines(x$year, x$value), form = ~ group )

If you convert the data frame into a groupedData object you can make
such a plot nearly a one-liner with the plot functions for groupedData
_except_ that those plots don't work in R.  They depend on trellis
graphics functions that are not (and probably won't be) available in R.

Sorry to tantalize but here is how you would get a trellis plot of
value ~ year by group in S with the nlme library

S> ydat <- groupedData( value ~ year | group, read.table("/tmp/ydat.dat"
+   , header = TRUE))
S> trellis.device( motif )
S> plot( ydat )

If someone with a bit of time on their hands would write a function in
R using coplot that implemented some of the capabilities of the
trellis function xyplot, it would be possible to do this in R.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._