thiagoveloso
thiagoveloso

Reputation: 2763

R - How to add legend title to levelplot saved to a variable?

I would like to add the title for a legend in a levelplot graph saved to a variable.

For example, this code works:

library(lattice)
library(grid)
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y  
levelplot(z~x*y, colorkey=list(labels=list(cex=1,font=2,col="brown"),height=1,width=1.4),main=list('b',side=1,line=0.5))
trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text(expression(m^3/m^3), 0.2, 0, hjust=0.5, vjust=1)
trellis.unfocus()

But this code, where the same plot is being saved as a variable, does NOT work:

p1 <- levelplot(z~x*y, colorkey=list(labels=list(cex=1,font=2,col="brown"),height=1,width=1.4),main=list('b',side=1,line=0.5))
trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text(expression(m^3/m^3), 0.2, 0, hjust=0.5, vjust=1)
trellis.unfocus()

How can I achieve this?

Upvotes: 5

Views: 4655

Answers (1)

user20650
user20650

Reputation: 25864

This doesn't answer your question directly, but it perhaps offers a change in workflow, where you can add a title to the key using the colorkey arguments.

It involves tweaking the draw.colorkey function.

The easiest way is to use fixInNamespace interactively

fixInNamespace("draw.colorkey", "lattice")

at the end of the function change the last few lines to

    }
    if (!is.null(key$title)) {
        key.gf <- placeGrob(key.gf, textGrob(key$title, hjust = key$hjust, 
            vjust = key$vjust, gp = key$gp), row = key$row, col = key$column)
    }
    if (draw) 
        grid.draw(key.gf)
    key.gf
}

Save and close, and you can then use as shown below.


However, you may not be able to do this interactively, so it can also be done as

library(lattice)
library(grid)

# Amend key function
# Hopefully a nicer way to do this!
mykey <- draw.colorkey

body(mykey)[28:30] <- list(
quote(
  if(!is.null(key$title)){
      key.gf <- placeGrob(key.gf,
                      textGrob(key$title,hjust=key$hjust, vjust=key$vjust, gp=key$gp),
                          row=key$row, col=key$column)
  }),
body(mykey)[[28]], 
body(mykey)[[29]])

# Assign to namespace: http://stackoverflow.com/questions/6254744/override-a-function-that-is-imported-in-a-namespace
unlockBinding("draw.colorkey", as.environment("package:lattice"))
assign("draw.colorkey", mykey, "package:lattice")
unlockBinding("draw.colorkey", getNamespace("lattice"))
assign("draw.colorkey", mykey, getNamespace("lattice"))

You can then pass a key title, specifying the position

# Draw plot
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y 
p <- levelplot(z~x*y, 
              colorkey=list(labels=list(cex=1, font=2, col="brown"),
                            height=1, width=1.4,
                            title=expression(m^3/m^3), row=3, column=1, vjust=2),
               main=list('b',side=1,line=0.5))


p

Which produces

enter image description here

Upvotes: 4

Related Questions