Kathryn Cogert
Kathryn Cogert

Reputation: 73

Changing the font of colorkey labels with draw.colorkey

I have the following sample code:

require(latticeExtra)
require(gridExtra)
require(ggplot2)
require(grid)


lay <- matrix(c(1,1,1,2), 1,4,byrow = FALSE)

z<-sort(rnorm(100))
x<- rep(seq(1,10), times=10)
y<-rep(seq(1,10), each=10)
p<- levelplot(z ~ x * y,
          par.settings=list(axis.text=list(fontfamily="serif"),),
          colorkey = list(labels=list(par.settings=list(fontfamily='serif'))))

leg.list <- p$legend$right$args$key
leg <-  draw.colorkey(leg.list)
grid.arrange(grobs = list(p, leg), layout_matrix = lay)

When I run this code I get the following image: output of above code

I would like the font of the labels in the little colorkey (part of the levelplot) to match the font of the labels in the large colorkey (made using draw.colorkey()). How can I do this?

Thanks so much for your help!

Upvotes: 1

Views: 236

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24262

Try this:

leg <-  draw.colorkey(leg.list)
leg$children[[4]]$children[[1]]$gp$cex <- 0.75
grid.arrange(grobs = list(p, leg), layout_matrix = lay)

enter image description here

Setting the same font family for the two colorkeys, we get a better result:

leg <-  draw.colorkey(leg.list)
leg$children[[4]]$children[[1]]$gp$cex <- 0.8
leg$children[[4]]$children[[1]]$gp$fontfamily <- "serif"
grid.arrange(grobs = list(p, leg), layout_matrix = lay)

enter image description here

Upvotes: 1

Related Questions