Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created May 1, 2014 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/89fb4d00564d172fff4e to your computer and use it in GitHub Desktop.
Save timelyportfolio/89fb4d00564d172fff4e to your computer and use it in GitHub Desktop.
lattice legend positioning example

Some examples of legend positioning in R lattice. There is nearly infinite flexibility if there is strong desire for customization. This illustrates some of the options available.

A lot of times I like to avoid legends entirely and use the direct.labels package to label the graphics directly.

require(lattice)
#should have just used r dataset but did this instead
df <- data.frame(
x = rep(1:20,5)
,y = runif(100)
,label = as.vector(sapply(
1:5
,function(x){
return(rep(paste(LETTERS[runif(4,1,26)],sep="",collapse=""),20))
}
))
)
xyplot(
y ~ x
,groups = label
,data = df
,type = "b"
#,auto.key = list(
# space = "bottom" # top, bottom, right, left
# ,title = "RandomGroup"
# ,columns = 5
# ,just = 0.1
#)
#very manual way
,auto.key = list(
title="RandomGroup"
#, columns = 5
, corner = c(1, 0)
, x = 0.95
, y = 0.1
)
, par.settings = list(
#layout.heights = list(top.padding = 10),
layout.widths = list(right.padding = 25)
)
,lattice.options = list(legend.bbox="full") #could also be panel
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment