[R] re: help with lattice plot

Deepayan Sarkar deepayan at stat.wisc.edu
Tue Jul 27 06:20:35 CEST 2004


On Monday 26 July 2004 21:51, Eve.McDonald-Madden at dse.vic.gov.au wrote:
> Dear List,
>
> I have been using R to create an xyplot using the panel function
> within lattice libraries. This plot is based on the data supplied in
> R named 'Oats'. The graph represents oat yield by nitro level with an
> overlay of each variety of oats for each nitro level.
>
> I have three questions regarding this graph:
> 1) I cannot seem to specify the type of symbol used by the plot, even
> though it is included in the code below, it will change when the code
> is changed?

You are doing several things wrong. There's more than one way to fix it. 
For example, the approach you have taken may be modified to give (after 
the lset(col.whitebg()) call in your example):


lset(list(superpose.symbol = list(pch = 18)))
xyplot(yield ~ nitro | Block,
       data = Oats,
       xlab = "Nitrogen Level",
       ylab = "Yield of oats",
       groups = Variety,
       as.table = T,
       panel = function(x, y, groups, subscripts) {
           panel.superpose(x = x, y = y, groups = groups,
                           subscripts = subscripts)
           panel.superpose(x = x, y = y, groups = groups,
                           subscripts = subscripts,
                           type ="r")
       },
       key =
       list(points = Rows(trellis.par.get("superpose.symbol"), 1:3 ),
            text = list(lab = levels(Oats$Variety))))

A line by line comparison with your code should indicate the 
differences.

But since you are using some lattice-specific features (not available in 
S-PLUS) anyway, I would take it a bit further and simplify this to 
(again, after the lset(col.whitebg()) call in your example):

xyplot(yield ~ nitro | Block,
       data = Oats,
       xlab = "Nitrogen Level",
       ylab = "Yield of oats",
       groups = Variety,
       as.table = TRUE, 
       type = c("p", "r"),
       par.settings = list(superpose.symbol = list(pch = 18)),
       auto.key = TRUE)


> 2) I have managed to include a legend on the graph using the key
> function; however the labels of the legend do not seem to correspond
> to the right coloured symbol as used in the plot itself for Variety.
> How can I get the symbols and text in the legend to match that used
> in the graph itself? 

Wherever there's the need for 'ordering' a factor, the order is 
determined by its levels - given by the levels() function. In this 
case, the order would be given by levels(Oats$Variety), and not 
as.character(unique(Oats$Variety)) which you were using for some 
reason. This fact is relevant to your next question as well.

> 3) Also, I am interested to know how I can 
> manipulate the order in which each graph (in this instance Nitro
> level) appears? For example, at the moment the graphs do not appear
> in numerical order (based on the levels of the factor nitro) can I
> reorder them so that they appear in the order   I, II,  III,
>                                                              IV,  V, 
>  VI ?

Surely you mean Block and not nitro?

It behaves in this way because that's the order given by 
levels(Oats$Block) (the reason should be apparent after 
reading ?groupedData from nlme). You can choose your own order when 
using the factor function (your current use doesn't do anything, by the 
way):

Oats$Block <-
    factor(Oats$Block,
           levels = c("I",  "II",   "III", "IV",  "V",  "VI"))


Hope that helps,

Deepayan




More information about the R-help mailing list