Skip to content

xyplot Legend Title and Position

4 messages · Justin McBride, Dennis Murphy, Deepayan Sarkar

#
Dear R Community,

I'm using xyplot in Lattice with a legend and a title on the legend.
The title on legend is being cut off, as can be seen by running the
code below.   The legend is on the right, but I would like to get to
the top right of the graphics window.  Is there a way to get the
legend title to display correctly and move the whole legend up the the
top right?

Thanks,
Justin

### R code

library(lattice)
Yield=c(16, 17, 11, 8, 16, 18)
Date = c(1, 1, 2, 3, 4, 5)
Machine = c(1, 3, 2, 2, 3, 1)


xyplot(Yield ~ Date,
       groups=Machine,
       auto.key=list(title="Machine", space = "right", cex=1.0),
        par.settings = list(superpose.symbol=list(pch = 0:18, cex=1)),
       )
#
Hi:

Part of the problem is that you have a point in the upper right corner
of your plot, so one way around it is to expand the y-range. Try this:

xyplot(Yield ~ Date,
      groups=Machine, ylim = c(7, 22),
      auto.key=list(title="Machine", corner = c(0.95, 1), cex=1.0),
       par.settings = list(superpose.symbol=list(pch = 0:18, cex=1)),
      )

HTH,
Dennis
On Tue, Jun 14, 2011 at 4:36 PM, Justin McBride <crazyhawk48 at gmail.com> wrote:
#
Dennis,

Thanks for your suggestion, but that is not exactly what I was after.
I was trying to get the legend in the margin on the top right of the
page and not in the plot frame.  Is there a way to do this?

Thanks,
Justin
On Tue, Jun 14, 2011 at 6:03 PM, Dennis Murphy <djmuser at gmail.com> wrote:
5 days later
#
On Wed, Jun 15, 2011 at 6:32 PM, Justin McBride <crazyhawk48 at gmail.com> wrote:
One option is:

xyplot(Yield ~ Date,
       groups=Machine,
       auto.key=list(title="Machine", space = "top", cex=1.0, just = 0.95),
       par.settings = list(superpose.symbol=list(pch = 0:18, cex=1)))

Strictly speaking, this places the legend right-justified on the top
(which is not quite the top-right corner). The other option, which
gives you more flexibility, but needs you to control space manually,
is

xyplot(Yield ~ Date,
       groups=Machine,
       auto.key=list(title="Machine", corner = c(1, 1), x = 0.95, y =
1, cex=1.0),
       par.settings = list(superpose.symbol=list(pch = 0:18, cex=1),
                           layout.heights = list(top.padding = 8),
                           layout.widths = list(right.padding = 8)),
       lattice.options = list(legend.bbox = "full")
       )


-Deepayan