[R] Code improvement
baptiste auguie
baptiste.auguie at googlemail.com
Thu Oct 22 14:33:04 CEST 2009
Hi,
I don't know if it helps, but looking at the output of xyplot you can
extract the legend (a grid.frame) as follows,
library(grid)
library(lattice)
p = xyplot(x~y, group=x,data=data.frame(x=1:10,y=1:10),
auto.key=list(space="right"))
legend = with(p$legend$right, do.call(lattice:::drawSimpleKey, args))
grid.draw(legend)
and lattice::draw.key might also help if you need to customize the legend.
HTH,
baptiste
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
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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
mailing list