[R] Draw abbreviated key (lattice, xyplot)

Vaclav Petricek vaclav.petricek at mff.cuni.cz
Fri Mar 19 16:59:32 CET 2004


Deepayan,

> Both auto.key and simpleKey are convenience tools to make key drawing
> easy in 'typical' cases. For more flexibility, define the key as a list
> (the structure is described under 'key' in ?xyplot). You may want to
> look at ?Rows as well, in conjunction with trellis.par.get().
> See ?splom and ?cloud for examples.
>
> You are not very explicit in your description, but perhaps your only
> problem with your second solution is that the first 5 levels are in
> alphabetical order (but you haven't said what order you want them to be
> in). This is an artifact of R's factor() function, where the levels are
> by default
>
>      factor(x, levels = sort(unique.default(x), na.last = TRUE), ...
>
> So your problem may be solved simply by redefining your country variable
> appropriately:
>
> yourdata$country <-
>     factor(as.character(yourdata$country),
>            levels = c('USA', 'Germany', < ... >))
>
> (the as.character() is probably redundant)

Thank you very much - this solves my problem. Putting the countries of
interest first has two benefits
  a) different colors are assigned to them
  b) including just first n of them in the key using simpleKey is easy

As I am more used a little different notation to construct the levels in
the order I want.

t<-transform(t,country=factor(country,
       levels=unique(c(subset(country,year==1997),unique(country)))))

where

  subset(country,year==1997) are the countries I want to be first
  unique(country) after that ensures that I include all the countries and
                  no NAs are introduced

This is how I plot the abbreviated key then

attach(t)
xyplot(papers~year, groups=country, type='l',
       key=simpleKey(levels(country)[1:10],columns=2,lines=T,points=F))

Thanks again,

Vaclav


> Hope that helps,
>
> Deepayan
>
> On Thursday 18 March 2004 09:42, Vaclav Petricek wrote:
> > Hello
> >
> > I have been experimenting with xyplot, reading the help and googling
> > but I am still unable to draw an abbreviated key.
> >
> > I would like to display key for a few particular countries.
> >
> > My dataset looks like this
> >
> >    year papers        country papers.total
> > 1  1988    403            USA          551
> > 2  1988     31 United Kingdom          551
> > 3  1988     24         Canada          551
> > 4  1988     20    Netherlands          551
> > 5  1988     19         Israel          551
> > 6  1988     16        Germany          551
> > 7  1988     13         France          551
> > 8  1988     10          Italy          551
> > 9  1988      8    Switzerland          551
> > 10 1988      5          Japan          551
> > 11 1988      5        Denmark          551
> > 12 1988      3          Spain          551
> > 13 1988      3         Russia          551
> > 14 1988      2         Sweden          551
> > 15 1988      2         Poland          551
> > 16 1988      2        Finland          551
> > 17 1988      2         Brasil          551
> > 18 1988      2      Australia          551
> > 19 1988      1       Thailand          551
> > 20 1988      1         Norway          551
> > 21 1988      1         Mexico          551
> > 22 1988      1        Ireland          551
> > 23 1988      1         Greece          551
> > 24 1989    649            USA          926
> > 25 1989     53 United Kingdom          926
> > 26 1989     43         France          926
> > 27 1989     37         Canada          926
> > 28 1989     36        Germany          926
> > 29 1989     28    Netherlands          926
> > 30 1989     19         Sweden          926
> > [...]
> >
> > I use xyplot to show how many papers
> >
> > > xyplot(papers~year,groups=country,type='l',auto.key=T)
> >
> > produces an extremely long key
> >
> > > xyplot(papers~year,groups=country,type='l',key=simpleKey(levels(as.
> > >factor(country))[1:5]))
> >
> > shows *alphabetically* first five countries in the key
> >
> > > xyplot(papers~year,groups=country,type='l',key=simpleKey(c('USA','G
> > >ermany')))
> >
> > displays correct countries but the colors obviously do not match.
> >
> > Could you please point me in the right direction?
> >
> > Vaclav
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list