[R] Code improvement
Sebastien Bihorel
sebastien.bihorel at cognigencorp.com
Thu Oct 22 14:07:10 CEST 2009
Dear R-Users,
I would like to have the opinion of the list on the following matter. I
have this generic function that creates multiple lattice scatterplots
per page based upon different subsets of the same dataset. The use of
different line/point colors/symbols in each plot is based upon a
'group' variable', which is the same for all plots. My goal is to create
a main legend per page:
1- because the 'group' variable' is the same for all plots, one legend
per page is sufficient;
2- because the subset of data used for a particular scatterplot does not
have to contain all unique elements of the group variable in the whole
dataset, the graphical settings need to be adjusted for each plot based
upon a general list of settings created from the whole dataset prior to
the creation of the plots;
3- for the same reason, I cannot use the key argument of xyplot to
create a legend from the first scatterplot.
Another piece of info is that this generic function could be used to
create very different categories of graphs, which each require a
different legend design.
At the moment, I am creating the legend based upon the general graph
settings and the category of plot. Prior to the creation of the graphs,
I store grid objects (text, point, line, or rectangle) into a list, with
also some information about the number of lines that the legend will
use, and the category of the plot. After the plots are printed to the
device, I open a viewport at the bottom of my page and split it
according to the type of the graph and the number of lines it needs.
Finally, I just loop through the content of the list and draw the grid
objects.
Overall, this is all fine but a bit 'brute force'. Also the final
'open-a-viewport-and-draw-inside' step is a big messy code within a
generic graph function, because one specific piece of code is needed per
category of plot, and because of additional subtleties (conditional
argument, etc... )
Based upon the behavior of the xyplot function, I understand that it
would be possible to store the fully formatted legend directly at its
creation and then just 'print' the stored object within my legend
viewport. Could anybody advise me on the process to follow to accomplish
that or maybe a few functions to look at?
As always, any help would be greatly appreciated.
Sebastien
More information about the R-help
mailing list