[R] Code improvement
Sebastien.Bihorel at cognigencorp.com
Tue Oct 27 17:11:12 CET 2009
Sorry for the late reply. I wanted to thank you for putting me on the
right track. I finally got something to work not really by extracting
the legend from a xyplot call, but by building my own legend grid.frame
and passing it to my low-level function. Understanding Grobs and
grid.pack took me some time, but now, it is working like a charm.
Thank again to you (and to Paul Murrell for the grid package!)
baptiste auguie wrote:
> I don't know if it helps, but looking at the output of xyplot you can
> extract the legend (a grid.frame) as follows,
> p = xyplot(x~y, group=x,data=data.frame(x=1:10,y=1:10),
> legend = with(p$legend$right, do.call(lattice:::drawSimpleKey, args))
> and lattice::draw.key might also help if you need to customize the legend.
> 2009/10/22 Sebastien Bihorel <sebastien.bihorel at cognigencorp.com>:
>> 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
>> 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
>> 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.
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help